0

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).

Jon Spring
  • 55,165
  • 4
  • 35
  • 53
KT_1
  • 8,194
  • 15
  • 56
  • 68

1 Answers1

1

as per your code , you are already getting the output as below. Please let me if this is the correct output, if you are looking for a different output then please let me know.

table_aggregate <- df %>% group_by(group1) %>% 
  summarise(sum_variable_a = sum(variable_a),
            sum_variable_b = sum(variable_b)) 

# output 
# A tibble: 2 × 3
  group1 sum_variable_a sum_variable_b
  <fct>           <dbl>          <dbl>
1 FALSE              22             46
2 TRUE               14             56

jkatam
  • 2,691
  • 1
  • 4
  • 12
  • 1
    Many thanks @jkatam - my output is a single dataframe 1 obs of 2 variables, with no group1 (just a '1' in the margins'). I had to run detach(package:plyr) and it works! – KT_1 Jun 27 '23 at 20:22