如何使用reshape2库按组转置数据框?

时间:2017-09-24 23:05:12

标签: r reshape2

问题

我有3辆汽车(分组变量),其中随时间的速度记录在以下数据集中。我想按组转置这个数据框。

数据

foo <- structure(list(file.ID2 = c("Cars_03", "Cars_03", "Cars_03", 
"Cars_03", "Cars_03", "Cars_03", "Cars_03", "Cars_03", "Cars_04", 
"Cars_04", "Cars_04", "Cars_04", "Cars_04", "Cars_04", "Cars_04", 
"Cars_04", "Cars_05", "Cars_05", "Cars_05", "Cars_05", "Cars_05", 
"Cars_05", "Cars_05", "Cars_05"), speed.kph.ED = c(129.3802848, 
129.4022304, 129.424176, 129.4461216, 129.4680672, 129.47904, 
129.5009856, 129.5229312, 127.8770112, 127.8221472, 127.7672832, 
127.7124192, 127.6575552, 127.6026912, 127.5478272, 127.4929632, 
134.1095616, 134.1205344, 134.1315072, 134.1534528, 134.1644256, 
134.1753984, 134.1863712, 134.197344)), row.names = c(NA, -24L
), class = c("tbl_df", "tbl", "data.frame"), .Names = c("file.ID2", 
"speed.kph.ED"))   

期望输出

> foo
              V2       V3       V4       V5       V6       V7       V8       V9
Cars_03 129.3803 129.4022 129.4242 129.4461 129.4681 129.4790 129.5010 129.5229
Cars_04 127.8770 127.8221 127.7673 127.7124 127.6576 127.6027 127.5478 127.4930
Cars_05 134.1096 134.1205 134.1315 134.1535 134.1644 134.1754 134.1864 134.1973

我尝试了什么

经过大量搜索,我发现reshape2库可能很有用。我发现这question似乎是相关的。因此,我尝试使用melt()dcast()函数,如下所示:

library(reshape2)
dcast(melt(foo,id.vars = 1),
      file.ID2 ~ variable, NULL)
Aggregation function missing: defaulting to length
  file.ID2 speed.kph.ED
1  Cars_03            8
2  Cars_04            8
3  Cars_05            8   

但这只是聚合了值(计数/总和)。请帮我获得所需的输出。感谢。

1 个答案:

答案 0 :(得分:2)

为了重塑数据,我想使用tidyr库的spread函数。

首先,添加id列。有十亿种不同的方法(我喜欢使用dplyr包):

library(dplyr)
foo = foo %>% group_by(file.ID2) %>% mutate(id = paste0("V",row_number()))

以下是将tidyr库用于spread数据的方法:

library(tidyr)
answer = spread(data = foo, key = id, value = speed.kph.ED)