1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| library("dplyr")
ex <- data.frame(region = rep(c("region1", "region2"), c(2L, 2L)),
g = rep(c("g1", "g2"), 2L),
val = c(35L, 40L, 10L, 20L),
stringsAsFactors = FALSE)
res <- vector(mode = "list", length = 3L)
names(res) <- c(unique(ex$region), "somme")
res$region1 <- dplyr::filter(ex, region == "region1") %>% dplyr::select(g, val)
res$region2 <- dplyr::filter(ex, region == "region2") %>% dplyr::select(g, val)
tmp <- group_by(ex, region) %>% group_by(g) %>% do(somme = sum(.$val))
names(tmp$somme) <- tmp$g
res$somme <- tmp$somme |
Partager