我有一个“整洁”格式的数据集,如下所示:
group type score price
1 A Fish + Chips 9 8
2 B Fish + Chips 7 20
3 C Fish + Chips 8 22
4 A Chips 9 0
5 B Chips 0 7
6 C Chips 8 16
7 A Snags 5 19
8 B Snags 9 8
9 C Snags 10 6
我想添加一些派生数据,如果将数据转换为宽格式,将使用列算术(加法、减法等)确定。我一直在尝试解决如何在不再次铸造和熔化的情况下做到这一点。在这里的简单示例中,我想通过从相应数据Fish
中减去数据来计算类型数据。到目前为止,我想出了以下几点:Chips
Fish + Chips
ddply(subset(mydata, type %in% c("Chips", "Fish + Chips")),
.(group), summarise, type="Fish",
score=score[type=="Fish + Chips"] - score[type=="Chips"],
price=price[type=="Fish + Chips"] - price[type=="Chips"])
这使
group type score price
1 A Fish 0 8
2 B Fish 7 13
3 C Fish 0 6
然后我可以rbind
得到原始数据。任何更好的方法的建议将不胜感激(即使那是铸造和融化)。
这是示例数据:
structure(list(group = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L,
2L, 3L), .Label = c("A", "B", "C"), class = "factor"), type = structure(c(2L,
2L, 2L, 1L, 1L, 1L, 3L, 3L, 3L), .Label = c("Chips", "Fish + Chips",
"Snags"), class = "factor"), score = c(9, 7, 8, 9, 0, 8, 5, 9,
10), price = c(8, 20, 22, 0, 7, 16, 19, 8, 6)), .Names = c("group",
"type", "score", "price"), row.names = c(NA, -9L), class = "data.frame")