combine two looping structures to obtain a matrix output
问题 I'm using two closely related formulas in R. I was wondering if it might be possible to combine B1 and B2 to get my desired matrix output shown below? z <- "group y1 y2 1 1 2 3 2 1 3 4 3 1 5 4 4 1 2 5 5 2 4 8 6 2 5 6 7 2 6 7 8 3 7 6 9 3 8 7 10 3 10 8 11 3 9 5 12 3 7 6" dat <- read.table(text = z, header = T) (B1 = Reduce("+", group_split(dat, group, .keep = FALSE) %>% map(~ nrow(.)*(colMeans(.)-colMeans(dat[-1]))^2))) # y1 y2 #61.86667 19.05000 (B2 = Reduce("+",group_split(dat, group, .keep =