Tdebeus
Tdebeus

Reputation: 1599

Change column class if column consists of numbers

I have a data frame where I had to convert all variables to the character class in order to bind_rows(). Now I want to identify and convert the columns that have numbers in them back to class numeric. I have 41 values so I don't want to have to mutate each of them separately.

Preferably the tidyverse way.

library(dplyr)

data_frame(number_var = as.character(rnorm(1:26)),
           character_var = LETTERS)

Upvotes: 0

Views: 167

Answers (1)

Tino
Tino

Reputation: 2101

You could use parse_guess from readrpackage:

library(dplyr)
library(readr)

df <- data_frame(number_var = as.character(rnorm(1:26)),
           character_var = LETTERS)

df %>% 
  mutate_all(parse_guess) # guess column type for each column

Upvotes: 4

Related Questions