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 sfirke

ha it seems to do precisely what I'm looking for. I'd never heard of it, so glad they exposed that function and that you could point me toward it! 🎯

useful!

Related questions

No questions were found.
source:https://uonfu.com/
answerer
Sam Firke sfirke City of Ann Arbor Ann Arbor, MI samfirke.com Data scientist, caring human. Currently focused on municipal data analysis and business intelligence.
Github User Rank List