I'm struggling with what I imagine is a multi-level sampling procedure in R. Let's say I have a dataset composed of a very biased sampling method. Therefore, the results obtained with the participants are biased. I would like to adjust the dataset to match two demographic variables (sex and age), which are coded as factor in the dataset. The following image described the situation.
I assume that I'll need to perform a "loop" calculation. As an example: to adjust the sample size of the first age interval (15-19), I'll need to define a new total in which this final sample fits the 50% 50% definition. The same procedure will be needed for all other age intervals.
That's the most related topic I've found.
x<-structure(list(age_cat = c("25-29", "30-34", "25-29", "20-24",
"25-29", "20-24", "35-39", "30-34", "25-29", "30-34", "25-29",
"30-34", "35-39", "45-49", "40-45", "20-24", "20-24", "25-29",
"35-39", "35-39", "25-29", "20-24", "30-34", "30-34", "40-45",
"25-29", "25-29", "25-29", "20-24", "40-45", "20-24", "40-45",
"30-34", "25-29", "45-49", "30-34", "45-49", "40-45", "25-29",
"35-39", "40-45", "25-29", "45-49", "35-39", "45-49", "40-45",
"20-24", "45-49", "40-45", "25-29", "35-39", "30-34", "30-34",
"25-29", "20-24", "20-24", "40-45", "35-39", "25-29", "25-29",
"20-24", "40-45", "20-24", "20-24", "45-49", "20-24", "35-39",
"20-24", "35-39", "45-49", "15-19", "45-49", "35-39", "35-39",
"30-34", "35-39", "45-49", "35-39", "30-34", "20-24", "35-39",
"40-45", "40-45", "40-45", "30-34", "45-49", "20-24", "30-34",
"45-49", "35-39", "20-24", "20-24", "20-24", "45-49", "20-24",
"45-49", "35-39", "25-29", "40-45", "40-45", "25-29", "35-39",
"45-49", "30-34", "45-49", "45-49", "45-49", "15-19", "30-34",
"45-49", "30-34", "30-34", "35-39", "25-29", "40-45", "15-19",
"20-24", "20-24", "40-45", "40-45", "45-49", "45-49", "35-39",
"40-45", "30-34", "35-39", "35-39", "25-29", "25-29", "20-24",
"20-24", "40-45", "20-24", "35-39", "20-24", "20-24", "30-34",
"25-29", "45-49", "25-29", "35-39", "20-24", "35-39", "35-39",
"35-39", "40-45", "35-39", "35-39", "20-24", "30-34", "25-29",
"15-19", "30-34", "35-39", "15-19", "20-24", "20-24", "35-39",
"25-29", "25-29", "25-29", "25-29", "30-34", "40-45", "35-39",
"30-34", "35-39", "40-45", "25-29", "30-34", "25-29", "25-29",
"45-49", "30-34", "30-34", "25-29", "15-19", "25-29", "20-24",
"15-19", "20-24", "30-34", "20-24", "40-45", "25-29", "25-29",
"30-34", "30-34", "25-29", "20-24", "40-45", "45-49", "25-29",
"25-29", "40-45", "35-39", "25-29", "45-49", "35-39", "30-34",
"45-49", "30-34", "30-34", "45-49", "35-39", "20-24", "45-49",
"30-34", "25-29", "45-49", "45-49", "40-45", "25-29", "20-24",
"40-45", "30-34", "35-39", "30-34", "20-24", "35-39", "20-24",
"30-34", "20-24", "35-39", "35-39", "30-34", "45-49", "40-45",
"45-49", "25-29", "35-39", "40-45", "30-34", "35-39", "30-34",
"35-39", "20-24", "25-29", "35-39", "30-34", "30-34", "25-29",
"45-49", "45-49", "40-45", "40-45", "35-39", "30-34", "25-29",
"35-39", "20-24", "40-45", "20-24", "30-34", "40-45", "20-24",
"45-49", "20-24", "40-45", "25-29", "40-45", "25-29", "45-49",
"30-34", "30-34", "45-49", "40-45", "30-34", "30-34", "20-24",
"20-24", "35-39", "30-34", "15-19", "35-39", "25-29", "45-49",
"30-34", "25-29", "35-39", "15-19", "40-45", "45-49", "15-19",
"35-39", "45-49", "45-49", "25-29"), sex_cat = structure(c(1L,
2L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L,
2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L,
2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 1L,
1L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L,
1L,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