I am trying to produce a table of mean scores for each participant in my tibble. The number of observants is much larger than the data given below, but this tibble should be sufficient. I need to produce a table for each unique user_id. I would like the table to have 10 rows, 8 of which are the means of the indicators 1-8 per timepoint, and the other two are domain means per timepoint. The mean of domain 0 is the mean of indicators 1-4, and the mean of domain 1 is the mean of indicators 5-8. I would also like the outputted tables to have four columns, one per timepoint. Thus, each teacher_id's outputted table should be a 10 by 4. I have attempted this with tidyverse and would appreciate help. Also, some users (read several) will not have values at all timepoints.
structure(list(Group = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1), user_id = c("Kim", "Kim",
"Kim", "Kim", "Kim", "Kim", "Kim",
"Kim", "Bob", "Bob", "Bob", "Bob",
"Bob", "Bob", "Bob", "Bob", "Bob",
"Bob", "Bob", "Bob", "Bob", "Bob",
"Bob", "Bob", "George", "George", "George", "George",
"George", "George", "George", "George", "George", "George", "George",
"George", "George", "George", "George", "George"), indicator = c("1",
"2", "3", "4", "5", "6", "7", "8", "1", "1", "2", "2", "3", "3",
"4", "4", "5", "5", "6", "6", "7", "7", "8", "8", "1", "1", "2",
"2", "3", "3", "4", "4", "5", "5", "6", "6", "7", "7", "8", "8"
), Timepoint = c(1, 1, 1, 1, 1, 1, 1, 1, 3, 4, 3, 4, 3,
4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4,
3, 4, 3, 4, 3, 4), score = c(3.5, 3.5, 2, 3, 3.5, 4,
3, 4, 2, 3, 2.5, 3, 1.5, 1.5, 0.5, 3, 2, 4, 2.5, 4, 2.5, 3.5,
3, 3.5, 3.5, 3, 2.5, 2.5, 2.5, 2, 2, 3, 3.5, 3.5, 3.5, 3.5, 3,
3, 3, 2.5)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-40L))
Attempted tidyverse code:
user_tables <- d %>%
group_by(user_id,indicator,Timepoint) %>%
summarise(Time1 = mean[which(indicator == 1 & Timepoint == 1)], mean[which(indicator == 2 & Timepoint == 1)], mean[which(indicator == 3 & Timepoint == 1)], mean[which(indicator == 4 & Timepoint == 1)], mean[which(indicator == 5 & Timepoint == 1)], mean[which(indicator == 6 & Timepoint == 1)], mean[which(indicator == 7 & Timepoint == 1)], mean[which(indicator == 8 & Timepoint == 1)],
Time2 = mean[which(indicator == 1 & Timepoint == 2)], mean[which(indicator == 2 & Timepoint == 2)], mean[which(indicator == 3 & Timepoint == 2)], mean[which(indicator == 4 & Timepoint == 2)], mean[which(indicator == 5 & Timepoint == 2)], mean[which(indicator == 6 & Timepoint == 2)], mean[which(indicator == 7 & Timepoint == 2)], mean[which(indicator == 8 & Timepoint == 2)],
Time3 = mean[which(indicator == 1 & Timepoint == 3)], mean[which(indicator == 2 & Timepoint == 3)], mean[which(indicator == 3 & Timepoint == 3)], mean[which(indicator == 4 & Timepoint == 3)], mean[which(indicator == 5 & Timepoint == 3)], mean[which(indicator == 6 & Timepoint == 3)], mean[which(indicator == 7 & Timepoint == 3)], mean[which(indicator == 8 & Timepoint == 3)],
Time4 = mean[which(indicator == 1 & Timepoint == 4)], mean[which(indicator == 2 & Timepoint == 4)], mean[which(indicator == 3 & Timepoint == 4)], mean[which(indicator == 4 & Timepoint == 4)], mean[which(indicator == 5 & Timepoint == 4)], mean[which(indicator == 6 & Timepoint == 4)], mean[which(indicator == 7 & Timepoint == 4)], mean[which(indicator == 8 & Timepoint == 4)]) %>%
split(., .$user_id)
Ultimately, I would like a table like this per user (where the NAs are the appropriate means) (Note: This one is for Bob - he didn't have scores for time 1 or time 2):
structure(c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, 1.625, 2, 2.5, 1.5, 0.5, 2.5, 2,
2.5, 2.5, 3, 2.625, 3, 3, 1.5, 3, 3.75, 4, 4, 3.5, 3.5), .Dim = c(10L,
4L), .Dimnames = list(c("Domain 0", "Ind 1", "Ind 2", "Ind 3",
"Ind 4", "Domain 1", "Ind 5", "Ind 6", "Ind 7", "Ind 8"), c("Time 1",
"Time 2", "Time 3", "Time 4")))
Thank you!