For a sample dataframe comprising a factor:
library(dplyr)
group1 <-c(TRUE, FALSE, FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE, TRUE, FALSE, FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE)
variable_a <- c(1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3)
variable_b <- c( 5, 8, 6, 8, 4, 5, 9, 2, 4, 5, 8, 6, 8, 4, 5, 9, 2, 4)
df <- data.frame(group1, variable_a, variable_b)
df$group1 <- as.factor(group1)
I want to use aggregate to summarise by group1:
table_aggregate <- df %>% group_by(group1) %>%
summarise(sum_variable_a = sum(variable_a),
sum_variable_b = sum(variable_b))
How do I tell R I want the sums by TRUE and FALSE and not aggregated together? (i.e. I want the sums of all variable_a and variable_b values for TRUE and FALSE on different rows. Currently I just have '1' in the margins and all the values are summed together).