Convert several columns from integer to numeric in R data.frame

五迷三道 提交于 2019-12-30 06:06:54

问题


I would like to convert columns from 2 to 13 (the last one) from integer to numeric.

For one column, I use the following code:

dades$V3 <- as.numeric(dades$V3)

I want to convert columns from 2 to 13 with the same command. I create this vector:

dades<-2:13

Then, how do I use lapply?


回答1:


We can use lapply on the subset of dataset (dades[2:13]), convert to numeric and assign it back to those columns.

dades[2:13] <- lapply(dades[2:13], as.numeric)


来源:https://stackoverflow.com/questions/35320991/convert-several-columns-from-integer-to-numeric-in-r-data-frame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!