Search code examples
rvectordplyrautomationsplice

separate output into multiple objects in R automatically


I'm not sure if this is possible, but is there a way in R to run a command and have it save the output into multiple objects based on group? For instance, I wrote a code that calculates what number of employees are in a supervisory role based on their department.

library(tidyverse)
sample <- tibble(department = c("Admin", "Admin", "Office of President", "Office of President"),
                 sup_status = c("Not Supervisor", "Supervisor", "Not Supervisor", "Supervisor"),
                 n = c(918, 152, 69, 192))

But, what I really want is a vector of the percentages of supervisors by department. I can get R to produce one long vector of all percentages:

library(tidyverse)

vector_of_all_percents <- sample %>%
  group_by(department) %>%
  mutate(sum_new = sum(n)) %>%
  rowwise() %>%
  mutate(percent = n/sum_new) %>%
  select(percent) %>%
  as_vector()

vector_of_all_percents
 percent1  percent2  percent3  percent4 
0.8579439 0.1420561 0.2643678 0.7356322 

My actual data has many departments. Is there a way to adjust my above code to get R produce objects by department automatically, something like this:

vector_for_admin
 percent1  percent2
0.8579439 0.1420561 

vector_for_office
percent1  percent2 
0.2643678 0.7356322

I'm not sure if the slice() or split() commands are what I need, or if this is even possible. Any guidance would be very appreciated!


Solution

  • You can use split to create a list:

    library(tidyverse)
    sample <- tibble(department =c("Admin", "Admin", "Office of President", "Office of President"),
                     sup_status =c("Not Supervisor", "Supervisor", "Not Supervisor", "Supervisor"),
                     n = c(918, 152, 69, 192))
    
    list_of_all_percents <- sample %>%
      group_by(department) %>%
      mutate(sum_new = sum(n)) %>%
      rowwise() %>%
      mutate(percent = n/sum_new) %>%
      split(.$department)
    
    list_of_all_percents
    #> $Admin
    #> Source: local data frame [2 x 5]
    #> Groups: <by row>
    #> 
    #> # A tibble: 2 x 5
    #>   department sup_status         n sum_new percent
    #>   <chr>      <chr>          <dbl>   <dbl>   <dbl>
    #> 1 Admin      Not Supervisor   918    1070   0.858
    #> 2 Admin      Supervisor       152    1070   0.142
    #> 
    #> $`Office of President`
    #> Source: local data frame [2 x 5]
    #> Groups: <by row>
    #> 
    #> # A tibble: 2 x 5
    #>   department          sup_status         n sum_new percent
    #>   <chr>               <chr>          <dbl>   <dbl>   <dbl>
    #> 1 Office of President Not Supervisor    69     261   0.264
    #> 2 Office of President Supervisor       192     261   0.736
    

    So if you want to access percents for Admin, you just do

    list_of_all_percents$Admin$percent
    #> [1] 0.8579439 0.1420561
    

    Created on 2020-02-20 by the reprex package (v0.3.0)