0

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.

alistaire
  • 42,459
  • 4
  • 77
  • 117
  • 1
    Please provide a reproducible example along with expected output. Read about [how to give a reproducible example](http://stackoverflow.com/questions/5963269). – Ronak Shah Dec 03 '20 at 02:29

2 Answers2

1

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))
Blue050205
  • 276
  • 2
  • 4
1

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))
meriops
  • 997
  • 7
  • 6
  • Thanks so much. Really helpful as I didn't know you could work in rows instead of the usual column formation. Really appreciate your help. Very kind. – Dapdot44 Dec 04 '20 at 04:11