2016-10-11 67 views
-1

輸入格式鑄造一個數據幀

structure(list(FROM_STOP_ID = c("DEP10001", "DEP10001", "DEP10001", 
          "DEP10001", "DEP10001", "DEP10001", 
      "DEP10001", "DEP10001", "DEP10001", 
          "DEP10001"), 
      TO_STOP_ID = c("DEP10001", "DEP10022", 
      "DEP25005", "DEP35024", "DEP45024", "DEP50002", 
      "STP62369", "STP62829", "STP63844", "STP63889"), 
      TRAVEL_TIME = c(0, 518, 497, 131, 0, 131, 483, 137, 
          489, 504)), .Names = c("FROM_STOP_ID",  "TO_STOP_ID", "TRAVEL_TIME" 

預計outpout格式

從輸入datframe爲rownames,第二列的列名,第三列的值一欄填寫了

structure(list(DEP10001 = 0, DEP10022 = 518, DEP25005 = 497, 
DEP35024 = 131, DEP45024 = 0), .Names = c("DEP10001", "DEP10022", 
"DEP25005", "DEP35024", "DEP45024"), row.names = "DEP10001", class = "data.frame") 
+0

上面的第一個data.frame格式不好,但是'tidyr :: spread(df1,TO_STOP_ID,TRAVEL_TIME)'或者其他任何long到wide的變體。 – alistaire

回答

0

我們可以用xtabsbase R

xtabs(TRAVEL_TIME~FROM_STOP_ID + TO_STOP_ID, df1N) 
+0

as.data.frame(acast(travel_time,FROM_STOP_ID_TO_STOP_ID,value.var =「TRAVEL_TIME」)) – Eswar

+0

這對我更好。謝謝 – Eswar

+0

@Eswar你必須加載一個庫。 – akrun