Aryh
Aryh

Reputation: 585

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?

Upvotes: 0

Views: 42

Answers (3)

akrun
akrun

Reputation: 886938

An option with subset from base R

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

Upvotes: 0

Matt
Matt

Reputation: 7385

Here is a dplyr solution:

  DF <- DF %>% 
  filter(Heart_attack != 0) %>% 
  droplevels()

Upvotes: 0

Chris Ruehlemann
Chris Ruehlemann

Reputation: 21400

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

Upvotes: 1

Related Questions