Скажем, у меня есть следующий data.frame df: # col1 col2 col3 othercol1 othercol11 # 1 Hello WHAT_hello2 Hello 10 3 # 2 WHAT_hello WHAT_hello WHAT_hello ...

1
Dunois 20 Янв 2021 в 17:23

2 ответа

Лучший ответ

Для функций, применяемых к строкам, а не столбцам, вы можете использовать c_across с rowwise:

df %>% 
  rowwise() %>% 
  filter(any(str_detect(c_across(matches('^col')), '^WHAT')))

# # A tibble: 2 x 5
# # Rowwise: 
#   col1       col2        col3       othercol1 othercol11
#   <chr>      <chr>       <chr>          <int>      <int>
# 1 Hello      WHAT_hello2 Hello              9          7
# 2 WHAT_hello WHAT_hello  WHAT_hello         3         10

Или, используя across с rowSums:

row_lgl <- 
  df %>% 
    transmute(across(.cols = matches("^col"), .fns = ~ str_detect(.x, "^WHAT"))) %>% 
    rowSums %>% 
    '>'(0)
           
df %>% 
  filter(row_lgl)
#         col1        col2       col3 othercol1 othercol11
# 1      Hello WHAT_hello2      Hello         9          7
# 2 WHAT_hello  WHAT_hello WHAT_hello         3         10
3
IceCreamToucan 20 Янв 2021 в 14:46

Используя base

df <- data.frame(col1 = c("Hello", "WHAT_hello", "Hello"), 
                 col2 = c("WHAT_hello2", "WHAT_hello", "Hello"), 
                 col3 = c("Hello", "WHAT_hello", "Hello"),
                 othercol1 = sample(1:10, 3), 
                 othercol11 = sample(1:10, 3), 
                 stringsAsFactors = FALSE)

df 
#>         col1        col2       col3 othercol1 othercol11
#> 1      Hello WHAT_hello2      Hello         1          9
#> 2 WHAT_hello  WHAT_hello WHAT_hello         3          2
#> 3      Hello       Hello      Hello         4          8

df[apply(df, 1, function(x) sum(grepl(pattern = "^WHAT_", x = x))) != 0, ]
#>         col1        col2       col3 othercol1 othercol11
#> 1      Hello WHAT_hello2      Hello         1          9
#> 2 WHAT_hello  WHAT_hello WHAT_hello         3          2

Создано 2021-01-20 пакетом REPEX (v0.3.0)

Используя tidyverse

library(tidyverse)
df <- data.frame(col1 = c("Hello", "WHAT_hello", "Hello"), 
                 col2 = c("WHAT_hello2", "WHAT_hello", "Hello"), 
                 col3 = c("Hello", "WHAT_hello", "Hello"),
                 othercol1 = sample(1:10, 3), 
                 othercol11 = sample(1:10, 3), 
                 stringsAsFactors = FALSE)


df %>% 
  filter(rowSums(across(.cols = where(is.character), .fns = ~ str_detect(.x, "^WHAT"))) != 0)
#>         col1        col2       col3 othercol1 othercol11
#> 1      Hello WHAT_hello2      Hello         1          3
#> 2 WHAT_hello  WHAT_hello WHAT_hello         7          4

Создано 2021-01-20 пакетом REPEX (v0.3.0)

1
Yuriy Saraykin 20 Янв 2021 в 15:28