[英]Is there a way in R to create a new column, assigning values conditional on other columns?
[英]Create a new conditional column based on binary values in other columns? (R)
我的数据如下所示:
A B C
1 0 0
0 1 0
0 1 0
0 0 1
这就是我要的:
A B C New_Column
1 0 0 A
0 1 0 B
0 1 0 B
0 0 1 C
因此,我正在创建一个新列,该列告诉我三个变量(A,B或C)中的哪个存在。 三列中只有一列每行包含1。 最好的方法是什么?
我们可以使用max.col
df1$New_Column <- names(df1)[max.col(df1, "first")]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.