使用 R 删除其中不包含字母的行
Remove rows that do NOT contain a letter in them using R
我试图过滤掉我的数据,所以我只包含 ID 至少有一个字母(在任何地方)的行。我很困惑,因为我有太多带有随机字符或随机空格的行,所以即使我尝试过滤掉空格,它也会错过它们。
这是我的数据:
library(tidyverse)
test <- tibble(id = c(" ", "91a", "90", "ab"),
score = c(5, 10, 15, 91))
这就是我想要的:
library(tidyverse)
answer <- tibble(id = c("91a","ab"),
score = c(10, 91))
谢谢!
您可以使用:
subset(test, grepl('[a-zA-Z]', id))
# id score
# <chr> <dbl>
#1 91a 10
#2 ab 91
或在dplyr
中:
library(dplyr)
test %>% filter(grepl('[a-zA-Z]', id))
您可以使用 stringr
包中的 str_detect
来检测 id
变量中是否存在模式。
library(dplyr)
library(stringr)
library(tibble)
test <- tibble(id = c(" ", "91a", "90", "ab"),
score = c(5, 10, 15, 91))
filter(test, str_detect(id, '[a-zA-Z]'))
#> # A tibble: 2 x 2
#> id score
#> <chr> <dbl>
#> 1 91a 10
#> 2 ab 91
由 reprex package (v0.3.0)
于 2021 年 3 月 10 日创建
我试图过滤掉我的数据,所以我只包含 ID 至少有一个字母(在任何地方)的行。我很困惑,因为我有太多带有随机字符或随机空格的行,所以即使我尝试过滤掉空格,它也会错过它们。
这是我的数据:
library(tidyverse)
test <- tibble(id = c(" ", "91a", "90", "ab"),
score = c(5, 10, 15, 91))
这就是我想要的:
library(tidyverse)
answer <- tibble(id = c("91a","ab"),
score = c(10, 91))
谢谢!
您可以使用:
subset(test, grepl('[a-zA-Z]', id))
# id score
# <chr> <dbl>
#1 91a 10
#2 ab 91
或在dplyr
中:
library(dplyr)
test %>% filter(grepl('[a-zA-Z]', id))
您可以使用 stringr
包中的 str_detect
来检测 id
变量中是否存在模式。
library(dplyr)
library(stringr)
library(tibble)
test <- tibble(id = c(" ", "91a", "90", "ab"),
score = c(5, 10, 15, 91))
filter(test, str_detect(id, '[a-zA-Z]'))
#> # A tibble: 2 x 2
#> id score
#> <chr> <dbl>
#> 1 91a 10
#> 2 ab 91
由 reprex package (v0.3.0)
于 2021 年 3 月 10 日创建