简体   繁体   English

如何在 r 中为 data.frame(NULL) 分配名称?

[英]How to assign data.frame(NULL) a name in r?

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但 R 一直给我错误

"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.谢谢。

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.应用于数据框的 names() 命令返回数据框列的名称,而您的数据框为空且没有列,因此不能有列名。 What are you trying to end up with?你想最终得到什么? Your data frame has a name: Corpusfreq.您的数据框有一个名称: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.不会给你一个 Null 数据框,而是一个带有单列“Word”的数据框,它有一行,而该行有一个 NA。 Maybe that's what you want?也许这就是你想要的?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM