【问题标题】:Warning message: In `...` : invalid factor level, NA generated警告消息:在 `...` 中:无效因子级别,NA 生成
【发布时间】:2013-05-25 01:43:03
【问题描述】:

我不明白为什么会收到此警告消息。

> fixed <- data.frame("Type" = character(3), "Amount" = numeric(3))
> fixed[1, ] <- c("lunch", 100)
Warning message:
In `[<-.factor`(`*tmp*`, iseq, value = "lunch") :
  invalid factor level, NA generated
> fixed
  Type Amount
1 <NA>    100
2           0
3           0

【问题讨论】:

    标签: r warnings r-faq


    【解决方案1】:

    警告消息是因为您的“类型”变量是一个因素,而“午餐”不是一个定义的级别。在制作数据框时使用stringsAsFactors = FALSE 标志强制“类型”为字符。

    > fixed <- data.frame("Type" = character(3), "Amount" = numeric(3))
    > str(fixed)
    'data.frame':   3 obs. of  2 variables:
     $ Type  : Factor w/ 1 level "": NA 1 1
     $ Amount: chr  "100" "0" "0"
    > 
    > fixed <- data.frame("Type" = character(3), "Amount" = numeric(3),stringsAsFactors=FALSE)
    > fixed[1, ] <- c("lunch", 100)
    > str(fixed)
    'data.frame':   3 obs. of  2 variables:
     $ Type  : chr  "lunch" "" ""
     $ Amount: chr  "100" "0" "0"
    

    【讨论】:

    • @David 为什么 R 将其转换为因子?
    • 因为这是 data.frame() 函数中的默认设置(这是默认设置,因为这是大多数用户在绝大多数时间想要的)。
    【解决方案2】:

    如果您是直接从 CSV 文件中读取,那么请这样做。

    myDataFrame <- read.csv("path/to/file.csv", header = TRUE, stringsAsFactors = FALSE)
    

    【讨论】:

    • stringAsFactors 抛出错误:未使用的参数 (stringAsFactors=FALSE)
    • stringsAsFactors - strings 必须是复数 (@Coliban)
    【解决方案3】:

    这是一个灵活的方法,它可以在所有情况下使用,特别是:

    1. 只影响一列,或
    2. dataframe 是通过应用以前的操作获得的(例如不立即打开文件,或创建新的数据框)。

    首先,un-factorize 使用 as.character 函数,然后,re-factorize 使用 as.factor(或简单地 factor)功能:

    fixed <- data.frame("Type" = character(3), "Amount" = numeric(3))
    
    # Un-factorize (as.numeric can be use for numeric values)
    #              (as.vector  can be use for objects - not tested)
    fixed$Type <- as.character(fixed$Type)
    fixed[1, ] <- c("lunch", 100)
    
    # Re-factorize with the as.factor function or simple factor(fixed$Type)
    fixed$Type <- as.factor(fixed$Type)
    

    【讨论】:

      【解决方案4】:

      解决此问题的最简单方法是在列中添加一个新因子。使用级别函数来确定您有多少因子,然后添加一个新因子。

          > levels(data$Fireplace.Qu)
          [1] "Ex" "Fa" "Gd" "Po" "TA"
          > levels(data$Fireplace.Qu) = c("Ex", "Fa", "Gd", "Po", "TA", "None")
          [1] "Ex"   "Fa"   "Gd"   "Po"   " TA"  "None"
      

      【讨论】:

        【解决方案5】:

        我遇到了从 .xlsx 文件中检索数据的类似问题。不幸的是,我在这里找不到正确的答案。我使用 dplyr 自己处理它,如下所示,这可能对其他人有所帮助:

        #install.packages("xlsx")
        library(xlsx)
        extracted_df <- read.xlsx("test.xlsx", sheetName='Sheet1', stringsAsFactors=FALSE)
        # Replace all NAs in a data frame with "G" character
        extracted_df[is.na(extracted_df)] <- "G"
        

        但是,我无法使用与stringsAsFactors 没有类似参数的readxl package 处理它。出于这个原因,我已经转移到xlsx 包。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-12-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-11-19
          • 1970-01-01
          相关资源
          最近更新 更多