gpt4 book ai didi

r - 过滤/子集包含某些字符串以外的任何内容的行

转载 作者:行者123 更新时间:2023-12-04 07:23:27 29 4
gpt4 key购买 nike

我有包含字符串的列的数据框,并希望过滤掉包含某些字符串以外的任何内容的所有行。考虑下面的简化示例:

string <- c('AA', 'BB' , 'AA, BB' , 'BB, AA', 'AAS', 'BB, CC',  'DD')
df <- data.frame(string)
我想保持:
AA
BB
AA, BB
BB, AA
请注意,这是一个 简化示例,您不能简单地过滤 AAS、CC 和 DD 或 AA 和 BB 的组合 ,因为有更多的替代字符串选项。

最佳答案

df %>% dplyr::filter(stringr::str_detect(string, "C|D|S", negate=T))
string
或者
pattern=c('C', 'D', 'S')
df %>%
dplyr::filter(
stringr::str_detect(string, paste(pattern, collapse="|"), negate=T)
)
1     AA
2 BB
3 AA, BB
4 BB, AA

关于r - 过滤/子集包含某些字符串以外的任何内容的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68353527/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com