dimanche 5 mai 2019

keeping certain rows in data frame with a condition in R

I have a data frame in R for which I want to remove certain rows provided that match certain conditions. How can I do it ?

I have tried using dplyr and ifelse but my code does not give right answer

check8 <- distinct(df5,prod,.keep_all = TRUE)

Does not work! gives the entire data set

Input is - check1 <- data.frame(ID = c(1,1,2,2,2,3,4), prod = c("R","T","R","T",NA,"T","R"), bad = c(0,0,0,1,0,1,0))

I want to have the output such that for IDs where both prod or NA are there, keep only rows with prod R, but if only one prod is there then keep that row despite the prod .

Aucun commentaire:

Enregistrer un commentaire