【发布时间】:2014-02-12 21:58:05
【问题描述】:
我想创建一个布尔列来说明每个样本是否为最大值。
我做了这个函数,并与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)
但是有没有更好、更有效的方法来做到这一点?
{附注实际上,我使用了我的真实数据sapply,例如is.maxes<-sapply(s, function(x) is.max(x[,"Sum"]),simplify=F)}
【问题讨论】:
标签: r boolean max sapply tapply