使用示例數據框:數值和處理缺失值
df <- structure(list(
KY27PHY1 = c("4", "5", "5", "4", "-", "4", "2","3", "5", "-", "4", "3", "3", "5", "5"),
KY27PHY2 = c("4", "4","4", "4", "-", "5", "2", "3", "5", "-", "5", "3", "3", "5", "5"),
KY27PHY3 = c("5", "4", "4", "4", "-", "5", "1", "4", "5","-", "4", "3", "3", "5", "5")),
.Names = c("KY27PHY1", "KY27PHY2","KY27PHY3"),
row.names = 197:211,
class = "data.frame")
我一直在使用下面的代碼將值轉換爲數字:
df$KY27PHY1<-as.numeric(df$KY27PHY1)
df$KY27PHY2<-as.numeric(df$KY27PHY2)
df$KY27PHY3<-as.numeric(df$KY27PHY3)
既然我已經遺漏值在df數據幀中,我總是收到警告信息:
Warning message:
NAs introduced by coercion
我認爲這不是問題,但我只是wa對我如何改進代碼提出了一些建議,所以我沒有得到這個消息。
另外,我怎麼能一次做所有的列(由名稱指定)?
非常感謝提前。
這真的很有用@ flodel - 我不知道那段代碼。 –
那麼,我會給它一個答案,然後...如果你願意接受一個。 – flodel