2017-03-31 113 views
-1

我是新來的R和我使用的數據集從kaggle on bombing operations in WWII有麻煩的更換NA

我想,以取代所有的NA的與「UKNOWN」一列,但我得到這個

Warning message: 
In `[<-.factor`(`*tmp*`, is.na(operations[,"theater.of.operations"]), : 
invalid factor level, NA generated 

這是我試圖做到這一點

operations[, "theater.of.operations"][is.na(operations[, "theater.of.operations"])] <- "UNKNOWN" 

否則怎麼我應該嘗試這樣做?

+2

轉換爲字符 – Sotos

+1

嘗試使用as.character()先將您的df列轉換爲字符,因爲Sotos提到 –

+0

謝謝我並不真正瞭解「轉換爲字符」的含義,但@SingleEntity使其更加清晰,所以我用「操作$劇院。操作< - as.character(操作$ theater.of.operations)」並且它工作。 – Niall

回答

2

要將NA(不是字符串類型)轉換爲字符串,例如"UNKNOWN",您需要將數據幀列轉換爲字符串,如下所示。

operations$theater.of.operations <- as.character(operations$theater.of.operations) 

關鍵元件是功能as.character()其投射到你所需的字符變量類型。