简体   繁体   中英

Delete rows from dataframe based on column value in R

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 0 level 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()

An option with subset from base R

df2 <-  droplevels(subset(DF, as.logical(as.integer(Heart_attack))))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM