r-在tibble的整行中搜索字符串



我正在尝试清理来自许多不同组的样本信息表,因此我关心的治疗信息可能位于任何数量的不同列中。下面是一个抽象的例子:

sample_info = tribble(
~id, ~could_be_here, ~or_here,    ~or_even_in_this_one,
1,   NA,             "not_me",    "find_me_other_stuff",
2,   "Extra_Find_Me", NA,         "diff_stuff",
3,   NA,              "Find_me",  NA,
4,   NA,              "not_here", "not_here_either"
)

我想在哪里找到";find_me";1( 不区分大小写,2(它可以在任何列中,3(它可以作为较大字符串的一部分。我想创建一个TRUE或FALSE的列;find_me";在任何列中都找到。我该怎么做?(我曾想过unite处理所有列,然后在这个烂摊子上运行str_detect,但肯定有一种不那么麻烦的方法,对吧?(

需要明确的是,我想要一个等价于sample_info %>% mutate(find_me = c(TRUE, TRUE, TRUE, FALSE))的最终tibble。

我希望在下面链接的类似情况中使用类似stringr::str_detect(., regex('find_me', ignore_case = T))pmap_lgl(any(c(...) <insert logic check>))的东西,但我不确定如何将它们组合成一个mutate兼容的语句。

我浏览过的内容:
按行操作,查看是否有列在任何其他列表中

R: 如何在使用str_detect时忽略大小写?

在R中,检查字符串是否出现在数据帧的行(任何列(中

一个dplyrpurrr选项可以是:

sample_info %>%
mutate(find_me = pmap_lgl(across(-id), ~ any(str_detect(c(...), regex("find_me", ignore_case = TRUE)), na.rm = TRUE)))
id could_be_here or_here  or_even_in_this_one find_me
<dbl> <chr>         <chr>    <chr>               <lgl>  
1     1 <NA>          not_me   find_me_other_stuff TRUE   
2     2 Extra_Find_Me <NA>     diff_stuff          TRUE   
3     3 <NA>          Find_me  <NA>                TRUE   
4     4 <NA>          not_here not_here_either     FALSE

或者只使用dplyr:

sample_info %>%
rowwise() %>%
mutate(find_me = any(str_detect(c_across(-id), regex("find_me", ignore_case = TRUE)), na.rm = TRUE))

我希望我把你的想法想对了。这就是我在多个列中查找所有find_me的方法:

library(dplyr)
library(purrr)
library(stringr)
sample_info = tribble(
~id, ~could_be_here, ~or_here,    ~or_even_in_this_one,
1,   NA,             "not_me",    "find_me_other_stuff",
2,   "Extra_Find_Me", NA,         "diff_stuff",
3,   NA,              "Find_me",  NA,
4,   NA,              "not_here", "not_here_either"
)
sample_info %>%
mutate(find_me_exist = if_any(, ~ str_detect(., regex("find_me", ignore_case = TRUE), )
, .names = "{.col}.fn{.fn}"))
# A tibble: 4 x 5
id could_be_here or_here  or_even_in_this_one find_me_exist
<dbl> <chr>         <chr>    <chr>               <lgl>        
1     1 NA            not_me   find_me             TRUE         
2     2 Extra_Find_me NA       diff_stuff          TRUE         
3     3 NA            find_Me  NA                  TRUE         
4     4 NA            not_here not_here_either     FALSE

很抱歉,我不得不编辑我的代码,使其不区分大小写。

如果您确实想尝试这种巧妙的方法,那么使用unite的想法确实有效:

sample_info %>% unite(new, remove = FALSE) %>% 
mutate(found = str_detect(.$new, regex("find_me", ignore_case = TRUE))) %>% 
select(-new)

这是dplyr::if_any的典型用例。所选列的if_any具有匹配,则新列输出为TRUE。将regex()与参数ignore_case = TRUE用于不区分大小写的匹配。

library(dplyr)
library(stringr)
sample_info |> 
mutate(find_me = if_any(-id,(x) str_detect(x, regex("find_me", ignore_case = TRUE))))
# A tibble: 4 × 5
id could_be_here or_here  or_even_in_this_one find_me
<dbl> <chr>         <chr>    <chr>               <lgl>  
1     1 NA            not_me   find_me_other_stuff TRUE   
2     2 Extra_Find_Me NA       diff_stuff          TRUE   
3     3 NA            Find_me  NA                  TRUE   
4     4 NA            not_here not_here_either     NA     

最新更新