Easiest way to subtract associated with one factor level from values associated with all other factor levels

假如想象 提交于 2019-11-29 12:47:30
df2 <- ddply(df, .(id1), transform, y = y-y[id2=="killed"])
df2[-which(df2$id2=="killed"),]
  id1   id2  y otherFactor
1   a live1  9           1
2   a live2  9           1
4   b live1 10           2
5   b live2 10           3

The by function can process sections of dataframe separately by factors (or you could use lapply(split(df , ...)):

>  by(df, df$id1, FUN= function(x) x[['y']]-x[ x$id2=="killed", "y"] )
df$id1: a
[1] 9 9 0
--------------------------------------------------------------------------- 
df$id1: b
[1] 10 10  0
> unlist( by(df, df$id1, FUN= function(x) x[['y']]-x[ x$id2=="killed", "y"] ) )
a1 a2 a3 b1 b2 b3 
 9  9  0 10 10  0 

You could assign this to a column in df and subset out the rows with id2 not equal to 'killing'.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!