Error in Summary.factor(1L, na.rm = FALSE) : sum not meaningful for factors for aggregate

StackOverflow https://stackoverflow.com/questions/23536186

  •  17-07-2023
  •  | 
  •  

Question

I have similar questiln as This link

value <- 1:10
Name <- c(rep("A",5),rep("B",5))
data <- data.frame(Name, value)

My approach using aggregate is

> aggregate(factor(Name) ~ value, data=data, FUN="sum")
Error in Summary.factor(1L, na.rm = FALSE) : 
  sum not meaningful for factors

And i follow the link while have the same error

> data[] <- lapply(data, function(x) type.convert(as.character(x)))
> aggregate(Name ~ value, data, sum)
Error in Summary.factor(1L, na.rm = FALSE) : 
  sum not meaningful for factors

Thanks

Was it helpful?

Solution

You have to switch Name and value:

aggregate(value ~ Name, data = data, FUN = "sum")
  Name value
1    A    15
2    B    40
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top