本文介绍了从与所有其他因子水平相关联的值中减去与一个因子水平相关联的最简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,其中包含实时"治疗率和杀死"治疗率.我想从活着的治疗中减去被杀死的治疗:

I've got a dataframe containing rates for 'live' treatments and rates for 'killed' treatments. I'd like to subtract the killed treatments from the live ones:

df <- data.frame(id1=gl(2, 3, labels=c("a", "b")),
                 id2=rep(gl(3, 1, labels=c("live1", "live2", "killed")), 2), 
                 y=c(10, 10, 1, 12, 12, 2),
                 otherFactor = gl(3, 2))

我想从y的所有其他值中减去id2=="killed"y值,并用id1的级别分开,同时保留otherFactor.我最终会得到

I'd like to subtract the values of y for which id2=="killed" from all the other values of y, separated by the levels of id1, while preserving otherFactor. I would end up with

id1    id2   y otherFactor
  a  live1   9           1
  a  live2   9           1
  b  live1  10           2
  b  live2  10           3

这几乎可行:

df_minusKill <- ddply(df, .(id1), function(x) x$y[x$id2!="killed"] - x$y[x$id2=="killed"])
names(df_minusKill) <- c("id1", "live1", "live2")
df_minusKill_melt <- melt(df_minusKill, measure.var=c("live1", "live2"))

,但是会丢失otherFactor的值.也许我可以使用mergeotherFactor的值放回去,但是实际上我有大约12个"otherFactor"列,因此自动将它们保持在其中就不那么麻烦了.

except that you lose the values of otherFactor. Maybe I could use merge to put the values of otherFactor back in, but in reality I have about a dozen "otherFactor" columns, so it would be less cumbersome to just keep them in there automatically.

推荐答案

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

这篇关于从与所有其他因子水平相关联的值中减去与一个因子水平相关联的最简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 07:40