r - 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!
解决方案
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
推荐阅读
- html - Fontawesome webfont 在内容属性上显示正方形(在本地工作正常并卡在 prod 环境中)
- java - 从类中获取信息以在应用程序中运行
- c# - Groupby Lambda 表达式
- python - CSV 到嵌套(分层)JSON - 使用 Python 标记父母
- ionic-framework - 无法水平滚动离子卡
- jquery - jQuery - 实时更新长度,而不是仅在页面加载时
- wordpress - Wordpress 主题版权编辑(页脚)
- tfs - TFS 开发运维。找不到外部项目文件
- android - 一个文本视图,字符串最多再换行
- php - Amazon Connect 从 Lambda 函数和 PHP 接收 JSON 数据