I have a data matrix (dfA) with several columns: province (dfA is a subset for one of the provinces), municipality, crop, number of farms, area, production ... And I want to calculate the number of total farms , total area, total production ... for each crop in each municipality.
STRUCTURE of the data matrix:
str(dfA)
'data.frame': 523 obs. of 8 variables:
$ CODPROV: Factor w/ 8 levels "4","11","14",..: 1 1 1 1 1 1 1 1 1 1 ...
$ MUN_INE: num 1 1 1 1 1 1 1 1 2 2 ...
$ OTE_NUM: Factor w/ 4 levels "15","20","36",..: 1 3 3 3 2 2 2 4 3 3 ...
$ NUM_EXP: num 1 1 7 14 2 2 3 20 3 8 ...
$ PRODEST: num 3510 830 1924 3388 696 ...
$ UTA_TOT: num 1 90 1705 2053 2613 ...
$ SUP_TOT: num 1395 15 820 1846 2519 ...
$ SAU_TOT: num 1368 15 367 1779 2450 ...
FIRST LINES of the data matrix:
CODE that I have written so far:
aggregate (cbind(df$NUM_EXP,df$PRODEST,df$UTA_TOT,df$SAU_TOT) ~ df$OTE_NUM, data=dfA, FUN=sum)
However, I do NOT want to add only according to 'df$OTE_NUM'
but according to 'df$MUN_INE'
and 'df$OTE_NUM'
. For this I tried to create a list:
aggregate (cbind(df$NUM_EXP,df$PRODEST,df$UTA_TOT,df$SAU_TOT) ~ list(df$MUN_INE, df$OTE_NUM), data=dfA, FUN=sum)
But entering the list gives me an error, how can I do it? I need something like this:
MANY THANKS for the help:)