使用示例数据框:
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
我认为这不是问题,但我只是想要一些关于如何改进代码的建议,所以我没有收到此消息。
另外,我如何一次完成所有列(按名称指定)?
提前谢谢了。