Skip to content

Instantly share code, notes, and snippets.

@gshotwell
Created December 16, 2016 14:46
Show Gist options
  • Save gshotwell/c994bec00ecce47b5aabe8ac2b89fd27 to your computer and use it in GitHub Desktop.
Save gshotwell/c994bec00ecce47b5aabe8ac2b89fd27 to your computer and use it in GitHub Desktop.
I often want to match the classes between two data.frames, for instance when you lose column class information by moving between wide and long formats. Here is a convenience function to do that.
classify <- function(value, function_char){
fun <- get(paste0("as.", function_char))
fun(value)
}
mtcars_char[] <- map(mtcars_char, as.character)
map(mtcars_char, class)
mtcars_char[] <- map2(mtcars_char,
map_chr(mtcars, class),
~classify(.x, .y))
map(mtcars_char, class)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment