expss looks great for my purpose, but I am having a small problem with it.
Try 1 (not working): When I use expss on a dataframe everything works fine, but then I want to subset the data frame and then call expss on each subset. To subset and call expss I am using a for loop. The data frame is subset correctly, but expss does not return anything.
municipal_lists <- unique(leeftijd1$Regio)[1:3]
for (i in seq_along(municipal_lists)){
zx <-subset(leeftijd1, leeftijd1$Regio == municipal_lists[i])
zx %>%
expss::tab_rows(Regio) %>%
expss::tab_cells(`Leeftijdscategorie 1`) %>%
expss::tab_cols(Perioden) %>%
expss::tab_stat_cases() %>%
expss::tab_pivot()
}
Try 2 (not working): I then created a function of all expss call. When I use this function on a data frame, it works. But if I put this function into a for loop, nothing is returned from the function.
get_table <-function(zx){
zx %>%
expss::tab_rows(Regio) %>%
expss::tab_cells(`Leeftijdscategorie 1`) %>%
expss::tab_cols(Perioden) %>%
expss::tab_stat_cases() %>%
expss::tab_pivot() #%>%
}
municipal_lists <- unique(leeftijd1$Regio)[1:3]
for (i in seq_along(municipal_lists)){
zx <-subset(leeftijd1, leeftijd1$Regio == municipal_lists[i])
get_table(zx)
}
Function is working: A function call works without a for loop, but for whole data frame (with a fixed index)
get_table <-function(zx){
zx %>%
expss::tab_rows(Regio) %>%
expss::tab_cells(`Leeftijdscategorie 1`) %>%
expss::tab_cols(Perioden) %>%
expss::tab_stat_cases() %>%
expss::tab_pivot()
}
municipal_lists <- unique(leeftijd1$Regio)[1:3]
zx <-subset(leeftijd1, leeftijd1$Regio == municipal_lists[1])
get_table(zx)
Direct Call is working: The simplest form without a for loop or without function call is working as expected.
zx <- leeftijd1
zx %>%
expss::tab_rows(Regio) %>%
expss::tab_cells(`Leeftijdscategorie 1`) %>%
expss::tab_cols(Perioden) %>%
expss::tab_stat_cases() %>%
expss::tab_pivot()
So, how do I get a expss table for each subset of the larger data frame? Could someone guide me please.
You need to do something with result of your calculations inside loop. You can print them, for example:
municipal_lists <- unique(leeftijd1$Regio)[1:3]
for (i in seq_along(municipal_lists)){
zx <-subset(leeftijd1, leeftijd1$Regio == municipal_lists[i])
zx %>%
expss::tab_rows(Regio) %>%
expss::tab_cells(`Leeftijdscategorie 1`) %>%
expss::tab_cols(Perioden) %>%
expss::tab_stat_cases() %>%
expss::tab_pivot() %>%
print()
}