3

我有一个“整洁”格式的数据集,如下所示:

  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中减去数据来计算类型数据。到目前为止,我想出了以下几点:ChipsFish + 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")
4

1 回答 1

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
于 2012-08-23T22:54:52.380 回答