Is there any possibility to get expected result in simplest way (without intermediate steps, as I did in my code)? I was looking for it and couldn't find any solution...
So, for example:
df <- data.frame( A = c("01","01","01","01","01","01","02","02","02","02","02","02"),
B = c("12","12","12","12","14","14","18","18","18","22","22","22"),
C = c("01","01","02","02","01","02","01","02","05","01","02","02"),
D = c("1","2","1","2","2","1","2","1","3","2","1","3"),
value_1 = c(25,14,1,15,0,15,0,16,18,74,112,36),
value_2 = c(2,0,48,12,0,47,95,14,188,65,14,47),
value_3 = c(78,5,14,26,9,0,4,0,0,8,0,4))
I would like to summarise values for each level (A,B,C,D) and combination of them to show the sum of each level in the final table.
A <- df %>%
group_by(A) %>%
summarise(across(value_1:value_3, sum))
B <- df %>%
group_by(A,B) %>%
summarise(across(value_1:value_3, sum))
C <- df %>%
group_by(A,B,C) %>%
summarise(across(value_1:value_3, sum))
AD <- df %>%
group_by(A,D) %>%
summarise(across(value_1:value_3, sum))
BD <- df %>%
group_by(A,B,D) %>%
summarise(across(value_1:value_3, sum))
result <-bind_rows(A, B, C, AD, BD)
result[is.na(result)] <- "00"
result <- result[,c("A","B","C","D", "value_1", "value_2", "value_3")]
result <- result[order(result$A, result$B, result$C, result$D),]
My data has much more levels, so how could I do it without making intermediate steps like: B, C.. to get the result like this:
> head(result)
# A tibble: 6 x 7
A B C D value_1 value_2 value_3
1 01 00 00 00 70 109 132
2 01 00 00 1 41 97 92
3 01 00 00 2 29 12 40
4 01 12 00 00 55 62 123
5 01 12 00 1 26 50 92
6 01 12 00 2 29 12 31
Is there any possibility to do it with .groups argument or using aggregate function instead of combination group_by and summarise?
JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)