I want to reduce lines of code to group different variables and add without removing the variables from my base.
This is what I did (It works but the code is a bit long)
test1
names(test1)
#[1] "id" "DPA_DESPAR"
#[3] "DPA_VALOR" "DPA_ANIO"
#[5] "DPA_CANTON" "DPA_DESCAN"
#[7] "DPA_PROVIN" "DPA_DESPRO"
#...
#[19] "exp_omega" "total_empl"
#...
#[23] "geometry"
ss1= test1 %>%
group_by(DPA_CANTON) %>%
summarise(tot1= sum(total_empl))
ss2 = test1 %>%
group_by(id) %>%
summarise(tot2 = sum(total_empl))
test1 = left_join(test1, ss1, by = 'DPA_CANTON') %>%
left_join(., ss2, by = 'id')
test1[1:10,21:22]
#tot1 tot2
#1 37781 23049
#2 37781 1456
#3 37781 1198
#4 37781 2253
#5 37781 800
#6 37781 1705
#7 37781 615
#8 37781 4663
#9 37781 2042
#10 6593 5022
This does not work
I used this
aggregate(test1$total_empl, by=list(Category=test1[c(1,5)]), FUN=sum)
#Error in aggregate.data.frame(as.data.frame(x), ...) :
# arguments must have same length
Data
test1 = structure(list(id= c("020150", "020151",
"020153", "020155", "020156", "020157", "020158", "020159", "020160",
"020250", "020251", "020350", "020351", "020353", "020354", "020450",
"020550", "020551", "020552", "020553"), DPA_CANTON = c("0201",
"0201", "0201", "0201", "0201", "0201", "0201", "0201", "0201",
"0202", "0202", "0203", "0203", "0203", "0203", "0204", "0205",
"0205", "0205", "0205"), total_empl= c(23049, 1456,
1198, 2253, 800, 1705, 615, 4663, 2042, 5022, 1571, 2481, 1077,
973, 441, 4689, 4884, 875, 1046, 341)), row.names = c(NA, 20L
), class = "data.frame")