Created
December 16, 2016 14:46
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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