简体   繁体   中英

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

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.

Hope that makes sense. By the way i am in early stages of learning R. Thank you for any assistance.

A couple of quick fixes should make your code work: (1) use rowwise() and (2) use across().

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

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM