Aggregate with max and factors

邮差的信 提交于 2019-12-05 12:22:57

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!