过滤列具有相同的元素

时间:2018-02-16 15:43:08

标签: r

我的文件有4列,第一列是学生姓名,第二列是数学的最终成绩,第三列是科学的最终成绩,第四列是艺术的最终成绩。最终成绩是通过还是失败。我想只保留通过所有科目的学生(即通过所有科目)。我使用R中的read.csv读取数据,但我无法过滤数据。

Col1       col2     col3    col4
Amanda     pass     fail    pass
Mick       pass     pass    pass
Andrew     pass     pass    fail
Mark       pass     pass    pass

形成上面的例子,我只需要保留那些像米克和马克一样通过的学生

3 个答案:

答案 0 :(得分:0)

我喜欢评论中的@Sotos示例,但我认为它不像dplyr示例那样容易理解:

library(dplyr)

df <- 
  read.table(header = TRUE, text = "Col1       col2     col3    col4
             Amanda     pass     fail    pass
             Mick       pass     pass    pass
             Andrew     pass     pass    fail
             Mark       pass     pass    pass")

df %>%
  filter(col2 == "pass" & col3 == "pass" & col4 == "pass")

答案 1 :(得分:0)

grades <- data.frame(name = letters,
           math = as.logical(sample(c(0,1), 26, replace = TRUE)),
           science = as.logical(sample(c(0,1), 26, replace = TRUE)),
           art = as.logical(sample(c(0,1), 26, replace = TRUE)))
## Filter those where the all but the name column is true
grades[apply(grades[,-1], 1, all),]

答案 2 :(得分:0)

使用基数R:

subset(df,!grepl("fail",do.call(paste,df)))
  Col1 col2 col3 col4
2 Mick pass pass pass
4 Mark pass pass pass

df[-grep("fail",do.call(paste,df)),]
  Col1 col2 col3 col4
2 Mick pass pass pass
4 Mark pass pass pass