如何在R中过滤多列

时间:2018-07-31 08:41:46

标签: r

我有数据:

structure(list(A = structure(c(7L, 4L, 8L, 9L, 3L, 5L, 6L, 1L, 
2L), .Label = c("i", "o", "q", "r", "s", "u", "w", "x", "y"), class = "factor"), 
Value = c(2, 2, 2, 3, 2, 3, 1, 1, 1), EDU = structure(c(1L, 
1L, 1L, 1L, 2L, 2L, 3L, 4L, 5L), .Label = c("A", "B", "D", 
"E", "F"), class = "factor")), class = "data.frame", row.names = c(NA, 
-9L))

我的输出是:

structure(list(A = structure(c(4L, 2L, 5L, 6L, 1L, 3L), .Label = c("q", 
"r", "s", "w", "x", "y"), class = "factor"), Value = c(2, 2, 
 2, 3, 2, 3), EDU = structure(c(1L, 1L, 1L, 1L, 2L, 2L), .Label = c("A", 
"B"), class = "factor")), class = "data.frame", row.names = c(NA, 
-6L))

我想过滤具有value >= 2的EDU列,并且应该出现在所有column A中。
例如,EDU列中的行元素“ A”中的示例在第二列中具有大于2的所有值。这将是我第一列中所有2以上的元素,因此第一列中所有EDU“ A”的值都在2以上
我尝试了dplyr的多个过滤器选项,但没有成功。

1 个答案:

答案 0 :(得分:0)

尝试这种代码,对我有用!

df <- data.frame(A = c("w","r","x","y","q","s","u","i","o"),
             Value = c("2","2","2","3","2","3","1","1","1"),
             EDU = c("A","A","A","A","B","B","D","E","F"))


# The two first columns have been transformed in numeric
n <- ncol(df) - 1
for(i in 1:n){
 df[,i] <- as.numeric(df[,i])
}

# Filter
library(dplyr)
library(magrittr)

df1 <- df %>%
 filter(Value >= 2 & EDU == "A")