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

Enric Agud Pique picture Enric Agud Pique · Feb 10, 2016 · Viewed 66.1k times · Source

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?

Answer

akrun picture akrun · Feb 10, 2016

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)