[英]How do I use an apply function in R to find the maximum of a variable for subgroups
salary <- data.frame(name=c("Kay","Dave","Jon","Jenny","Jim","Eve","Ed"),
salary=c(60000, 100000, 50000, 80000, 30000, 40000, 20000),
gender=factor(c("F","M","M","F","M","F","M")))
I am able to filter a female data frame with我能够过滤女性数据框
femaleFiltered <- salary[ which(salary$gender=='F'), ]
and I am trying to use我正在尝试使用
getMaxSal1 <- function(x) {
x[which.max(x$salary),]
}
apply(femaleFiltered,2,getMaxSal1)
but get error "Error in x$salary: $ operator is invalid for atomic vectors"但得到错误“x$salary 中的错误:$ 运算符对原子向量无效”
We can use subset
from base R
我们可以使用来自
base R
的subset
subset(femaleFiltered, salary == max(salary))
# name salary gender
#4 Jenny 80000 F
Or as per OP's requirement about a possible use of * apply
或根据 OP 关于可能使用 *
apply
的要求
femaleFiltered[unlist(lapply(names(femaleFiltered),
function(nm) which.max(femaleFiltered[nm]$salary))),]
# name salary gender
#4 Jenny 80000 F
I'd suggest我建议
aggregate(salary$salary, by=list(salary$gender), FUN = max)
oh just read about needing to do an apply function.哦,刚刚读到需要申请 function。
Make a list of two data frames, one for males, one for females.列出两个数据框,一个是男性,一个是女性。
Then use lapply() with FUN = max.然后将 lapply() 与 FUN = max 一起使用。
lapply(list(femaleFiltered$salary, maleFiltered$salary), max)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.