I have a data.frame with columns of factors, on which I want to compute a max (or min, or quantiles). I can't use these functions on factors, but I want to.
Here's some example :
set.seed(3)
df1 <- data.frame(id = rep(1:5,each=2),height=sample(c("low","medium","high"),size = 10,replace=TRUE))
df1$height <- factor(df1$height,c("low","medium","high"))
df1$height_num <- as.numeric(df1$height)
# > df1
# id height height_num
# 1 1 low 1
# 2 1 high 3
# 3 2 medium 2
# 4 2 low 1
# 5 3 medium 2
# 6 3 medium 2
# 7 4 low 1
# 8 4 low 1
# 9 5 medium 2
# 10 5 medium 2
I can easily do this:
aggregate(height_num ~ id,df1,max)
# id height_num
# 1 1 3
# 2 2 2
# 3 3 2
# 4 4 1
# 5 5 2
But not this:
aggregate(height ~ id,df1,max)
# Error in Summary.factor(c(2L, 2L), na.rm = FALSE) :
# ‘max’ not meaningful for factors
I want to take the biggest "height", and keep in my aggregated table the same levels as in the original table. In my real data I have many columns and I want to keep my factors sorted to keep my plots clean and consistent.
I can do it this way, and use the following structure in other aggregating functions as well :
use_factors <- function(x,FUN){factor(levels(x)[FUN(as.numeric(x))],levels(x))}
aggregate(height ~ id,df1,use_factors,max)
# id height
# 1 1 high
# 2 2 medium
# 3 3 medium
# 4 4 low
# 5 5 medium
Or I could overload the max
min
median
and quantile
functions I suppose
But I feel I'm surely reinventing the wheel.
Is there an easy way to do this ?
‘max’ not meaningful for factors
– Macnamara