I would like to be able to use a condition to filter what gets summarised in summarise_at calls. I know this can be done when calling dplyr:: summarise() directly but I would like to avoid having to specify this for each variable.
I have provided the below code thinking that this call to summarise_at would give me the correct data. But it does not. I would like the code to return the data set labelled final data. Where there is a row for each individual with each variable summed ONLY when the row matches with the filtering condition. When it does not match or there is nothing to sum I would like it to return 0 or NA.
Data = tibble(Group = c(1,1,1,1,1,1,1,1),
Week = c(1,2,3,4,1,2,3,4),
Condition = c(1,1,1,0,0,1,1,0),
Var1 = 1:8,
Var2 = 9:16,
Var3 = 17:24)
Data %>%
group_by(Group, Week, Condition) %>%
summarise_at(vars(Var1, Var2, Var3), ~sum(., na.rm = T)) %>%
ungroup()
Final.Data = tibble(Group = c(1,1,1,1),
Week = c(1,2,3,4),
Var1 = c(1,8,10,0),
Var2 = c(9,24,26,0),
Var3 = c(17,40,42,0))