2

I have file that has 4 columns, the first columns is the student name, second his final grade in Math, third is final grade in science,and the fourth column is final grade in art. the final grade is pass or fail. I want to keep only students who passed all subjects (i.e. has pass on all subjects). I read the data using read.csv in R, but I wasn't able to filter the data.

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

form the example above, I need to keep only students who passed all like Mick and Mark

Sotos
  • 51,121
  • 6
  • 32
  • 66
Mark K.
  • 67
  • 7
  • 1
    What's the problem? What's your specific question? Please provide a [Minimal, complete, and verifiable example of your problem.](https://stackoverflow.com/help/mcve) – Steven Feb 16 '18 at 15:46
  • 2
    You got my downvote because you did not provide a reproducible example of your dataset. Without the reproducible example it is difficult for people to study your question and develop a solution. If you can update your post by providing a reproducible example and the desired output, I will retract my downvote and give you an upvote. – www Feb 16 '18 at 15:46
  • Try `df[rowSums(df[-1] == 'pass') == ncol(df)-1,]` – Sotos Feb 16 '18 at 15:55
  • [How to make a great R reproducible example?](http://stackoverflow.com/questions/5963269) – Sotos Feb 16 '18 at 15:58
  • Thank you Sotos, and www. Please accept my apologize, I am new learner in R. – Mark K. Feb 16 '18 at 16:04
  • Mark. No need to apologize. I just want to make sure you got the help you need and others can help you. I decided to retract my downvote and give you an upvote as I promised, but please notice that your data are still not reproducible. Please refer to the link Sotos shared. – www Feb 16 '18 at 16:07
  • try read.csv.sql from sqldf package. – Rushabh Patel Feb 16 '18 at 16:16

3 Answers3

0

I like @Sotos example in the comments, but I don't think it's as easy to understand as a dplyr example:

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")
Steven
  • 3,238
  • 21
  • 50
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),]
trosendal
  • 1,225
  • 9
  • 14
0

using base 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
Onyambu
  • 67,392
  • 3
  • 24
  • 53