[英]Create a new conditional column based on binary values in other columns? (R)
My data looks like this: 我的数据如下所示:
A B C
1 0 0
0 1 0
0 1 0
0 0 1
This is what I'm going for: 这就是我要的:
A B C New_Column
1 0 0 A
0 1 0 B
0 1 0 B
0 0 1 C
So I'm creating a new column that tells me which of the three variables (A, B, or C) is present. 因此,我正在创建一个新列,该列告诉我三个变量(A,B或C)中的哪个存在。 Only one of the three columns will contain a 1 per row.
三列中只有一列每行包含1。 What is the best way to go about this?
最好的方法是什么?
我们可以使用max.col
df1$New_Column <- names(df1)[max.col(df1, "first")]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.