Last active
January 4, 2017 01:31
-
-
Save aammd/566290959bd3e935e52b32067d5e9582 to your computer and use it in GitHub Desktop.
cantrip to turn a matrix into a data.frame, assuming that the first row of the matrix contains a header row
This file contains hidden or 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
matrix_to_df_firstline_header <- function(mat){ | |
requireNamespace("purrr") | |
mat %>% | |
## cut columns into lists | |
apply(2, function(s) list(s)) %>% | |
flatten %>% | |
map(flatten_chr) %>% | |
## set names to the first element of the list | |
{set_names(x = map(., ~ .x[-1]), | |
nm = map_chr(., 1))} %>% | |
## create data.frames | |
as.data.frame(stringsAsFactors = FALSE) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment