将R data.frame中的几列从整数转换为数字 [英] Convert several columns from integer to numeric in R data.frame
本文介绍了将R data.frame中的几列从整数转换为数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将 2 到 13(最后一个)的列从整数转换为数字.
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)
我想使用相同的命令将列从 2 转换为 13.我创建了这个向量:
I want to convert columns from 2 to 13 with the same command. I create this vector:
dades<-2:13
那么,我该如何使用lapply
?
推荐答案
我们可以在数据集的子集(dades[2:13]
)上使用lapply
,转换到 numeric
并将其分配回这些列.
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)
这篇关于将R data.frame中的几列从整数转换为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文