Aggregating values in R

Asked

Viewed 170 times

1

When trying to add values per person, I get the error below saying that the function sumdoes not work for factors, however, my vector is numerical and even using a function to ensure this, I continue to receive the error!

url <- "https://raw.githack.com/fsbmat/StackOverflow/master/DiariasT.csv"
Diaria2 <- data.table::fread(url, dec = ",",colClasses = c("factor", "factor",  "numeric","numeric", "numeric"))
Diaria2$VALOR_DIA <- as.numeric(as.character(Diaria2$VALOR_DIA))
agg = aggregate(Diaria2,
                  by = list(Diaria2$VALOR_DIA),
                  FUN = sum)

Error in Summary.factor(18L, na.rm = FALSE) : 
  ‘sum’ not meaningful for factors
  • Wants to aggregate by NOME_ORG_SOL, for NOME or both?

  • I want to add by NAME.

1 answer

2


The question code has the variable to aggregate, VALOR_DIA, where the aggregation variable should be. And the aggregation variable, which cannot be the whole basis, is missing a class object "data.frame".

A way to aggregate.

This way uses a formula to aggregate the left side by the right side variables (only one).

agg <- aggregate(VALOR_DIA ~ NOME, data = Diaria2, FUN = sum)

Another way.

This way is the way used in question but correctly.

agg2 <- aggregate(Diaria2$VALOR_DIA,
                 by = list(Diaria2$NOME),
                 FUN = sum)

Comparing the results it is seen that the only difference is in the names of the columns of the two results, the values of the sums are equal.
Personally, I prefer the formula interface because it is intuitive and also because it produces results with the same names as the variables used.

all.equal(agg, agg2)
#[1] "Names: 2 string mismatches"
names(agg)
#[1] "NOME"      "VALOR_DIA"
names(agg2)
#[1] "Group.1" "x" 
  • Thank you very much! I tried in many ways and didn’t realize the simple mistake I was making!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.