我希望使用plyr计算每个类别中最频繁的因子水平,使用下面的代码。数据框b显示了请求的结果。为什么
c$mlevels
只有"numeric"这个值?require(plyr)
set.seed(0)
a <- data.frame(cat=round(runif(100, 1, 3)),
levels=factor(round(runif(100, 1, 10))))
mode <- function(x) names(table(x))[which.max(table(x))]
b <- data.frame(cat=1:3,
mlevels=c(mode(a$levels[a$cat==1]),
mode(a$levels[a$cat==2]),
mode(a$levels[a$cat==3])))
c <- ddply(a, .(cat), summarise,
mlevels=mode(levels))
mode
存在于base
中。 - A5C1D2H2I1M1N2O1R2T1summarise
看到的是默认在base
中的不同mode
。这就是为什么@AnandaMahto在他的编辑中解释说,使用已经在R中使用过的所有默认名称不明智,因为你的代码更容易出现错误。 - Arunhere
函数:ddply(a, .(cat), here(summarise), mlevels=mode(levels))
。文档中的解释应该会有所帮助。 - hadley