Home > Net >  Mapping my user function in several columns with purrr
Mapping my user function in several columns with purrr

Time:05-05

I´m trying to use map function to apply it in several columns in my dataframe. My function has two parameters; data and a column, and works fine, but I´d like to apply it a several columns in the same time. For example, that's how it works with the variable BAD (numerical)

> frec_procedure<-function(data,colu){
  
    data%>% count({{colu}},name = "Frecuency") %>% 
      mutate(Percent =formattable::percent(Frecuency / sum(Frecuency)),
             Cumulative_Freq=cumsum(Frecuency),
             Cumulative_Perc=cumsum(Percent))
  }
> 
> d1 %>% frec_procedure(BAD)
# A tibble: 2 x 5
    BAD Frecuency Percent    Cumulative_Freq Cumulative_Perc
  <dbl>     <int> <formttbl>           <int> <formttbl>     
1     0      4771 80.05%                4771 80.05%         
2     1      1189 19.95%                5960 100.00%       

or with variable JOB(character)

d1 %>% frec_procedure(JOB)
# A tibble: 7 x 5
  JOB     Frecuency Percent    Cumulative_Freq Cumulative_Perc
  <chr>       <int> <formttbl>           <int> <formttbl>     
1 Mgr           767 12.87%                 767 12.87%         
2 Office        948 15.91%                1715 28.78%         
3 Other        2388 40.07%                4103 68.84%         
4 ProfExe      1276 21.41%                5379 90.25%         
5 Sales         109 1.83%                 5488 92.08%         
6 Self          193 3.24%                 5681 95.32%         
7 NA            279 4.68%                 5960 100.00%    

So, the trouble is when I try to apply my function in others variables at the same time using map function (purr package). I´m using this to map the function, but the results are only show the total number of cases of d1 without considering the frec_procedure per variable

> try_map <- tibble(vars = c("BAD", "JOB", "REASON")) %>% 
    mutate(metrics = vars %>% map(~frec_procedure(d1)))
> try_map$metrics
[[1]]
# A tibble: 1 x 4
  Frecuency Percent    Cumulative_Freq Cumulative_Perc
      <int> <formttbl>           <int> <formttbl>     
1      5960 100.00%               5960 100.00%        

[[2]]
# A tibble: 1 x 4
  Frecuency Percent    Cumulative_Freq Cumulative_Perc
      <int> <formttbl>           <int> <formttbl>     
1      5960 100.00%               5960 100.00%        

[[3]]
# A tibble: 1 x 4
  Frecuency Percent    Cumulative_Freq Cumulative_Perc
      <int> <formttbl>           <int> <formttbl>     
1      5960 100.00%               5960 100.00%   

I would appreciate any advice or help possible to achieve the outcome. Thank you so much.

CodePudding user response:

The {{}} works with unquoted arguments. In order to pass quoted, we may have to convert to symbol and evaluate (!!) - ensym does that

frec_procedure<-function(data,colu){

   data%>% count(!! rlang::ensym(colu),name = "Frecuency") %>% 
     mutate(Percent =formattable::percent(Frecuency / sum(Frecuency)),
            Cumulative_Freq=cumsum(Frecuency),
            Cumulative_Perc=cumsum(Percent))
 }

-testing

> map(c("cyl", "vs"), ~ frec_procedure(mtcars, !! .x))
[[1]]
  cyl Frecuency Percent Cumulative_Freq Cumulative_Perc
1   4        11  34.38%              11          34.38%
2   6         7  21.88%              18          56.25%
3   8        14  43.75%              32         100.00%

[[2]]
  vs Frecuency Percent Cumulative_Freq Cumulative_Perc
1  0        18  56.25%              18          56.25%
2  1        14  43.75%              32         100.00%

> frec_procedure(mtcars, cyl)
  cyl Frecuency Percent Cumulative_Freq Cumulative_Perc
1   4        11  34.38%              11          34.38%
2   6         7  21.88%              18          56.25%
3   8        14  43.75%              32         100.00%
  • Related