我有一个“整洁”格式的数据集,如下所示:
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")
答案 0 :(得分:2)
我认为你的更好,但这是一个非plyr / reshape解决方案:
mydataspl <- split(mydata, mydata$type)
subs <- merge(mydataspl$"Fish + Chips", mydataspl$Chips, by= 1)
data.frame(subs[,"group", drop=FALSE], type="Fish",
score=with(subs, score.x-score.y),
price=with(subs, price.x-price.y)
)
group type score price
1 A Fish 0 8
2 B Fish 7 13
3 C Fish 0 6