I have a dataframe that I need to reshape to facilitate its use in a viz application. Here's a condensed version of the dataframe:
Carrier <- c("Mesa", "United", "JetBlue", "ExpressJet", "SkyWest")
Flight_Num <- c(7124, 7177, 334, 1223, 6380)
Origin <- c("ORD", "EWR", "SFO", "BOS", "BDL")
Dest <- c("PIT", "BOI", "DSM", "CWA", "CMH")
Sched_Depr <- c(1955, 1900, 1845, 1253, 1755)
df <- data.frame(Carrier, Flight_Num, Origin, Dest, Sched_Depr)
Carrier Flight_Num Origin Dest Sched_Depr
1 Mesa 7124 ORD PIT 1955
2 United 7177 EWR BOI 1900
3 JetBlue 334 SFO DSM 1845
4 ExpressJet 1223 BOS CWA 1253
5 SkyWest 6380 BDL CMH 1755
Origin
and Dept
are interpreted as geographical data (i.e. coordinates) by the viz application. I need to collate them in a single column named Coords
. At the same time I need to create a new order marker variable Order_Points
. So the new, reshaped dataframe would look like this:
Carrier Flight_Num Coords Sched_Depr Order_Points
1 Mesa 7124 ORD 1955 1
2 Mesa 7124 PIT 1955 2
3 United 7177 EWR 1900 1
4 United 7177 BOI 1900 2
5 JetBlue 334 SFO 1845 1
6 JetBlue 334 DSM 1845 2
7 ExpressJet 1223 BOS 1253 1
8 ExpressJet 1223 CWA 1253 2
9 SkyWest 6380 BDL 1755 1
10 SkyWest 6380 CMH 1755 2
What would be an efficient way to collate two columns like this while keeping (and duplicating) the other variables?