【问题标题】:How to use filter across and str_detect together to filter conditional on mutlitple columns如何一起使用 filter across 和 str_detect 来过滤多个列的条件
【发布时间】:2021-09-08 19:16:04
【问题描述】:

我有这个数据框:

df <- structure(list(col1 = c("Z2", "A2", "B2", "C2", "A2", "E2", "F2", 
"G2"), col2 = c("Z2", "Z2", "A2", "B2", "C2", "D2", "A2", "F2"
), col3 = c("A2", "B2", "C2", "D2", "E2", "F2", "G2", "Z2")), class = "data.frame", row.names = c(NA, -8L))

> df
  col1 col2 col3
1   Z2   Z2   A2
2   A2   Z2   B2
3   B2   A2   C2
4   C2   B2   D2
5   A2   C2   E2
6   E2   D2   F2
7   F2   A2   G2
8   G2   F2   Z2

我想在tidyverse 设置中明确使用filteracrossstr_detect 来过滤所有以A 开头的行而不是col1:col3

预期结果:

  col1 col2 col3
1   Z2   Z2   A2
2   A2   Z2   B2
3   B2   A2   C2
4   A2   C2   E2
5   F2   A2   G2

我试过了:

library(dplyr)
library(stringr)
df %>% 
    filter(across(c(col1, col2, col3), ~str_detect(., "^A")))

这给出了:

[1] col1 col2 col3
<0 Zeilen> (oder row.names mit Länge 0)

我想了解为什么这段代码在使用 filteracrossstr_detect 时不起作用!

【问题讨论】:

    标签: r dplyr tidyverse stringr across


    【解决方案1】:

    我们可以使用if_any,因为across 将寻找&amp; 条件,即所有列都应满足特定行的条件才能获得filtered

    library(dplyr)
    library(stringr)
    df %>% 
        filter(if_any(everything(), ~str_detect(., "^A"))) 
    

    -输出

       col1 col2 col3
    1   Z2   Z2   A2
    2   A2   Z2   B2
    3   B2   A2   C2
    4   A2   C2   E2
    5   F2   A2   G2
    

    根据?across

    if_any() 和 if_all() 将相同的谓词函数应用于选择的列,并将结果组合成一个逻辑向量:当谓词对于任何选定的列为 TRUE 时,if_any() 为 TRUE,if_all()当所有选定列的谓词为 TRUE 时为 TRUE。

    across() 取代了 summarise_at()、summarise_if() 和 summarise_all() 等“范围变体”系列。

    if_any/if_all 不是作用域变体的一部分

    【讨论】:

    • 谢谢阿克伦。由于作用域动词(_if、_at、_all)已被现有动词中across() 的使用所取代,我想像在其他用例中一样使用filter, across and str_detect?!为什么不可能?
    • @TarJae if_anyif_all 是不同的,不属于 filter_allfilter_at 等。我认为这是由于 across 的限制而引入的。
    猜你喜欢
    • 2017-11-29
    • 1970-01-01
    • 2018-09-02
    • 2018-05-30
    • 1970-01-01
    • 1970-01-01
    • 2021-08-20
    • 2021-11-02
    • 1970-01-01
    相关资源
    最近更新 更多