by function in R
by
Apply A Function To A Data Frame Split By Factors
Function by is an object-oriented wrapper for tapply applied to data frames.
usage
by(data, INDICES, FUN, …, simplify = TRUE)
Example
7男3,通过下面的函数可以返回的是:男生的平均年龄,女生的平均年龄by(age, gender, function(x), mean(x))
p <- read.csv('age.csv', header = F)
p[1,1]=c("female")
colnames(p)=c("Gender","Age")
attach(p)
aver=by(Age, Gender, function(x) mean(x))
print(aver)
Gender: female[1] 27.5
---------------------------------------------------------------------------------------
Gender: male[1] 38.57143