Good afternoon.
I'm using the aggregate
function to group some data. However, I'm only using one variable to add. I would like to use more than one variable. It is possible?
I'm using the following example:
TESTE = aggregate(VALOR ~ REFERENCIA + GRUPO_COPA + CIDADE, data=DADOS,FUN=sum)
I would like to use variable QTDE
next to VALOR
to add, that is, to add another column, with the following columns:
REFERENCIA, GRUPO_COPA, CIDADE, VALOR, QTDE
Is it possible in the aggregate or in another function this example? Thankful.
Edit
Look at my example using the dput:
structure(list(REFERENCIA = c("JAN_2017", "JAN_2017", "JAN_2017", "JAN_2017", "FEV_2017", "FEV_2017", "FEV_2017", "FEV_2017", "FEV_2017" ), GRUPO_COPA = c("AZUL", "AZUL", "AMARELO", "AMARELO", "VERDE", "VERDE", "VERDE", "AZUL", "AZUL"), CIDADE = c("SP", "SP", "SP", "SP", "RJ", "BSB", "BSB", "BSB", "SP"), VALOR = c(1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000), QTDE = c(1, 3, 5, 7, 9, 11, 13, 15, 17)), .Names = c("REFERENCIA", "GRUPO_COPA", "CIDADE", "VALOR", "QTDE"), row.names = c(NA, 9L), class = "data.frame")
I would like to group this dataset (similar to aggregate or similar) by adding the VALUE and QTDE columns.