使用dplyr根据列值范围选择列
假设我有这个数据集:使用dplyr根据列值范围选择列,r,dplyr,R,Dplyr,假设我有这个数据集: df <- data.frame(a = rep(1:2, 5), b = c("value", "character", "string", "anotherstring", "character", NA, "code", "variable", NA, "cell"),
df <- data.frame(a = rep(1:2, 5),
b = c("value", "character", "string", "anotherstring", "character", NA, "code", "variable", NA, "cell"),
c = c(1, 2, 5, 4, 5, 7, 8, 9, 6, 10),
d = rep(2:1, 5),
e = rep(1, 10))
df
a b c d e
1 1 value 1 2 1
2 2 character 2 1 1
3 1 string 5 2 1
4 2 anotherstring 4 1 1
5 1 character 5 2 1
6 2 <NA> 7 1 1
7 1 code 8 2 1
8 2 variable 9 1 1
9 1 <NA> 6 2 1
10 2 cell 10 1 1
df您可以使用:
library(dplyr)
df %>% select_if(~any(. == 1) & any(. == 2) & all(. %in% 1:2))
# a d
#1 1 2
#2 2 1
#3 1 2
#4 2 1
#5 1 2
#6 2 1
#7 1 2
#8 2 1
#9 1 2
#10 2 1
在较新版本的dplyr
中,可以将其编写为:
df %>% select(where(~any(. == 1) & any(. == 2) & all(. %in% 1:2)))
在基本R过滤器中相同:
Filter(function(x) any(x == 1) & any(x == 2) & all(x %in% 1:2) , df)
您可以使用:
library(dplyr)
df %>% select_if(~any(. == 1) & any(. == 2) & all(. %in% 1:2))
# a d
#1 1 2
#2 2 1
#3 1 2
#4 2 1
#5 1 2
#6 2 1
#7 1 2
#8 2 1
#9 1 2
#10 2 1
在较新版本的dplyr
中,可以将其编写为:
df %>% select(where(~any(. == 1) & any(. == 2) & all(. %in% 1:2)))
在基本R过滤器中相同:
Filter(function(x) any(x == 1) & any(x == 2) & all(x %in% 1:2) , df)