【发布时间】:2017-09-05 14:11:39
【问题描述】:
我有一个看起来像这样的数据框(使用 reshape2::cast 和合并):
time days treatment extrafactor1 extrafactor2 extrafactor3 animal1 animal2 animal3
10 83 control B water 2 2 67 40
10 83 control B water 3 50 67 39
10 83 control A water 3 22 80 63
10 83 control A water 2 40 40 100
10 83 treated A water 3 40 69 92
10 83 treated A water 1 64 56 6
10 83 treated A water 2 90 67 52
10 83 treated B water 2 14 36 77
10 83 treated B water 3 41 83 55
10 83 treated B water 1 66 31 51
11 86 control B water 1 99 100 10
11 86 control B water 2 23 27 22
11 86 control A water 3 57 10 65
11 86 control A water 1 60 2 49
11 86 control A water 2 23 14 44
11 86 control B water 3 97 45 20
11 86 treated B water 2 71 15 24
11 86 treated B water 3 49 55 63
11 86 treated A water 3 54 88 27
我想从处理过的样本中减去对照样本中不同动物的值。当然,减法应在其他因素的水平匹配时进行,因此“11_86_treatment_A_water_3”的animal1-value应减去“11_86_control_A_water_3”的animal1-value,这适用于每只动物。我一直在用 plyr 尝试一些事情,比如
df2 <- ddply(df, .(time,days,treatment,extrafactor1,extrafactor2,extrafactor3), transform, animal1 = animal1-animal1[treatment=="control"])
但它给了我很多 NA,我确信缺少信息来充分执行我想要的。实际上有 100 多种动物。
我的尝试是从这里改编的,但是输入变量和要执行的列更少:Easiest way to subtract associated with one factor level from values associated with all other factor levels 在这里:R ddply with multiple variables
也可以等到将表格重新整形为ggplot的长格式,如果这样会让事情变得更容易?
你对我有什么建议吗?
【问题讨论】:
-
嘿!查看stackoverflow.com/questions/5963269/… 以创建可重现的示例。这样更容易提供帮助。
-
你的预期输出是什么