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?
Asked
Active
Viewed 98 times
0
-
1If 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 Answers
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