Changing Columns 22-300 from double (or other type) into numeric

122 Views Asked by At

I have a huge dataset and some columns are text. Upon importing the Excel file and using preview, I can manually change the first 50 columns to numeric, if this applies. But, there are still 250 more columns I need to change to numeric. How would I use R code to change all columns from column 22 through column 300 to numeric?

2

There are 2 best solutions below

0
On BEST ANSWER

One way:

mydf[22:300] <- lapply(mydf[22:300], as.numeric)
0
On

We can use type.convert (assuming the columns are character class)

df1[22:300] <- type.convert(df1[22:300], as.is = TRUE)

Also, with mutate_at from dplyr

library(tidyverse)
df1 %>%
     mutate_at(22:300, type_convert)