I want to calculate the proportion of plan with respect to school type in the dataset below. Thing is, I have to first subset the dataset a couple of times. In this case, for example, I need to subset the dataset so that I only get schools which offer from level 0 up to level 04. How can I do that?
edit:
The way below is working, but I'm not filtering all schools that have all levels from level 0 up to level 04 , I'm getting the schools that have either of them. Ideas to that would be much appreciated.
data description:
# SCHOOL = name of the school
# Q9 = type of school
# Q11 = levels that the schools offer
# Q40 = types of planning that the school offers related to each level.
Note: All plannings are related to a school level. Hence, If I don't filter by the levels that each school offers and just calculate this columns' prop, my results will be misleading because each school can offer different level options (all of them, 1, 2 of them, etc)
my attempt:
### D) filter schools which only offer UP TO Level 04 ###
##################################### Quest40_2 ##########################
### check vector's names:
unique(quest40_2$Q11)
unwanted_4_2 <- quest40_2 %>%
filter(Q11 %in% c('level05','level06')) ### FILTER UNWANTED SCHOOL LEVELS
### create a vector with UNWANTED SCHOOL NAMES:
filter_vec_4_2 <- unique(unwanted_4_2$SCHOOL) ### get unique names (only school has 1 name)
### assign the original dataframe to a dummy data frame:
filtered_df_4_2 <- quest40_2
### loop over unwanted schools' names to remove them:
for (i in 1 :length(filter_vec_4_2)) {
filtered_df_4_2 <- filtered_df_4_2[!filtered_df_4_2$SCHOOL == filter_vec_4_2[i],]
}
I need to count how many times EACH 'plan' occurs by each type of 'school' (and get its proportion). Problem = each school can have more than one 'plan' type. Since each school can have more than one plan type, if we want to count the proportion of
plan types according to each school type, we cannot divide by n
b <- filtered_df_4_2 %>% drop_na(Q40) %>%
count(Q40, Q9) %>%
group_by(Q9)
Which leaves me to:
### counting the unique schools by each type of 'curriculo':
b2 <- b2 %>%
select(SCHOOL, Q9) %>%
unique() %>% count(Q9)
## join dataframes and get the proportion of schools that have each type of 'planejamento'
## within the curriculum types
c <- b %>% full_join(b2, by = 'Q9') %>%
mutate(prop = round((n.x/n.y *100), digits = 2)) %>%
select(-n.x, -n.y)
Q1 = I don't think I'm filtering it correctely since I'm not exclusively filtering the schools that offer alll levels up to 01 to 04. I guess I'm doing an 'or' not an 'and'
Q2 = Is there a way to avoid the loop with tidyverse ? Thanks in adv.
Ultimately, I'm trying to get the starts to => 'do the schools plan their agenda for the levels they offer or not and how does the type of school impact this?' (later this can be modeled, but now I just need percentages)
data:
dput(quest40_2)
structure(list(SCHOOL = structure(c("School1", "School1", "School1",
"School1", "School1", "School1", "School1", "School2", "School2",
"School2", "School2", "School2", "School2", "School2", "School3",
"School3", "School3", "School3", "School3", "School3", "School3",
"School3", "School3", "School4", "School4", "School4", "School4",
"School4", "School4", "School5", "School5", "School5", "School5",
"School5", "School5", "School6", "School6", "School6", "School6",
"School6", "School7", "School7", "School7", "School7", "School7",
"School7", "School8", "School8", "School8", "School8", "School9",
"School9", "School9", "School9", "School9", "School10", "School10",
"School10", "School10", "School10", "School11", "School11", "School11",
"School11", "School11", "School11", "School11", "School11", "School11",
"School12", "School12", "School12", "School12", "School12", "School12",
"School12", "School13", "School13", "School13", "School13", "School13",
"School13", "School13", "School13", "School13", "School13", "School14",
"School14", "School14", "School15", "School15", "School15", "School15",
"School15", "School15", "School16", "School16", "School16", "School16",
"School16", "School16", "School16", "School17", "School17", "School17",
"School17", "School17", "School18", "School18", "School18", "School18",
"School18", "School19", "School19", "School19", "School19", "School19",
"School19", "School20", "School20", "School20", "School21", "School21",
"School21", "School21", "School21", "School22", "School22", "School22",
"School22", "School22", "School23", "School23", "School23", "School23",
"School23", "School23", "School24", "School24", "School24", "School24",
"School24", "School24", "School25", "School25", "School25", "School25",
"School25", "School26", "School26", "School26", "School26", "School26",
"School26", "School26", "School26", "School26", "School27", "School27",
"School27", "School27", "School27", "School27", "School27", "School28",
"School28", "School28", "School28", "School28", "School28", "School28",
"School28", "School29", "School29", "School29", "School29", "School29",
"School29", "School30", "School30", "School30", "School30", "School30",
"School30", "School30", "School30", "School30", "School30", "School30",
"School31", "School31", "School31", "School31", "School31", "School31",
"School31", "School31", "School31", "School31", "School31", "School32",
"School32", "School32", "School32", "School32", "School32", "School32",
"School32", "School32", "School32", "School32", "School33", "School33",
"School33", "School33", "School33", "School33", "School33", "School34",
"School34", "School34", "School34", "School34", "School34", "School34",
"School34", "School35", "School35", "School35", "School35", "School36",
"School36", "School36", "School36", "School36", "School37", "School37",
"School37", "School37", "School37", "School37", "School37", "School37",
"School38", "School38", "School38", "School38", "School39", "School39",
"School39", "School39", "School39", "School39"), class = c("glue",
"character")), Q9 = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 3L, 3L,
3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 3L,
3L, 3L, 3L, 3L, 3L), .Label = c("typeA", "typeB", "typeC", "typeD"
), class = "factor"), Q11 = structure(c(3L, 7L, 4L, 2L, NA, NA,
NA, 7L, 4L, 2L, 1L, NA, NA, NA, 7L, 4L, 2L, 1L, 5L, NA, NA, NA,
NA, 4L, 2L, 1L, NA, NA, NA, 7L, 4L, 2L, 1L, 5L, NA, 7L, 4L, 2L,
1L, NA, 4L, 2L, 1L, 5L, 6L, NA, 4L, 2L, 1L, NA, 4L, 2L, 1L, 5L,
NA, 4L, 2L, 1L, 5L, NA, 4L, 2L, 1L, 5L, NA, NA, NA, NA, NA, 7L,
4L, 2L, 1L, NA, NA, NA, 7L, 4L, 2L, 1L, 5L, 6L, NA, NA, NA, NA,
5L, 6L, NA, 4L, 2L, 1L, 5L, 6L, NA, 4L, 2L, 1L, 5L, NA, NA, NA,
7L, 4L, 2L, 1L, NA, 7L, 4L, 2L, 1L, NA, 4L, 2L, 1L, NA, NA, NA,
2L, 1L, NA, 4L, 2L, 1L, 5L, NA, 4L, 2L, 1L, 5L, NA, 7L, 4L, 2L,
1L, NA, NA, 4L, 2L, 1L, NA, NA, NA, 4L, 2L, 1L, 5L, NA, 7L, 4L,
2L, 1L, 5L, NA, NA, NA, NA, 2L, 1L, 5L, NA, NA, NA, NA, 3L, 7L,
4L, 2L, 1L, 5L, 6L, NA, 4L, 2L, 1L, 5L, NA, NA, 7L, 4L, 2L, 1L,
5L, 6L, NA, NA, NA, NA, NA, 7L, 4L, 2L, 1L, 5L, 6L, NA, NA, NA,
NA, NA, 7L, 4L, 2L, 1L, 5L, 6L, NA, NA, NA, NA, NA, 7L, 4L, 2L,
1L, 5L, 6L, NA, 7L, 4L, 2L, 1L, 5L, NA, NA, NA, 3L, 7L, 4L, NA,
7L, 4L, 2L, NA, NA, 4L, 2L, 1L, 5L, NA, NA, NA, NA, 4L, 2L, 1L,
NA, 7L, 4L, 2L, 1L, 5L, NA), .Label = c("level04", "level03",
"level0", "level02", "level05", "level06", "level01"), class = "factor"),
Q40 = structure(c(NA, NA, NA, NA, 2L, 6L, 5L, NA, NA, NA,
NA, 2L, 6L, 5L, NA, NA, NA, NA, NA, 2L, 6L, 5L, 4L, NA, NA,
NA, 2L, 6L, 5L, NA, NA, NA, NA, NA, 1L, NA, NA, NA, NA, 1L,
NA, NA, NA, NA, NA, 1L, NA, NA, NA, 1L, NA, NA, NA, NA, 1L,
NA, NA, NA, NA, 1L, NA, NA, NA, NA, 2L, 6L, 5L, 4L, 1L, NA,
NA, NA, NA, 2L, 6L, 5L, NA, NA, NA, NA, NA, NA, 2L, 6L, 4L,
3L, NA, NA, 1L, NA, NA, NA, NA, NA, 1L, NA, NA, NA, NA, 2L,
6L, 5L, NA, NA, NA, NA, 2L, NA, NA, NA, NA, 1L, NA, NA, NA,
2L, 6L, 5L, NA, NA, 1L, NA, NA, NA, NA, 1L, NA, NA, NA, NA,
1L, NA, NA, NA, NA, 2L, 1L, NA, NA, NA, 2L, 6L, 5L, NA, NA,
NA, NA, 1L, NA, NA, NA, NA, NA, 2L, 6L, 5L, 4L, NA, NA, NA,
2L, 6L, 5L, 4L, NA, NA, NA, NA, NA, NA, NA, 2L, NA, NA, NA,
NA, 2L, 1L, NA, NA, NA, NA, NA, NA, 2L, 6L, 5L, 4L, 3L, NA,
NA, NA, NA, NA, NA, 2L, 6L, 5L, 4L, 3L, NA, NA, NA, NA, NA,
NA, 2L, 6L, 5L, 4L, 3L, NA, NA, NA, NA, NA, NA, 1L, NA, NA,
NA, NA, NA, 2L, 6L, 5L, NA, NA, NA, 2L, NA, NA, NA, 2L, 6L,
NA, NA, NA, NA, 2L, 6L, 5L, 4L, NA, NA, NA, 1L, NA, NA, NA,
NA, NA, 1L), .Label = c("none", "plan1_level0upto02", "plan5_level_05",
"plan4_level_05", "plan3_level_04", "plan2_level_03"), class = "factor")), row.names = c(NA,
-253L), class = c("tbl_df", "tbl", "data.frame"))
This might be what you need or give you a good start. It shows the number of plans per type with levels 0-4, also shows the percentages.
left_join(na.omit(quest40_2[, c("SCHOOL", "Q9", "Q11")]),
na.omit(quest40_2[, c("SCHOOL", "Q40")]), c("SCHOOL"),
multiple = "all") %>%
group_by(Q9, Q40) %>%
filter(sub(".*(\\d+)$", "\\1", Q11) <= 4) %>%
summarize(n = n(), .groups = "drop") %>%
mutate(percentage = n / sum(n) * 100) %>%
print(n = Inf)
# A tibble: 23 × 4
Q9 Q40 n percentage
<fct> <fct> <int> <dbl>
1 typeA none 3 0.974
2 typeA plan1_level0upto02 6 1.95
3 typeA plan5_level_05 4 1.30
4 typeA plan4_level_05 6 1.95
5 typeA plan3_level_04 6 1.95
6 typeA plan2_level_03 6 1.95
7 typeB none 21 6.82
8 typeB plan1_level0upto02 47 15.3
9 typeB plan5_level_05 4 1.30
10 typeB plan4_level_05 14 4.55
11 typeB plan3_level_04 31 10.1
12 typeB plan2_level_03 31 10.1
13 typeC none 32 10.4
14 typeC plan1_level0upto02 21 6.82
15 typeC plan4_level_05 4 1.30
16 typeC plan3_level_04 15 4.87
17 typeC plan2_level_03 18 5.84
18 typeD none 3 0.974
19 typeD plan1_level0upto02 8 2.60
20 typeD plan5_level_05 8 2.60
21 typeD plan4_level_05 8 2.60
22 typeD plan3_level_04 4 1.30
23 typeD plan2_level_03 8 2.60
As a follow up on a recent SO question (see here) I am wondering how to perform multiple t.tests in R with weighted data (package srvyr). I cant make it run and would be happy if anyone could help me here. I added a random sample in the code below.
Many thanks!
#create data
surveydata <- as.data.frame(replicate(1,sample(1:5,1000,rep=TRUE)))
colnames(surveydata)[1] <- "q1"
surveydata$q2 <- sample(6, size = nrow(surveydata), replace = TRUE)
surveydata$q3 <- sample(6, size = nrow(surveydata), replace = TRUE)
surveydata$q4 <- sample(6, size = nrow(surveydata), replace = TRUE)
surveydata$group <- c(1,2)
#replace all value "6" wir NA
surveydata[surveydata == 6] <- NA
#add NAs to group 1 in q1
surveydata$q1[which(surveydata$q1==1 & surveydata$group==1)] = NA
surveydata$q1[which(surveydata$q1==2 & surveydata$group==1)] = NA
surveydata$q1[which(surveydata$q1==3 & surveydata$group==1)] = NA
surveydata$q1[which(surveydata$q1==4 & surveydata$group==1)] = NA
surveydata$q1[which(surveydata$q1==5 & surveydata$group==1)] = NA
#add weights
surveydata$weights <- round(runif(nrow(surveydata), min=0.2, max=1.5), 3)
#create vector for relevant questions
rquest <- names(surveydata)[1:4]
# create survey design
library(srvyr)
surveydesign <- surveydata %>%
as_survey_design(strata = group, weights = weights, variables = c("group", all_of(rquest)))
# perform multiple t.test (doesn't work yet)
outcome <- do.call(rbind, lapply(names(surveydesign$variables)[-1], function(i) {
tryCatch({
test <- t.test(as.formula(paste(i, "~ survey")), data = surveydesign)
data.frame(question = i,
group1 = test$estimate[1],
group2 = test$estimate[2],
difference = diff(test$estimate),
p_value = test$p.value, row.names = 1)
}, error = function(e) {
data.frame(question = i,
group1 = NA,
group2 = NA,
difference = NA,
p_value = NA, row.names = 1)
})
}))
As I understand it you have a series of question columns in the example q1 to q4. You've used srvyr to generate a weights column. It is possible in our data that for a particular question one entire group maybe all NA and you'd like to generate results into a df even when that is true. You want a weighted Student's t-test making use of the weights column not a simple t-test. The only function I found that provides that is weights::wtd.t.test which doesn't offer a formula interface but wants to be fed vectors.
In order of steps taken:
Load requisite libraries
library(srvyr)
library(dplyr)
library(rlang)
library(weights)
Build a custom function that removes the NAs by variable, pulls the vectors for x, y, weightx, weighty, runs the test, and extracts the info you want into a df row.
multiple_wt_ttest <- function(i) {
i <- ensym(i)
xxx <- surveydata %>%
filter(!is.na(!!i)) %>%
split(.$group)
newx <- pull(xxx[[1]], i)
newy <- pull(xxx[[2]], i)
wtx <- pull(xxx[[1]], weights)
wty <- pull(xxx[[2]], weights)
test <- wtd.t.test(x = newx,
y = newy,
weight = wtx,
weighty = wty,
samedata = FALSE)
data.frame(question = rlang::as_name(i),
group1 = test$additional[[2]],
group2 = test$additional[[3]],
difference = test$additional[[1]],
p.value = test$coefficients[[3]])
}
Once we have the function we can use lapply to apply it column by column (notice it handles the case in q2 where group == 1 is all NA.
lapply(names(surveydata)[1:4], multiple_wt_ttest)
#> [[1]]
#> question group1 group2 difference p.value
#> 1 q1 NaN 3.010457 NaN NA
#>
#> [[2]]
#> question group1 group2 difference p.value
#> 1 q2 3.009003 3.071842 -0.06283922 0.515789
#>
#> [[3]]
#> question group1 group2 difference p.value
#> 1 q3 2.985096 2.968867 0.0162288 0.8734034
#>
#> [[4]]
#> question group1 group2 difference p.value
#> 1 q4 2.856255 3.047787 -0.1915322 0.04290471
Finally, wrap it in a do.call and rbind to make the df you desire
do.call(rbind, lapply(names(surveydata)[1:4], multiple_wt_ttest))
#> question group1 group2 difference p.value
#> 1 q1 NaN 3.010457 NaN NA
#> 2 q2 3.009003 3.071842 -0.06283922 0.51578905
#> 3 q3 2.985096 2.968867 0.01622880 0.87340343
#> 4 q4 2.856255 3.047787 -0.19153218 0.04290471
Your data (without showing all the gyrations to create it and heading the first 200 rows)
surveydata <-
structure(list(q1 = c(NA, 1L, NA, 4L, NA, 5L, NA, 3L, NA, 5L,
NA, 5L, NA, 1L, NA, 5L, NA, 3L, NA, 4L, NA, 5L, NA, 4L, NA, 2L,
NA, 5L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 5L,
NA, 4L, NA, 4L, NA, 3L, NA, 4L, NA, 2L, NA, 4L, NA, 3L, NA, 1L,
NA, 1L, NA, 3L, NA, 5L, NA, 3L, NA, 5L, NA, 5L, NA, 4L, NA, 2L,
NA, 5L, NA, 1L, NA, 3L, NA, 2L, NA, 5L, NA, 4L, NA, 1L, NA, 5L,
NA, 2L, NA, 2L, NA, 4L, NA, 1L, NA, 3L, NA, 4L, NA, 5L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L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("1", "2"), class = "factor"),
weights = c(1.445, 0.408, 0.621, 0.961, 1.492, 0.625, 1.131,
0.246, 0.612, 0.621, 1.311, 0.649, 1.282, 0.898, 1.268, 0.641,
0.764, 0.759, 0.306, 0.707, 0.899, 0.785, 1.279, 0.458, 0.882,
0.384, 1.492, 0.468, 0.785, 0.707, 0.489, 1.113, 0.692, 0.293,
0.642, 1.327, 0.362, 1.405, 1.173, 0.732, 0.661, 0.522, 1.001,
0.374, 1.181, 0.819, 1.389, 0.43, 0.477, 0.879, 0.634, 0.417,
0.359, 1.007, 0.866, 0.203, 1.469, 0.294, 1.326, 1.391, 0.871,
1.036, 1.251, 0.417, 1.074, 1.268, 0.963, 0.469, 0.215, 1.074,
0.644, 1.054, 0.787, 0.714, 0.568, 0.397, 1.421, 0.692, 0.262,
0.644, 0.793, 0.808, 0.25, 0.842, 1.039, 0.359, 0.987, 1.257,
0.301, 0.203, 0.823, 1.328, 1.192, 0.256, 1.099, 0.668, 1.129,
0.413, 0.266, 1.121, 0.893, 1.484, 0.568, 1.255, 0.531, 0.461,
0.773, 0.298, 0.233, 0.676, 0.478, 0.806, 0.556, 0.201, 0.801,
0.348, 1.396, 0.552, 0.384, 0.615, 0.499, 0.819, 0.954, 0.943,
0.956, 0.323, 0.706, 0.699, 0.9, 1.156, 1.436, 1.115, 0.762,
0.258, 1.421, 0.644, 1.349, 0.251, 0.735, 0.479, 1.055, 1.395,
1.062, 1.155, 0.869, 0.436, 0.415, 0.745, 1.247, 0.21, 0.879,
0.776, 0.747, 0.835, 0.609, 0.733, 0.563, 1.067, 1.436, 0.679,
1.497, 1.385, 1.087, 1.286, 0.503, 0.738, 0.504, 0.665, 1.421,
1.288, 0.691, 0.972, 0.467, 0.425, 0.406, 0.862, 0.749, 0.935,
0.291, 0.444, 1.118, 1.048, 0.886, 0.982, 0.578, 1.402, 0.778,
1.139, 0.804, 0.618, 1.147, 0.594, 0.984, 0.986, 0.941, 0.794,
0.323, 1.41, 0.902, 0.417)), row.names = c(NA, 200L), class = "data.frame")
I am writing a function, which is using references from two data sets (RawDataSplit and Hierarchy). All these datasets are connected. I need to summarise the data based on a function that will be applied to all the selected columns. I am using summarise_ to do this.
I have created a function which will be used to summarise the columns per_qid, this is to calculate percentage for each column.
Then I am creating a function with three arguments: fav_fun(dataset,levelname,demo_var)
dataset is for the data on which we need aggregations, in this case it is RawDataSplit
levelname is one of the column in RawDataSplit by which data would be grouped
demo_var would be one of the variable chosen from data set 'Hierarchy', this would be one of the demographic variable like Age.
Function for aggregation:
per_qid<-function(x){
counts<-sum(ifelse(x==4|x==5,1,0),na.rm=TRUE
total<-sum(ifelse(x==1|x==2|x==3|x==4|x==5,1,0),na.rm=TRUE)
#Masking and rounding
x<- ifelse(total<masking_criteria,NA,roundUp(counts*100/total)) )
x
}
Function to aggregate RawDataSplit file:
fav_fun<-function(dataset,levelname,demo_var){
demo_var<- enquo(demo_var)
levelname<-enquo(levelname)
demographic<-Hierarchy[,demo_var]
demo<-levels(demographic)
summ_method<- paste('per_qid(',questions,')')
summ_name<-paste0(questions,'_',demographic)
fav <- dataset %>%
group_by(!!levelname) %>%
filter(status=="Complete") %>%
filter(!!demo_var==demo[1]) %>%
summarise_(.dots = setNames,(summ_method,summ_name))
fav
}
this fav_fun is throwing different kind of errors sometimes for summarise_ and sometimes for other variables. Maybe something is wrong with the quoting and enquoting of variable also.
The sample data for these datasets are:
RawDataSplit:
structure(list(QID_1 = c(4L, 3L, 5L, 2L, 5L, 5L), QID_2 = c(4L,
3L, 5L, 2L, 5L, 5L), QID_3 = c(4L, 4L, 4L, 6L, 5L, 5L), mastercode_1 = c(NA,
NA, NA, NA, NA, NA), mastercode_2 = structure(c(1L, 1L, 1L, 1L,
1L, 1L), .Label = "BROLLAND", class = "factor")), row.names = c(NA,
-6L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x00372498>)
questions:
c("QID_1", "QID_10", "QID_11")
Hierarchy : (for now I have included one column only which is Age, I need to use it as demo_var in the function)
structure(c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 4L, 4L,
NA, 4L, NA, 3L, 3L, 2L, 4L, 3L, 3L, 3L, 4L, 5L, 4L, NA, NA, 3L,
3L, 3L, 5L, 3L, 3L, 4L, NA, 4L, 4L, 3L, 3L, 4L, 3L, 4L, 3L, 4L,
4L, NA, 4L, 3L, 4L, 3L, 4L, 4L, NA, 5L, 4L, 3L, 4L, 3L, 4L, 3L,
NA, NA, 3L, NA, 3L, 4L, 3L, 3L, NA, 4L, 4L, 4L, 4L, 3L, 4L, 3L,
2L, 2L, 3L, 4L, 4L, 2L, 2L, 4L, 2L, 2L, 4L, 2L, 2L, 4L, 3L, 2L,
3L, 4L, 4L, 3L, 4L, NA, 2L, 2L, 3L, 3L, 1L, 2L, 2L, NA, 3L, NA,
NA, NA, 4L, 4L, 3L, 4L, 3L, 3L, 4L, 2L, 5L, 4L, 2L, NA, 4L, 2L,
4L, 2L, NA, 4L, NA, 4L, 3L, 4L, 4L, 4L, 3L, 4L, NA, 3L, 3L, 3L,
3L, 5L, 3L, 3L, 2L, 4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 4L, 2L, NA, 3L, NA, 3L, NA, 3L, 4L, 4L,
3L, 4L, 3L, 2L, 4L, 5L, 3L, 3L, NA, 3L, NA, NA, NA, NA, 2L, NA,
NA, NA, NA, NA, NA, 3L, NA, 2L, NA, 4L, NA, 3L, NA, NA, 3L, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 4L, NA,
2L, NA, NA, NA, NA, 3L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, 3L, NA, NA, NA, NA, 2L, NA, 2L, 2L, NA, 3L, NA, NA, NA, NA,
NA, NA, NA, 2L, NA, 2L, 3L, NA, 2L, NA, 2L, 3L, 3L, NA, 2L, 4L,
3L, NA, NA, NA, NA, NA, NA, NA, 2L, NA, NA, NA, NA, NA, NA, NA,
3L, NA, NA, NA, 4L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, 2L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, 4L, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, 2L, NA, NA, 2L, 4L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
2L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 2L, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 3L,
NA, 3L, 3L, 2L, 2L, NA, 3L, 1L, NA, NA, NA, 4L, 2L, NA, NA, NA,
NA, NA, 5L, NA, NA, NA, NA, 2L, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, 4L, NA, NA, NA, NA, NA, NA, NA, 5L, NA, NA, NA, NA, NA,
NA, NA, NA, NA, 2L, NA, NA, NA, NA, 2L, NA, NA, NA, 2L, NA, NA,
NA, NA, 3L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 2L, 4L, NA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, NA, NA, NA, NA, 3L, NA, 4L, 3L, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, 4L, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 5L, NA, NA, 4L, 4L, NA,
NA, NA, NA, NA, 4L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
4L, NA, NA, 3L, 3L, NA, 3L, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, 4L, NA, NA, NA, NA, 4L, 4L, 3L, 3L, NA, NA, NA, NA, 3L, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, 4L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
3L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, 4L, NA, NA, NA, NA, 3L, NA, 3L, NA, 4L, 3L, NA, NA,
NA, 4L, NA, 3L, NA, NA, NA, 4L, 3L, NA, NA, 3L, 3L, NA, NA, NA,
NA, NA, NA, 3L, NA, NA, 4L, NA, 5L, NA, NA, 4L, NA, NA, NA, NA,
NA, 5L, 4L, NA, 5L, NA, 2L, 2L, NA, NA, NA, NA, NA, NA, NA, NA,
5L, NA, NA, NA, NA, NA, NA, 5L, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, 5L, NA, 3L, 2L, NA, 5L, NA, NA, NA, NA, NA, NA,
NA, 2L, 2L, NA, 3L, NA, 4L, NA, 4L, 5L, 4L, 4L, NA, NA, 4L, NA,
NA, 5L, 4L, NA, NA, 3L, 5L, NA, 5L, NA, NA, NA, 3L, 3L, 5L, 5L,
3L, 3L, NA, 2L, 1L, 5L, 5L, NA, 4L, 5L, 5L, NA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4L, 4L, 4L, 5L, 3L, 5L, 5L, 3L, 3L, 5L, 5L,
4L, 4L, 3L, 5L, 4L, NA, NA, 4L, NA, 3L, NA, NA, 4L, 3L, 4L, 3L,
3L, 1L, 3L, 2L, 3L, 5L, NA, NA, 1L, NA, 3L, NA, NA, 4L, 4L, NA,
NA, 3L, 2L, 3L, NA, NA, 3L, NA, 4L, 4L, 5L, NA, 4L, 3L, 5L, 5L,
4L, 5L, 4L, 4L, 4L, 5L, NA, 5L, 4L, 5L, NA, 4L, 3L, 4L, 4L, 4L,
4L, NA, 5L, NA, 5L, 5L, 2L, 5L, 4L, 4L, NA, 5L, 4L, 4L, 4L, 4L,
NA, 2L, NA, NA, 3L, 5L, 5L, 4L, 4L, 5L, 5L, 3L, 3L, 5L, 4L, 3L,
2L, 5L, 3L, 5L, 5L, 4L, 5L, 2L, 2L, 3L, 4L, 3L, 3L, 3L, 2L, 2L,
4L, 3L, 2L, 2L, 4L, 5L, 4L, 1L, 1L, 2L, 1L, 2L, 4L, 2L, 2L, 5L,
NA, 3L, 2L, 1L, 4L, 4L, 3L, 3L, 2L, 2L, 1L, 3L, 3L, NA, 3L, 3L,
3L, 4L, 3L, 4L, 1L, 2L, 4L, 3L, 4L, 1L, 2L, 2L, 4L, 2L, 2L, 3L,
2L, 5L, 5L, 3L, 5L, NA, 3L, NA, 3L, 5L, 4L, 4L, 5L, 3L, 3L, 3L,
4L, 3L, 3L, 4L, 5L, 4L, 4L, 4L, NA, NA, 4L, NA, 4L, 5L, 4L, 3L,
5L, 3L, 4L, 3L, 4L, NA, 3L, NA, 3L, 3L, NA, NA, 4L, 4L, NA, NA,
NA, NA, NA, NA),
.Label = c("18 to less than 25 years old", "25 to less than 35 years old",
"35 to less than 45 years old", "45 to less than 55 years old",
"More than 55 years old"), class = "factor")
I need a function fav_fun which takes these three arguments and aggregates the RawDataSplit and modifies names too. Like for QID_1 the new name after aggregation would be QID_1_45 to less than 55 years old for level 1 of Age variable
function will be called as: fav_fun(RawDataSplit,mastercode_2,Age)