2017-04-18 5 views
0

Ich habe ein data.frame die Quelle-Ziel bringt fließt:Aktualisierungsmatrixelement, das durch durch einen Looping data.frame

#od flows data.frame with trips per year as flows 
set.seed(123) 
origin <- c(rep(1,3),rep(2,3),rep(3,3)) 
destination <- c(rep(1:3,3)) 
flow <- c(runif(9, min=0, max=1000)) 
od_flows <- data.frame(origin,destination,flow) 

# od matrix with all possible origins and destinations 
od_flows_all_combos <- matrix(0,10,10) 

od_flows 
od_flows_all_combos 

> od_flows 
    origin destination  flow 
1  1   1 287.5775 
2  1   2 788.3051 
3  1   3 408.9769 
4  2   1 883.0174 
5  2   2 940.4673 
6  2   3 45.5565 
7  3   1 528.1055 
8  3   2 892.4190 
9  3   3 551.4350 
> od_flows_all_combos 
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] 
[1,] 0 0 0 0 0 0 0 0 0  0 
[2,] 0 0 0 0 0 0 0 0 0  0 
[3,] 0 0 0 0 0 0 0 0 0  0 
[4,] 0 0 0 0 0 0 0 0 0  0 
[5,] 0 0 0 0 0 0 0 0 0  0 
[6,] 0 0 0 0 0 0 0 0 0  0 
[7,] 0 0 0 0 0 0 0 0 0  0 
[8,] 0 0 0 0 0 0 0 0 0  0 
[9,] 0 0 0 0 0 0 0 0 0  0 
[10,] 0 0 0 0 0 0 0 0 0  0 

Ich möchte die od_flows_all_combos Matrix mit Werten der od_flows aktualisieren data.frame solchen Diese Ursprungswerte (in df) entsprechen den Spaltennummern (in der Matrix) und den Zielwerten (in df) den gleichen Zeilen in der Matrix. Beispiel:

Aktualisieren Sie od_flows_all_combos [1,1] mit 287.5775 usw. für alle Zeilen in df.

Ich würde gerne über die data.frame od_flows nach Zeilen "loopen" und dabei eine apply-Funktion verwenden. Dies ist nur ein Beispiel. Mein aktuelles od_flow data.frame hat ein Dim (1'200'000 x 3) und die Matrix (2886x2886). Also brauche ich einen effizienten Ansatz für dieses Problem. diese

Mein war erster Ansatz:

for(i in 1:nrow(od_flows)){ 
    od_flows_all_combos[rownames(od_flows_all_combos)==od_flows[i,2],colnames(od_flows_all_combos)==od_flows[i,1]] <- od_flows[i,3] 
    } 

Berechnung noch nicht beendet ist ...

mir jemand mit einer Lösung helfen könnte mit einer Funktion anwenden?

Vielen Dank!

Antwort

0

Sie können den od_flows Datenrahmen als Matrix direkt organisieren, unter der Annahme, od_flows füllt komplett your_desired_matrix

require(dplyr) 

set.seed(123) 
origin <- c(rep(1,3),rep(2,3),rep(3,3)) 
destination <- c(rep(1:3,3)) 
flow <- c(runif(9, min=0, max=1000)) 
od_flows <- data.frame(origin,destination,flow) 

od_flows_order = od_flows %>% arrange(origin, destination) 

your_desired_matrix = matrix(od_flows_order$flow, ncol = 3, byrow = TRUE) 

your_desired_matrix 

     [,1]  [,2]  [,3] 
[1,] 287.5775 788.3051 408.9769 
[2,] 883.0174 940.4673 45.5565 
[3,] 528.1055 892.4190 551.4350 
+0

Oder wenn es eine Menge von Nullwerten eine spärliche Matrix stattdessen ist https://stat.ethz.ch/R -manual/R-devel/Bibliothek/Matrix/html/sparseMatrix.html – zeehio

+0

Mein Ziel ist es, meine od_flows_all_combos Matrix für räumliche Regressionen weiter zu verwenden. OD-Matrizen sind in der Regel dünn besetzte Matrizen, daher muss ich die od_flows_all_combos-Matrix aktualisieren, die die Flüsse zwischen den Gemeinden in meinem Land darstellt. Thx sowieso! – thoscha

+0

okay, habe eine einfache Lösung gefunden: dcast-function macht alles was ich brauche: 'dcast (od_flows, destination ~ origin)' – thoscha