在R data.frame中将几个列从整数转换为数字

时间:2022-05-12 01:39:03

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

我想从第2列到第13列(最后一个)从整数转换为数字。

I use for one column the following code

我用一列代码如下

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

if I want to convert from column 2 to 13 with the same command, so select them

如果我想使用相同的命令从第2列转换为13,那么选择它们

dades<-2:13

and then how do I have to use lapply?

然后我该如何使用lapply?

1 个解决方案

#1


11  

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

我们可以在数据集的子集上使用lapply(dades [2:13]),转换为数字并将其分配回那些列。

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

#1


11  

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

我们可以在数据集的子集上使用lapply(dades [2:13]),转换为数字并将其分配回那些列。

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