2012-08-23 62 views
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 + Chips數據中減去Chips數據來計算Fish類型的數據。到目前爲止,我想出了以下內容:

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") 

回答

2

我認爲你是更好,但這裏有一個非plyr /重塑的解決方案:

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 
+0

我越來越接近調用這一個...看起來不會有任何其他建議。 – seancarmody

+0

感謝您的基礎R版本。我很高興我似乎沒有錯過一個令人信服的簡單,優雅或簡單的選擇。 – seancarmody