2012-07-25 57 views
0

數據我有一個數據幀,看起來像投,加入和減少重塑

MAP name  series ID 
1.0 aspartame baseline 902349 
1.0 aspartame baseline 82749 
1.0 aspartame baseline 928542 
1.0 aspartame next  928542 
0.8 aspartame next  82749 
0.8 aspartame next  902349 

而且我想加入的ID這些數據,並計算基準,並在接下來的MAP之間的差異 得分了。所以這應該給

diff name  ID 
0.2 aspartame 902349 
0.2 aspartame 82749 
0.0 aspartame 928542 

回答

2

按照要求通過OP :使用reshape(或更確切地說reshape2)的解決方案。

d <- read.table(text = " 
MAP name  series ID 
1.0 aspartame baseline 902349 
1.0 aspartame baseline 82749 
1.0 aspartame baseline 928542 
1.0 aspartame next  928542 
0.8 aspartame next  82749 
0.8 aspartame next  902349", header = TRUE) 

require(reshape2) 

dcast(data = d, formula = ID + name~ ., value.var = "MAP", 
     fun.aggregate = function(x) (x[1] - x[2])) 

 ID  name NA 
1 82749 aspartame 0.2 
2 902349 aspartame 0.2 
3 928542 aspartame 0.0 
1

這裏是基礎R的方式(假設你的data.frame名爲df):

aggregate(list(MAP = df$MAP), 
      by=list(ID = df$ID, name = df$name), 
      function(x) x[1] - x[2]) 
#  ID  name MAP 
# 1 82749 aspartame 0.2 
# 2 902349 aspartame 0.2 
# 3 928542 aspartame 0.0 
0

你可以使用plyr:

library(plyr) 
ddply(your_data, c("ID", "name"), 
     function(df){subset(df, series == "baseline", select = "MAP")- 
     subset(df, series == "next", select = "MAP")})