0

so I have a csv file with around 60 variables that I have imported in R and I'd like to convert the columns to datatypes like numeric, Posixt, and Boolean. In my previous files I needed to do so only for a limited set of columns so I simply used 4-5 times.

data$var1 <- as.numeric(as.character(data$var1)
data$var2 <- as.numeric(as.character(data$var2)

For now however, when I want to convert 60 columns I'd rather not write 60 lines of code again and again. Could someone help me come up with a more efficient way?

JK077
  • 1
  • 2
  • To turn first 2 columns to numeric from factor you can do `df[1:2] <- lapply(df[1:2], function(x) as.numeric(as.character(x)))`. You can do the same to change other columns to their respective classes. – Ronak Shah Jul 10 '20 at 05:40
  • You could look into `mutate_at` or `mutate_if` dplyr functions. – Paul Jul 10 '20 at 06:38

0 Answers0