简体   繁体   English

如何使用 mutate function 从一组具有相似名称的现有列中添加新列

[英]How to add a new column using mutate function from a group of existing columns with similar names

I would like to add a column to my data frame based upon the values in other columns.我想根据其他列中的值向我的数据框中添加一列。

Here is an extract of the data.这是数据的摘录。

1

On each row if any of the 4 TOPER columns have any of the following values (92514, 92515, 92508, 92510, 92511 or 92512( i want the S_Flag column to be equal to 1, If not the S_Flag value should be 0. Have highlighted the data where this true (case nos 2, 4, 6 and 8) - therefore S_Flag should be made 1. Have tried using a ifelse inside a mutate function. Just not sure how to identify looking across all 4 TOPER columns within the ifelse function??? Have tried在每一行上,如果 4 个 TOPER 列中的任何一个具有以下任何值(92514、92515、92508、92510、92511 或 92512(我希望S_Flag列等于 1,如果不是,则S_Flag值应该为 0。有突出显示正确的数据(第 2、4、6 和 8 号案例)-因此应制作S_Flag 1。已尝试在mutate function 中使用ifelse 。只是不确定如何识别查看 ifelse 中的所有 4 个ifelse列function??? 试过

tt <- mutate(rr, S_Flag = ifelse( any(vars(everything()) %in% toper_vec), 1,0))

where rr is the original data frame and toper_vec is a vector containing the 6 TOPER column values.其中rr是原始数据框, toper_vec是包含 6 个 TOPER 列值的向量。

Hope that makes sense.希望这是有道理的。 By the way i am in early stages of learning R.顺便说一句,我正处于学习 R 的早期阶段。 Thank you for any assistance.感谢您提供任何帮助。

A couple of quick fixes should make your code work: (1) use rowwise() and (2) use across().几个快速修复应该使您的代码工作:(1)使用 rowwise() 和 (2) 使用 cross()。

The revised code reads:修改后的代码如下:

tt <- rr %>%
  rowwise() %>%
  mutate(S_Flag = if_else( any(across(everything()) %in% toper_vec), 1,0))

A similar question was addressed in the following informative post: Check row wise if value is present in column and update new column row wise以下内容丰富的帖子中解决了类似的问题: Check row wise if value is present in column and update new column row wise

Applying the suggested approach in that post to your immediate question, the following should work:将该帖子中建议的方法应用于您的直接问题,以下应该有效:

library(tidyverse)

toper_vec <- c(92514, 92515, 92508, 92510, 92511, 92512)

df <- data.frame("CASE" = c(1, 2, 3, 4, 5),
                 "TOPER1" = c(86509, 92514, 87659, 45232, 86509),
                 "TOPER2" = c(12341, 10094, 12341, 92508, 10094),
                 "TOPER3" = c(86509, 67326, 41908, 50567, 50567))


new_df <- df %>%
  rowwise() %>%                                   
  mutate(S_Flag = case_when(TOPER1 %in% toper_vec ~ 1,    
                       TOPER2 %in% toper_vec ~ 1,
                       TOPER3 %in% toper_vec ~ 1, 
                       TRUE               ~ 0))

Here's an alternative, reusing toper_vec and df from Blue050205:这是一个替代方案,重用toper_vec中的 toper_vec 和df

df  %>%
  rowwise() %>%
  mutate(s_flag = if_else(any(c_across(starts_with("TOP")) %in% toper_vec), 1, 0))

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

相关问题 使用 mutate 添加具有自定义 function 的新列 - Add new columns with custom function using mutate 如何根据多个现有列中的数据对新列使用 mutate - How to use mutate for a new column based the data in multiple existing columns 我想通过使用来自多个列的数据变异 function 在 rstudio 中创建一个新列 - I want to create a new column in rstudio through mutate function using data from multiple columns 使用粘贴功能从现有列名创建新列名 - Creating new column names from existing column names using paste function 如何使用 dplyr 变异从将一列输入到返回列表的 function 中创建新列? - How to use dplyr mutate to create new columns from inputting one column into a function that returns a list? 如何使用mutate根据现有列中的状态缩写添加带有状态名称的列? - How to use mutate to add column with state names based on the state abbreviation in an existing column? 如何在变异 function 中获取列名 - how to get column names in mutate function R dataframe 使用cross / all_of / mutate_if从现有列创建多个新列 - R dataframe create mulitple new columns from existing columns using across / all_of / mutate_if 使用dplyr mutate根据列名向量创建新列 - use dplyr mutate to create new columns based on a vector of column names dplyr 根据现有列的值改变新列 - dplyr mutate new column according to the value of existing columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM