我想创建一个布尔列,说明每个样本是否为最大值。我制作了这个功能并将其用于tapply
:
is.max <- function(x){
x <- data.frame(x)
x$x <- round(x$x,5)
x_max <- round(max(x),5)
for(i in 1:nrow(x)) {
if(x$x[i] == x_max) x$is.max[i] <- T
else x$is.max[i] <- F
}
return(x$is.max)
}
y <- c(rnorm(10), runif(10), rnorm(10,1))
f <- gl(3,10)
m <- tapply(y,f,is.max)
但是有没有更好、更有效的方法来做到这一点?
{PS 实际上是我使用的真实数据sapply
,例如is.maxes<-sapply(s, function(x) is.max(x[,"Sum"]),simplify=F)
}