Aggregate with max and factors
Asked Answered
M

1

4

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 ?

Macnamara answered 19/7, 2017 at 16:21 Comment(0)
E
5

Actually, you can do the aggregation that you want, if you use an ordered factor.

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"), ordered = TRUE)
df1$height_num <- as.numeric(df1$height)

aggregate(height~id, df1, max) 
  id height
1  1   high
2  2 medium
3  3 medium
4  4    low
5  5 medium
Earmark answered 19/7, 2017 at 16:27 Comment(2)
Amazing thanks, I knew it had to be easy! It's quite misleading that the error says ‘max’ not meaningful for factorsMacnamara
Yes. That message could be more helpful.Earmark

© 2022 - 2024 — McMap. All rights reserved.