在R data.frame中将几列从整数转换为数值 [英] Convert several columns from integer to numeric in R data.frame

查看:377
本文介绍了在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆