我的数据框如下所示:
var1 var2 var3
1 2 5 "other"
2 25 3 "sample"
3 4 5 "baseline_other"
4 60 5 "baseline_sample"
5 40 5 "other"
6 60 5 "other"
7 25 3 "sample"
8 6 8 "other"
9 60 7 "other"
10 4 3 "other"
我想在df$var1
的情况下添加一个新列,我在其中使用行df$var3==baseline_other
的{{1}}值进行计算。
如果df$var3 == "other"
它应该使用行df$var3 == "sample"
的{{1}}的值。 (我希望我能让自己清楚)。所以我想在df$var1
行中df$var3==baseline_sample
给出x_other
的值。 (使用df$var1
的{{1}}也是如此。
我尝试baseline_other
,但收到错误x_sample
。
当我能够完成这项工作时,我还想添加一个带有计算的列(如上所述),如下所示:
baseline_sample
并为"示例"重复此操作。这样:
x_other <- df[df$var3=="baseline_other",df$var1]
我希望自己足够清楚。但我的问题是实际按行名和列选择数据框中的某个值,然后在计算中使用该值。
我的新df应如下所示:
undefined columns selected