简体   繁体   English

根据其他列中的二进制值创建新的条件列? (右)

[英]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.

相关问题 R 中有没有办法创建一个新列,以其他列为条件分配值? - Is there a way in R to create a new column, assigning values conditional on other columns? 以 R 数据框中的其他列为条件创建新的二进制列 - Create new binary column conditional on other column in R data frame 根据其他2列中的值创建新列 - Create new column based on values in 2 other columns 根据条件语句从其他列填充R中的新列 - Populate new column in R from other columns based on conditional statement 在 R 中,如何根据其他列的列标题的值创建新列 - In R, how to create a new column from the column headings of other columns based on their values R - 基于其他列中其他行的值的新列 - R - New column based on values from other rows in other columns 如何使用R根据其他列的值创建新列 - How to create new columns based on other columns' values using R R根据其他两列中的值在新列中打印值 - R print values in new column based on the values in two other columns 在 R 中创建一个用户生成的函数,它根据其他列的值创建一个新的日期列 - Create a user generated function in R which creates a new column of dates based on values from other columns 根据 R 中数据框中所有其他列中的字符串值,使用 dplyr 创建一个新列 - Create a new column using dplyr based on string values in all other columns in a data frame in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM