-1

I was trying to give a null data frame a name,

    word_list = NULL
    corpusfreq <- data.frame(word_list)
    names(corpusfreq) <- c("Word")

but R keeps giving me the error that

"Error in names(corpusfreq) <- c("Word") : 
  'names' attribute [1] must be the same length as the vector [0]"

I have looked at several similar questions but none of them addressed my question.

Thanks.

Community
  • 1
  • 1
Chubing
  • 187
  • 1
  • 3
  • 10
  • You need to have the columns to name, whether or not they're filled. To allocate and name them, maybe `data.frame(Word = vector())`, though you can [re]name them after the fact, if you like – alistaire Oct 04 '16 at 20:53
  • 3
    `data.frame(NULL)` means you will have no columns and no rows, and hence nothing to name. In fact, `NULL` usually means the removal of a column in the data frame context, so even `data.frame(Word = NULL)` won't name anything. Conclusion - it doesn't make much sense to do this. – Rich Scriven Oct 04 '16 at 20:56
  • You might have been looking for `data.frame(Word = character())`, which gives a single column DF with no rows. – Rich Scriven Oct 04 '16 at 21:03

1 Answers1

-1

The names() command as applied to data frames returns names of the columns of the data frame, and your data frame is null and has no columns, and therefore cannot have column names. What are you trying to end up with? Your data frame has a name: Corpusfreq.

corpusfreq <- data.frame("Word" = NA)

Will give you not a Null data frame, but a data frame with a single column, "Word", that has one row, and that row has an NA. Maybe that's what you want?

Joy
  • 769
  • 6
  • 24