警告信息:在`...`:無效因子級別,NA生成我不明白為什么我收到這條警告信息。> 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 Amount1 <NA> 1002 03 0
2 回答

蠱毒傳說
TA貢獻1895條經(jīng)驗 獲得超3個贊
警告消息是因為您的“類型”變量是一個因素而“午餐”不是定義的級別。stringsAsFactors = FALSE
在創(chuàng)建數(shù)據(jù)框時使用標志強制“類型”為字符。
> 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"

MYYA
TA貢獻1868條經(jīng)驗 獲得超4個贊
如果您直接從CSV文件中閱讀,請執(zhí)行此操作。
myDataFrame <- read.csv("path/to/file.csv", header = TRUE, stringsAsFactors = FALSE)
- 2 回答
- 0 關(guān)注
- 4067 瀏覽
添加回答
舉報
0/150
提交
取消