r - Programmatically factorize selected columns in data frame, the tidy way? -


here simplified example:

library(tidyverse)  frame <- tribble(   ~a, ~b, ~c,    1,  1,  2,    5,  4,  7,    2,  3,  4,     3,  1,  6 )  key <- tribble(   ~col, ~name, ~type, ~labels,      1,   "a",   "f",     c("one", "two", "three", "four", "five"),      2,   "b",   "f",     c("uno", "dos", "tres", "cuatro"),      3,   "c",   "f",     1:7 ) 

is there elegant way of programmatically sweeping across columns in frame , applying specific factor class, based on parameters in key? expected result be:

# tibble: 4 x 3             b      c   <fctr> <fctr> <fctr> 1    1    uno      2 2   5 cuatro      7 3    2   tres      4 4  3    uno      6 

the best solution have far using purrr's map2() assignment imo not elegant:

frame[key$col] <- map2(key$col, key$labels,          function(x, y) factor(frame[[x]], levels = 1:length(y), labels = y)) 

does have more tidy solution? note original data frame has hundreds of columns , need re-factor different levels/labels majority of them, process has automated.

i'm interested see other solutions proposed this. suggestion change proposed solution clearer frame going modified in way rather leaving in body of function used map2.

for example, pass frame additional argument in call map2:

frame[key$col] <- map2(key$col, key$labels,                         function(x, y, z) factor(z[[x]], levels = 1:length(y), labels = y),                         frame) 

or same thing using pipe operator %>%:

frame[key$col] <- frame %>%   { map2(key$col, key$labels,           function(x, y, z) factor(z[[x]], levels = 1:length(y), labels = y), .) } 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -