Sample_ID<-c("a1","a2","a3","a4","a5","a6")
Heart_attack<-c("1", "0", "1", "1", "0", "2")
DF<-data.frame(Sample_ID,Heart_attack)
I want to exclude from my data frame, all the samples having "0" in Heart_attack.How to do that?
If you do str(DF) you will see that Heart_attack is of type factor. Therefore you need to drop the 0 level:
df2 <- droplevels(DF[-which(DF$Heart_attack == "0"), ])
df2
Sample_ID Heart_attack
1 a1 1
3 a3 1
4 a4 1
6 a6 2
To check whether the 0level really has disappeared, you can use table:
table(df2$Heart_attack)
1 2
3 1
Here is a dplyr solution:
DF <- DF %>%
filter(Heart_attack != 0) %>%
droplevels()
0is still there even after the subset operation. Check out table(DF)(after the subset): table(DF$Heart_attack) 0 1 2 0 3 1 This goes for both the base R and the dplyr solution!dplyr solution with droplevels()