R error sum not meaningful for factors
The error comes when you try to call sum(x)
and x
is a factor.
What that means is that one of your columns, though they look like numbers are actually factors (what you are seeing is the text representation)
simple fix, convert to numeric. However, it needs an intermeidate step of converting to character first. Use the following:
family[, 1] <- as.numeric(as.character( family[, 1] ))
family[, 3] <- as.numeric(as.character( family[, 3] ))
For a detailed explanation of why the intermediate as.character
step is needed, take a look at this question: How to convert a factor to integer\numeric without loss of information?
Aggregate ‘sum’ not meaningful for factors in R
Using dplyr :
agg <- df %>%
group_by(col2, col3) %>%
summarise(col4 = sum(col4),
col5 = sum(col5))
# col2 col3 col4 col5
# <fct> <fct> <dbl> <dbl>
# 1 mi re 4 4
# 2 se my 6 6
# 3 ty my 5 5
Is that what you are looking for ?
In aggregate: sum not meaningful for factors
It is because of how you're creating your dataframe. For example, c1
is character because a vector can only have one class. When you put them into a dataframe, those character vectors are further coerced to factor. Thus you're trying to run sum
on factors. You figured this out already, but then tried to convert factors to numeric, which is probably giving you nonsensical results.
The easy answer is to build your dataframe column-wise rather than row-wise, so you don't get into so many coercion problems.
Given the data you already have, this will solve your problem:
df[] <- lapply(df, function(x) type.convert(as.character(x)))
aggregate(. ~ V1, df, sum)
(Thanks to @AnandaMahto for the much cleaner way of doing that conversion than what I originally had.)
Result:
V1 V2 V3 V4 V5 V6 V7
1 Afghanistan 2 54 34.5 10.4 2 0
2 Albania 12 160 72.5 70.5 664 12
Error in Summary.factor ‘min’ not meaningful for factors!! How can I eliminate this error?
You could use as.numeric
in lapply
like this:
test<-as.data.frame(lapply(test, function(x) {
levels(x)[levels(x) %in% na_code] <- 0
as.numeric(x) }))
Hope it helps!
Error in Summary.factor(1L, na.rm = FALSE) : sum not meaningful for factors for aggregate
You have to switch Name
and value
:
aggregate(value ~ Name, data = data, FUN = "sum")
Name value
1 A 15
2 B 40
sum' not meaningful for factors while using diag(prop.table()) functionality
The reason is mentioned in the error
, the variable
is factor
. It is not possible to apply prop.table
directly on a factor
class as it requires some computation.
prop.table(m1)
Error in Summary.factor(c(2L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L,
: ‘sum’ not meaningful for factors
Based on the values shown, it should be a logical vector, so convert it to logical and it should work
as.logical(m1)
prop.table(as.logical(m1))
#[1] 0.09090909 0.09090909 0.00000000 0.00000000 0.00000000 0.00000000 0.09090909 0.00000000 0.00000000 0.09090909 0.00000000 0.09090909 0.00000000 0.00000000
#[15] 0.09090909 0.00000000 0.09090909 0.09090909 0.00000000 0.09090909 0.09090909 0.00000000 0.00000000 0.09090909
data
set.seed(24)
m1 <- factor(sample(c(TRUE, FALSE), 24, replace=TRUE))
kdd_test_target <- factor(sample(c(TRUE, FALSE), 24, replace=TRUE))
R: aggregating data frame sum not meaningful factors
Based on the str(nbaagg)
, nbaagg
is a list
of vector
s and not a data.frame. It can be converted to data.frame
with as.data.frame
(here the list
elements are of equal length
nbaagg <- as.data.frame( nbaagg)
then, we can use
aggregate(.~ Team, nbaagg, FUN = sum, na.rm = TRUE, na.action = NULL)
It was created as a list
in this step
nbaagg <- lapply(nbaagg, function(x) type.convert(as.numeric(x)))
The lapply
output is always a list
. If we want to have the same attributes as in the original dataset, use []
nbaagg[] <- lapply(nbaagg, function(x) type.convert(as.numeric(x)))
Here, the type.convert
can be directly used on the dataset assuming they are all character
class instead of a loop with lapply
nbaagg <- type.convert(nbaagg, as.is = TRUE)
Related Topics
Create Frequency Tables for Multiple Factor Columns in R
Using Dynamic Column Names in 'Data.Table'
How to Change the Color Value of Just One Value in Ggplot2's Scale_Fill_Brewer
Setting Function Defaults R on a Project Specific Basis
How to Change the First Row to Be the Header in R
R - Emulate the Default Behavior of Hist() with Ggplot2 for Bin Width
Create Empty Data Frame with Column Names by Assigning a String Vector
Network Chord Diagram Woes in R
How to Change Python Path in Reticulate
Filling Area Under Curve Based on Value
How to Append Rows to an R Data Frame
Removing Display of Row Names from Data Frame
Same Function Over Multiple Data Frames in R
Ggplot - Multiple Legends Arrangement