return 当字符串出现在 data.frame 的任何列中时为 TRUE

return TRUE when character string present in any column of data.frame

非常简单的问题,但我在 Whosebug 的任何地方都看不到这个确切的问题(而且我自己也无法解决!)。如果数据帧的任何行中存在字符串,我想 return TRUE(如果不是,则为 FALSE!)。例如:

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)

这很有趣,因为我一小时前创建了完全相同的主题。

答案是

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")))][]

你可以这样做:

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