使用示例数据框:数值和处理缺失值
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