Home > Enterprise >  Add extra columns based on two conditions, with the possibility for NA values
Add extra columns based on two conditions, with the possibility for NA values

Time:06-28

I have a dataset that kind of look like this:

NAME         JOB TITLE           ROLE DESCRIPTION
Mark         Cyber specialist    N/A
Peter        Manager             Manager with focus on cyber
Steven       CFO                 N/A
Lucas        VP                  General manager technology safety

Now I want to create an extra column which indicates if the job of the person is cyber related. But there have to be a 1 (cyber related) at Mark, Peter and Lucas. So, there should be looked at 2 columns instead of one. If the condition is only based on 1 column I know that the code should look like this:

pattern <- paste(c("cyber", "Cyber", "technology", "Technology", "computer", "Computer"), collapse = "|")
df <- df %>% 
  mutate(`Cyber Job` = ifelse(str_detect('column', pattern), 1, 0))

But i don't know how the code should look like if the condition is based on two columns

CodePudding user response:

You can use the following code which checks whether there is any string that matches the pattern using grepl, across the columns:

library(dplyr)
df %>% 
  rowwise() %>%
  mutate(`Cyber Job` = any(grepl(pattern,across(everything())))*1)

Output:

# A tibble: 4 × 4
# Rowwise: 
  NAME   JOB.TITLE        ROLE.DESCRIPTION                  `Cyber Job`
  <chr>  <chr>            <chr>                                   <dbl>
1 Mark   Cyber specialist N/A                                         1
2 Peter  Manager          Manager with focus on cyber                 1
3 Steven CFO              N/A                                         0
4 Lucas  VP               General manager technology safety           1

Data

df <- data.frame(NAME = c("Mark", "Peter", "Steven", "Lucas"),
                 `JOB TITLE` = c("Cyber specialist", "Manager", "CFO", "VP"),
                 `ROLE DESCRIPTION` = c("N/A", "Manager with focus on cyber", "N/A", "General manager technology safety"))

CodePudding user response:

We may also use if_any

library(dplyr)
library(stringr)
df %>%
  mutate(`Cyber Job` =  (if_any(everything(), str_detect, pattern = pattern)))
    NAME        JOB.TITLE                  ROLE.DESCRIPTION Cyber Job
1   Mark Cyber specialist                               N/A         1
2  Peter          Manager       Manager with focus on cyber         1
3 Steven              CFO                               N/A         0
4  Lucas               VP General manager technology safety         1
  •  Tags:  
  • r
  • Related