-1

Input Format

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"

Expected outpout format

First column from input datframe as rownames , second column as column names, third column as values filled up

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")
Eswar
  • 309
  • 1
  • 3
  • 10
  • The first data.frame above is poorly formed, but `tidyr::spread(df1, TO_STOP_ID, TRAVEL_TIME)` or any other long to wide variant. – alistaire Oct 11 '16 at 06:17

1 Answers1

1

We can do this with xtabs from base R

xtabs(TRAVEL_TIME~FROM_STOP_ID + TO_STOP_ID, df1N)
akrun
  • 874,273
  • 37
  • 540
  • 662