I have a dataframe like this:
set.seed(12)
df <- data.frame(
v1 = sample(LETTERS, 10),
v2 = sample(LETTERS, 10),
v3 = sample(LETTERS, 10),
v4 = c(sample(LETTERS, 8), sample(letters, 2)),
v5 = c(sample(letters, 1), sample(LETTERS, 7), sample(letters, 2))
)
df
v1 v2 v3 v4 v5
1 B K F G p
2 U U T W N
3 W J C V Y
4 G I Q S E
5 D F E N T
6 A X Z T C
7 V Y K X I
8 M Z D Q A
9 Y L H k d
10 R B L j t
Run Code Online (Sandbox Code Playgroud)
I want to subset dfon those rows that contain a lowercase value in any of df's columns. It can be done like this:
df1 <- df[grepl("[a-z]", df$v1) | grepl("[a-z]", df$v2) | grepl("[a-z]", df$v3) |
grepl("[a-z]", df$v4) | grepl("[a-z]", df$v5), ]
df1
v1 v2 v3 v4 v5
1 B K F G p
9 Y L H k d
10 R B L j t
Run Code Online (Sandbox Code Playgroud)
但这是不经济的,如果您有很多(更多)列,并且容易出错。有没有更干净、更简单、更经济的方法,最好是在基 R 中?
df[rowSums(sapply(df, function(x) x %in% letters)) > 0,]
#OR
df[apply(df == sapply(df, tolower), 1, any),]
# v1 v2 v3 v4 v5
#1 B L L M e
#9 R N D t t
#10 F X M h x
Run Code Online (Sandbox Code Playgroud)
一种选择是grepl在每一列上应用lapply以创建一个list逻辑vectors 和Reduce它|
df[Reduce(`|`, lapply(df, grepl, pattern = "[a-z]")),]
# v1 v2 v3 v4 v5
#1 B L L M e
#9 R N D t t
#10 F X M h x
Run Code Online (Sandbox Code Playgroud)
或使用 filter_all
library(dplyr)
library(stringr)
df %>%
filter_all(any_vars(str_detect(., "[a-z]")))
# v1 v2 v3 v4 v5
#1 B L L M e
#2 R N D t t
#3 F X M h x
Run Code Online (Sandbox Code Playgroud)
您可以使用paste每行,然后使用grepl.
df[grepl("[a-z]", apply(df, 1, paste, collapse="")),]
# v1 v2 v3 v4 v5
#1 B L L M e
#9 R N D t t
#10 F X M h x
Run Code Online (Sandbox Code Playgroud)
或者另一个选择是do.call
df[grepl("[a-z]", do.call(paste, df)),]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
122 次 |
| 最近记录: |