【发布时间】:2020-10-23 01:42:18
【问题描述】:
我正在寻找一种方法来重塑我的数据:
> test
policyID startYear product
1: G000246-000 2014 Product 1
2: G000246-000 2014 Product 2
3: G000246-000 2014 Product 3
4: G000246-000 2015 Product 1
5: G000246-000 2015 Product 2
6: G000246-000 2015 Product 3
到这里:
policyID 2014 2015
1: G000246-000 Product 1 Product 1
2: G000246-000 Product 2 Product 2
3: G000246-000 Product 3 Product 3
我试过了:
reshape(test, idvar = "policyID", timevar = "startYear", direction = "wide")
但我明白了:
policyID product.2014 product.2015
1: G000246-000 Product 1 Product 1
达到我想要的结果的最佳方法是什么?
数据:
structure(list(policyID = c("G000246-000", "G000246-000", "G000246-000",
"G000246-000", "G000246-000", "G000246-000"), startYear = c(2014,
2014, 2014, 2015, 2015, 2015), product = c("Product 1", "Product 2",
"Product 3", "Product 1", "Product 2", "Product 3")), row.names = c(NA,
-6L), class = c("data.table", "data.frame"))
【问题讨论】:
-
既然您使用
data.table,请尝试dcast(test, policyID + rowid(startYear) ~ startYear)。在您的数据中,您缺少一个唯一标识符。 -
@markus startyear 被修改为别的东西
标签: r dplyr data.table reshape tidyr