如何在R中选择以特定字符串开头的行?

neskvpey  于 7个月前  发布在  其他
关注(0)|答案(4)|浏览(90)

我有这样的数据:

df <- data.frame(name = c("James", "jonathan", "Abel", "Cynthia", "Cornelius", "alex"))

      name
     James
  jonathan
      Abel
   Cynthia
 Cornelius
      alex

字符串
我想选择name不以“A”或“J”开头开始的行。预期结果:

name
   Cynthia
 Cornelius


我想要一个简单的dplyr解决方案。

4nkexdtk

4nkexdtk1#

一种选择是将dplyr::filter()与不区分大小写的stringr::str_starts()结合使用:

df |>
    dplyr::filter(
        !stringr::str_starts(name, "(?i)A|J")
    )

字符串
(?i)告诉它对大小写不敏感,|告诉它查找a/Aj/J
输出量:

name
1   Cynthia
2 Cornelius

cnwbcb6i

cnwbcb6i2#

您可以将greplfilter一起使用。"^[A|J]"匹配以A或J开头的字符串,而ignore.case = TRUE表示小写和大写字母都匹配。由于您希望保留不以A或J开头的值,因此可以使用!反转选择:

library(dplyr)
df |>
  filter(!grepl("^[A|J]", name, ignore.case = TRUE))

#        name
# 1   Cynthia
# 2 Cornelius

字符串

v9tzhpje

v9tzhpje3#

在base R中,可以使用grep来索引(使用invert = TRUEignore.case = TRUE):

df[grep("^A|^J", df$name, invert = TRUE, ignore.case = TRUE),]

#[1] "Cynthia"   "Cornelius"

字符串

gblwokeq

gblwokeq4#

dplyr没有任何特殊的字符串函数。您只想使用filter测试第一个字母是否不是A或J。这里有一种方法,将name转换为大写以进行测试,使用substr提取第一个字符,然后测试它是否不是J或A:

library(dplyr)
df |>
  filter(!substr(toupper(name), 1, 1) %in% c("J", "A"))

字符串
你可以用很多其他的方法来编写测试,比如从base使用greplstartsWith,使用stringr::str_detectstringi::stri_detect等等。

相关问题