0
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?

3 Answers 3

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

Here is a dplyr solution:

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

2 Comments

The level 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!
Thanks! I didn't realize that. Updated the dplyr solution with droplevels()
0

An option with subset from base R

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.