profile
viewpoint

Ask questionsFeature: turn all should-be numeric columns into numeric

I have some data that comes in quite dirty, including as all character columns because there are extra "header" rows that appear periodically in the data. But most of the columns should be numeric.

I clean it up and remove those other-header rows so I have pure data. At this point I want to put the should-be numeric columns back to numerics. Here's what I have, should this be included in janitor? Or generalized somehow? I am looking for input.

# if all values of a character vector are numbers or NA, return it as class numeric
recast_numerics <- function(x){
  if(is.character(x) & !any(is.na(as.numeric(x[!is.na(x)])))){
    as.numeric(x)
  } else {
    x
  }
}

# Usage:
dat %>%
  mutate_all(recast_numerics)

It seems to work for my test case, the only thing I don't like is that it prints a warning when it tries coercing actual character vectors to numeric and that generates NAs.

sfirke/janitor

Answer questions jzadra

Wow I didn't know about that either. I had a custom function that went and checked each column to see if converting caused NAs (ie not all numeric), and only if it doesn't cause them, converts to numeric. type_convert() is much better. Thanks.

useful!

Related questions

No questions were found.
source:https://uonfu.com/
answerer
Jonathan Zadra jzadra @Sorenson-Impact Salt Lake City, UT www.sorensonimpact.com Director of Data Science @ Sorenson Impact Center
Github User Rank List