2016-06-24 53 views
1

我有一個數據幀DF,看起來像以下:整形數據部分地從廣角至長中的R

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 

我想使輸出如下所示重新組織它:

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 

所以我一直在玩與周圍的reshape2包的希望,這是我需要以下這篇文章的內容: http://www.cookbook-r.com/Manipulating_data/Converting_data_between_wide_and_long_format/

因爲數據似乎在一些寬格式的迭代中顯示。也許我錯了,因爲我仍然在學習[R但這裏是我的嘗試:

  df2 = melt(df, 
      id.vars=c("id", "pickuptime", "pickupx", "pickupy"), 
      measure.vars=c("dropofftime", "dropoffy", "dropoffx"), 
        variable.name="x", 
        value.name="y") 

不知道在哪裏可以從這裏走,或者我甚至在正確的軌道,因爲我還沒有發現其他的例子在網上。感謝幫助!

回答

2

我不知道這是否可以使用融化,但你可以將它們分開,然後堆疊它們。事情是這樣的:

library(reshape2) 


df_pickup <- df[,c("id",names(df)[grepl("pick",names(df))])] 
names(df_pickup)<- c("id","time","x","y") 
df_dropoff <- df[,c("id",names(df)[grepl("drop",names(df))])] 
names(df_dropoff)<- c("id","time","x","y") 

df2 <- rbind(cbind(df_pickup,pickup_dropoff="pickup"),cbind(df_dropoff,pickup_dropoff="dropoff")) 
df2 

    id    time  x y pickup_dropoff 
1 1  2/1/2013 12:23 73.00 40   pickup 
2 1  1/1/2013 12:45 73.60 41   pickup 
3 2  1/2/2013 13:00 73.45 42   pickup 
4 2  1/2/2013 14:50 73.11 41   pickup 
5 2  1/2/2013 16:00 73.10 41   pickup 
6 1  2/1/2013 12:34 73.00 41  dropoff 
7 1  1/1/2013 12:57 73.50 41  dropoff 
8 2  1/2/2013 14:00 73.00 42  dropoff 
9 2  1/2/2013 15:30 73.00 44  dropoff 
10 2  1/2/2013 18:00 74.00 42  dropoff 

數據

df<-structure(list(id = c(1L, 1L, 2L, 2L, 2L), pickuptime = structure(c(5L, 
1L, 2L, 3L, 4L), .Label = c(" 1/1/2013 12:45", " 1/2/2013 13:00", 
" 1/2/2013 14:50", " 1/2/2013 16:00", " 2/1/2013 12:23"), class = "factor"), 
    pickupx = c(73, 73.6, 73.45, 73.11, 73.1), pickupy = c(40L, 
    41L, 42L, 41L, 41L), dropofftime = structure(c(5L, 1L, 2L, 
    3L, 4L), .Label = c(" 1/1/2013 12:57", " 1/2/2013 14:00", 
    " 1/2/2013 15:30", " 1/2/2013 18:00", " 2/1/2013 12:34" 
    ), class = "factor"), dropx = c(73, 73.5, 73, 73, 74), dropy = c(41L, 
    41L, 42L, 44L, 42L), X = c(NA, NA, NA, NA, NA)), .Names = c("id", 
"pickuptime", "pickupx", "pickupy", "dropofftime", "dropx", "dropy", 
"X"), class = "data.frame", row.names = c(NA, -5L)) 
+0

這是真棒,謝謝! – LoF10