Home > Software engineering >  How to assign new variables after group_split by automatically?
How to assign new variables after group_split by automatically?

Time:11-25

I try do split a dataframe by two variables, year and sectors. I did split them with group_split but everytime I need them, I have to call them with $ operator. I want to give them a name automatically so I do not need to use $ for every usage. I know I can assign them to new names by hand but I have more than 70 values so it's a bit time consuming

dummy <- data.frame(year = rep(2014:2020, 12),
                    sector = rep(c("auto","retail","sales","medical"),3),
                    emp = sample(1:2000, size = 84))

dummy%>%
    group_by(year)%>%
    group_split(year)%>%
    set_names(nm = unique(dummy$year)) -> dummy_year

head(dummy_year$2014)

    
year    sector  emp
<int>   <chr>  <int> 
2014    auto    171     
2014    medical 1156        
2014    sales   1838        
2014    retail  1386        
2014    auto    1360        
2014    medical 1403    

I want to call them like

some_kind_of_function(dummy_year, assign new variable by date) 

head(year_2014)
year    sector  emp
<int>   <chr>  <int> 
2014    auto    171     
2014    medical 1156        
2014    sales   1838        
2014    retail  1386        
2014    auto    1360        
2014    medical 1403

maybe a for loop?

CodePudding user response:

group_split wouldn't create named list. We can use split from base R

lst1 <- split(dummy, dummy$year)
names(lst1) <- paste0('year_', names(lst1))

If we want to create objects (not recommended), use list2env

list2env(lst1, .GlobalEnv)

-output

> year_2014
   year  sector  emp
1  2014    auto  740
8  2014 medical  123
15 2014   sales  700
22 2014  retail  166
29 2014    auto  323
36 2014 medical  653
43 2014   sales  986
50 2014  retail 1814
57 2014    auto 1381
64 2014 medical  661
71 2014   sales 1362
78 2014  retail  641

CodePudding user response:

Maybe you want something like this:

library(dplyr)
dummy %>% 
  split(f = paste0("year_", as.factor(.$year)))
  • Related