1

我目前正在尝试做的是将所有列分散到多个列上,例如这个数据框

Person_ID  Car_ID Car_Type Car_Speed
a          1      x        50
a          2      y        70
a          3      z        100
b          4      y        70

我想把它变成这个

Person_ID  Car_ID1 Car_Type1 Car_Speed1 Car_ID2 Car_Type2 Car_Speed2 Car_ID3 Car_Type3 Car_Speed3
a          1        x        50         2      y        70           3       z         100    
b          4        y        70

有人可以帮忙吗?谢谢你。

4

1 回答 1

2

这可以很容易地完成dcastfrom data.tablewhich can take multiple value.varcolumns

library(data.table)#v1.9.7+
dcast(setDT(df1), Person_ID~rowid(Person_ID), 
              value.var = c("Car_ID", "Car_Type", "Car_Speed"))
#     Person_ID Car_ID_1 Car_ID_2 Car_ID_3 Car_Type_1 Car_Type_2 Car_Type_3 Car_Speed_1 Car_Speed_2
#1:         a        1        2        3          x          y          z          50          70
#2:         b        4       NA       NA          y         NA         NA          70          NA
#    Car_Speed_3
#1:         100
#2:          NA

或者在创建按“Person_ID”分组的序列列之后使用reshapefrombase R

df2 <- transform(df1, Seq = ave(seq_along(Person_ID), Person_ID, FUN = seq_along))
reshape(df2, idvar = "Person_ID", timevar = "Seq", direction = "wide")
于 2016-06-15T10:06:01.313 回答