2

I have the following dataframe (df1)

> df1
  var1 var2 var3 df2
1   ac   bc   bc  0
2   bc   bc   cc  1
3   dc   ec   dc  1
4   gc   gc   gc  0

I would like to get a new dataframe (df2) that will contain only the values of 1 in column df2. df2 will be as followed:

> df2
      var1 var2 var3  
    2   bc   bc   cc  
    3   dc   ec   dc 

How can I do it?

1
  • 2
    df1[df1$df2 == 1, -4] Commented Sep 21, 2015 at 12:47

1 Answer 1

1

subset should work:

df2 <- subset(df1, df2 !=0)
df2 <- df2[,1:3]
df2
#var1 var2 var3
#2   bc   bc   cc
#3   dc   ec   dc
Sign up to request clarification or add additional context in comments.

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.