0

I have a dataframe data with the following structure:

Classes ‘tbl_df’ and 'data.frame':  4391 obs. of  53 variables

When I try to subset it to get the top 100 rows using

data100 = data[1:100,]

I get this error:

Error in `[.data.frame`(X[[i]], ...) : undefined columns selected

What could be the reason?

user3051065
  • 401
  • 1
  • 4
  • 15

1 Answers1

1

Found the answer - I needed to use

as.data.frame(data)

before subsetting because tbl_df is not subsettable the same way as a data frame. This was needed due to using dplyr earlier and it outputting a table instead of a df.

user3051065
  • 401
  • 1
  • 4
  • 15
  • 1
    You might have some larger issue you may wish to investigate, because `as.tbl(mtcars)[1:10, ]` (example) works fine. Seems strange that you would need `as.data.frame()` to solve it. – Rich Scriven Aug 30 '15 at 21:44
  • i would say more than likely you forgot that comma – rawr Aug 30 '15 at 21:51
  • Comma was definitely there, the code is just as copied, that was one of the first things I double-checked. Not sure why as.data.frame() worked, but it's all running fine now – user3051065 Aug 30 '15 at 22:04