0

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?

Ray
  • 25
  • 4
  • 1
    If you want to convert all columns of one type into another, you can use `mutate_if()`. For example, here's the code to convert all character columns to numeric: `mutate_if(dataframe, is.character, as.numeric)` – Andrew Brēza Jul 08 '19 at 14:04

2 Answers2

2

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)
akrun
  • 874,273
  • 37
  • 540
  • 662
1

One way:

mydf[22:300] <- lapply(mydf[22:300], as.numeric)
LyzandeR
  • 37,047
  • 12
  • 77
  • 87