Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
853 views
in Technique[技术] by (71.8m points)

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

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

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

This almost works:

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...