0

I use head(df,20) to see the first twenty rows of my data. Then I want to see the first twenty rows of my data where VAR1 equal to 1. I use this head(df[df$VAR1==1],20) But it shows

Error in `[.data.frame`(df, df$VAR1== 1) : 
  undefined columns selected

How do I fix it? Thanks in advance.

Terence Tien
  • 329
  • 1
  • 3
  • 15
  • You're subsetting a two-dimensional data.frame, so you need a comma so R knows you're talking about rows instead of columns: `head(df[df$VAR1==1,],20)` – alistaire Jun 03 '16 at 03:28
  • 1
    Duplicate http://stackoverflow.com/questions/19205806/r-undefined-columns-selected-in-subsetting-my-data-frame – Rich Scriven Jun 03 '16 at 04:56

1 Answers1

3

As alistaire mentioned in the comments***:

head(df[df$VAR1==1,],20)

***and if he puts it as an answer I'll delete this so that he can get credit, we just need answers in the answers section and for them to get marked as the solution for the benefit of future readers (most people won't look in comments for a solution)

Hack-R
  • 22,422
  • 14
  • 75
  • 131