I have a data frame df that looks like the following:
id pickuptime pickupx pickupy dropofftime dropx dropy
1 2/1/2013 12:23 73 40 2/1/2013 12:34 73 41
1 1/1/2013 12:45 73.6 41 1/1/2013 12:57 73.5 41
2 1/2/2013 13:00 73.45 42 1/2/2013 14:00 73 42
2 1/2/2013 14:50 73.11 41 1/2/2013 15:30 73 44
2 1/2/2013 16:00 73.1 41 1/2/2013 18:00 74 42
I want to reorganize it so that the output looks like the following:
id time x y pickup_dropoff
1 2/1/2013 12:23 73 40 pickup
1 2/1/2013 12:34 73 41 dropoff
1 1/1/2013 12:45 73.6 41 pickup
1 1/1/2013 12:57 73.5 41 dropoff
2 1/2/2013 13:00 73.45 42 pickup
2 1/2/2013 14:00 73 42 dropoff
2 1/2/2013 14:50 73.11 41 pickup
2 1/2/2013 15:30 73 44 dropoff
2 1/2/2013 16:00 73.1 41 pickup
2 1/2/2013 18:00 74 42 dropoff
So I've been playing around with the reshape2 package in hopes that this is what I need following this post: http://www.cookbook-r.com/Manipulating_data/Converting_data_between_wide_and_long_format/
since the data seems to be displayed in some iteration of wide format. Perhaps I'm mistaken, as I am still learning R but here is my attempt:
df2 = melt(df,
id.vars=c("id", "pickuptime", "pickupx", "pickupy"),
measure.vars=c("dropofftime", "dropoffy", "dropoffx" ),
variable.name="x",
value.name="y")
Not sure where to go from here or if I'm even on the right track as I haven't found other examples on the web. Appreciate the help!