![](/img/trans.png)
[英]Creating a summary statistic table from MULTIPLE categories within a variable
[英]How to make a boolean variable from a summary statistic in a dataframe?
我想創建一個布爾值列,該值表明每個樣本是否為最大值。 我做了這個功能,並與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)
}
是的,您可以使用tapply
在一行中執行此操作:
tapply(y,f,function(x) round(x,5)==round(max(x),5))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.