首页 > 解决方案 > Create data table containing all possible combinations of values from four lists

问题描述

I have the following four lists.

varnames <- list("beefpork", "breakfast", "breakfast_yn", "diet_soda", "food_label", "fruit_and_veggie", "fruit_juice", "fruits", "milk",                      "min_foods","regular_soda", "ssb", "total_fruit", "vegetables",                      "asthma", "bmiclass3", "bmiclass4","bmiclass5", "dental_absence",                     "dental_appt", "diabetes", "food_allergies", "sore_teeth", "trying_weight",                     "count_pa60days", "count_vigpa20days", "gaming_bedroom", "other_organized_pa", "pa30outdoor","paguidelines", "pc_time", "school_transport", "sport_teams", "tv_bedroom", "tv_time_char", "video_games_char")
grades <- list("2", "4", "8", "11")
groups <- list("none", "ethnic", "bordercounty")
regions <- list("state", "hsr")

And the following function, which returns an integer:

all_empty = function(outcome, groupvar, gradevar, regionvar){
  #How many observations?

  if (groupvar == "none") 
    fmla <- as.formula(paste0("~", outcome))
  else 
    fmla <- as.formula(paste0("~", outcome, "+", groupvar))

  if (regionvar == "hsr")
    mydata = span_phrwts
  else if (regionvar == "state" & groupvar %in% c("none", "ethnic"))
    mydata = span_statewts
  else if (regionvar == "state" & groupvar == "bordercounty")
    mydata = span_borderwts
  else mydata = span_statewts

  myrow = svytable(fmla, subset(mydata, grade==gradevar)) %>% nrow()
  return(myrow)
}

I'm trying to write a code that will run the function on all 864 possible combinations of the values from my lists, and create one data table with 864 rows and 5 columns.

I would like the final table to look something like this, but has not been successful:

Variable          Grade          Group           Region     Obs
beefpork          2              none            state      5
beefpork          4              none            state      5
beefpork          8              none            state      3
beefpork          11             none            state      0

This is my attempt to run this, but am unable to calculate the rownum correctly.

output_all <- matrix(ncol = 5, nrow = length(varnames)*length(grades)*length(groups)*length(regions))
for(l in 1:length(regions)) {
  for (k in 1:length(grades)) {
    for(j in 1:length(groups)) {
      for(i in 1:length(varnames)){
        rownum = i + ((length(groups)*length(grades)*length(regions)) - 1)
        output_all[rownum, 1] = varnames[[i]]
        output_all[rownum, 2] = groups[[j]]
        output_all[rownum, 3] = grades[[k]]
        output_all[rownum, 4] = regions[[l]]
        output_all[rownum, 5] = all_empty(varnames[[i]], groups[[j]], grades [[k]], regions[[l]])

      }
    }
  } 
}
output_all %>% as_data_frame() %>% View()

Any help/advice would be much appreciated!

标签: rloops

解决方案


If it's ok to use vectors and not lists, tidyr::crossing seems like a straightforward approach.

varnames <- c("beefpork", "breakfast", "breakfast_yn", "diet_soda", "food_label", "fruit_and_veggie", "fruit_juice", "fruits", "milk",                      "min_foods","regular_soda", "ssb", "total_fruit", "vegetables",                      "asthma", "bmiclass3", "bmiclass4","bmiclass5", "dental_absence",                     "dental_appt", "diabetes", "food_allergies", "sore_teeth", "trying_weight",                     "count_pa60days", "count_vigpa20days", "gaming_bedroom", "other_organized_pa", "pa30outdoor","paguidelines", "pc_time", "school_transport", "sport_teams", "tv_bedroom", "tv_time_char", "video_games_char")
grades <- c("2", "4", "8", "11")
groups <- c("none", "ethnic", "bordercounty")
regions <- c("state", "hsr")


tidyr::crossing(varnames, grades, groups, regions)


# A tibble: 864 x 4
   varnames grades groups       regions
   <chr>    <chr>  <chr>        <chr>  
 1 asthma   11     bordercounty hsr    
 2 asthma   11     bordercounty state  
 3 asthma   11     ethnic       hsr    
 4 asthma   11     ethnic       state  
 5 asthma   11     none         hsr    
 6 asthma   11     none         state  
 7 asthma   2      bordercounty hsr    
 8 asthma   2      bordercounty state  
 9 asthma   2      ethnic       hsr    
10 asthma   2      ethnic       state  

推荐阅读