0

Really simple problem but I can't see this exact problem anywhere in Stackoverflow (and I can't work it out myself!). I want to return TRUE if a character string is present in any row of a dataframe (FALSE if not!). EG:

col1 <- c("no","no","no","no","pair")
col2 <- c("no","no","pair","no","no")
col3 <- c("chicks","no","pair","no","no")
df<- cbind.data.frame(col1,col2,col3)
df

  col1 col2   col3
1   no   no chicks
2   no   no     no
3   no pair   pair
4   no   no     no
5 pair   no     no

#### I want col4 to be TRUE FALSE FALSE FALSE FALSE - return only rows with chicks
# I tried like col4<-lapply("chicks", grepl, x = df) but this runs on each column not each row

####I want col5 to be TRUE FALSE TRUE FALSE TRUE - any row with pair or chicks
# eg col5<-lapply("chicks"|"pair", grepl, x = df)
davidj444
  • 115
  • 5

2 Answers2

1

It's funny because I created the exact same topic just one hour ago.

Check if a value among a list exist in multiple column R data.table

Answer was

library(data.table)
df <- as.data.table(df)

df[, col4 := apply(df[,c("col1", "col2", "col3")],1, function(x) any(x %in% c("chicks")))][]

df[, col5 := apply(df[,c("col1", "col2", "col3")],1, function(x) any(x %in% c("pair", "chicks")))][]
1

You can do:

library(tidyverse)
df %>%
  rowwise() %>%
  mutate(col4 = any(str_detect(c_across(c(col1, col2, col3)), 'chicks')),
         col5 = any(str_detect(c_across(c(col1, col2, col3)), 'chicks|pair'))) %>%
  ungroup()

# A tibble: 5 x 5
  col1  col2  col3   col4  col5 
  <chr> <chr> <chr>  <lgl> <lgl>
1 no    no    chicks TRUE  TRUE 
2 no    no    no     FALSE FALSE
3 no    pair  pair   FALSE TRUE 
4 no    no    no     FALSE FALSE
5 pair  no    no     FALSE TRUE 
deschen
  • 10,012
  • 3
  • 27
  • 50