简体   繁体   English

如何根据多个现有列中的数据对新列使用 mutate

[英]How to use mutate for a new column based the data in multiple existing columns

Good Morning,早上好,

this is my data set containing data of different client's races.这是我的数据集,包含不同客户种族的数据。

White  Asian  Black  Native  Islander  Other
1       0       0      0       0         0
0       1       0      0       0         0
0       0       0      1       0         0
0       0       1      0       0         0
1       0       0      0       1         0
0       0       0      0       0         1

The data is stored with a Boolean where 0 = No and 1 = Yes数据以布尔值存储,其中 0 = 否,1 = 是

So if a client has 1 for the column white, then they are white.因此,如果客户的列白色为 1,则它们是白色的。

But if a client has a 1 for white and islander then they are multi racial.但是,如果客户对白人和岛民的评分为 1,那么他们就是多种族。

So this would be my desired output所以这将是我想要的输出

White  Asian  Black  Native  Islander  Other   Race
1       0       0      0       0         0     White
0       1       0      0       0         0     Asian
0       0       0      1       0         0     Native
0       0       1      0       0         0     Black
1       0       0      0       1         0     Multi-Racial 
0       0       0      0       0         1     Other

I'm familiar with mutate() but I've only used mutate based off one column.我熟悉 mutate() 但我只使用了基于一列的 mutate。

Can anyone provide a code that can help with my desired output?任何人都可以提供可以帮助我获得所需输出的代码吗?

Using ifelse() with max.col() should get you what you want.使用ifelse()max.col()应该可以得到你想要的。 For rows that only have one value you index the name the value was in, otherwise it is "Multi-Racial"对于只有一个值的行,您索引该值所在的名称,否则为"Multi-Racial"

df1$Race <- ifelse(rowSums(df1) == 1, names(df1)[max.col(df1)], "Multi-Racial")
df1
  White Asian Black Native Islander Other         Race
1     1     0     0      0        0     0        White
2     0     1     0      0        0     0        Asian
3     0     0     0      1        0     0       Native
4     0     0     1      0        0     0        Black
5     1     0     0      0        1     0 Multi-Racial
6     0     0     0      0        0     1        Other

Or, using mutate() :或者,使用mutate()

df1 %>%
  mutate(Race = ifelse(rowSums(.) == 1, names(.)[max.col(.)], "Multi-Racial"))

Data :数据

df1 <- read.table(header = T, text = "White  Asian  Black  Native  Islander  Other
1       0       0      0       0         0
0       1       0      0       0         0
0       0       0      1       0         0
0       0       1      0       0         0
1       0       0      0       1         0
0       0       0      0       0         1")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 purrr 中的 map 和 dplyr::mutate 根据列对创建多个新列 - How to use map from purrr with dplyr::mutate to create multiple new columns based on column pairs 根据现有列的条件派生或更改新列 - Derive or Mutate a new column based on conditions on existing columns 基于现有列的新列/变异 - New column / mutate based on existing column dplyr:根据变量字符串选择的多个列来更改新列 - dplyr: mutate new column based on multiple columns selected by variable string 使用dplyr mutate根据列名向量创建新列 - use dplyr mutate to create new columns based on a vector of column names 在R; 如何将str_extract与mutate一起使用,以基于现有列向dataFrame添加新的“标志”列(T / F) - In R; How to use str_extract with mutate to add a new “flag” column (T/F) to a dataFrame based on an existing column 根据对自身的引用改变多个新列 - Mutate multiple new columns based on references to themselves 如何使用mutate仅根据数据框其他行的子集创建新列? - How can I use mutate to create a new column based only on a subset of other rows of a data frame? 根据 R 中的多个条件对新列进行变异 - Mutate a new column based on multiple conditions in R 使用mutate_at重新编码多个列值,并基于管道中的变异列创建新列 - Recode multiple column values using mutate_at and creating a new column based on mutated columns in a pipe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM