1. split函数
1.1 函数功能
Divide into Groups and Reassemble
split divides the data in the vector x into the groups defined by f.
The replacement forms replace values corresponding to such a division.
分组并重新组合
split将向量x中的数据划分为f定义的组。
1.2 函数语法
split(x, f, drop = FALSE, sep = ".", lex.order = FALSE, ...)
1.3 函数参数
x
vector or data frame containing values to be divided into groups.
必输参数,待分组的对象,对象为向量或者数据框
f
a ‘factor’ in the sense that as.factor(f) defines the grouping, or a list of such factors in which case their interaction is used for the grouping.
必输参数,分组因素,表示进行分组的依据
studentID <- seq(1,20)
gender <- rep(c('M','M','F','F','F'),2)
class <- rep(c('1班','2班','2班','1班'),5)
math <- rep(c(56,78,90,73,87),4)
English <- rep(c(34,57,98,76,95),4)
data <- data.frame(studentID,gender,class,math,English,
stringsAsFactors = F)
data
# 按照性别分组
sub_data <- split(data,f=list(data$gender))
sub_data
# 按照性别、班级分组
sub_data2 <- split(data,f=list(data$gender,data$class))
sub_data2
drop
logical indicating if levels that do not occur should be dropped
逻辑值,表明没有取值的水平是否删除,默认不删除
# 修改性别参数
data$gender <- c('M','F','M','M')
data
# 按照性别、班级分组
sub_data3 <- split(data,f=list(data$gender,data$class))
sub_data3
当drop=FALSE时
当drop=TRUE时,没有数据的女性1班删除
# 按照性别、班级分组
sub_data3 <- split(data,f=list(data$gender,data$class),drop=TRUE)
sub_data3
sep
character string, passed to interaction in the case where f is a list.
lex.order
logical, passed to interaction when f is a list.
...
further potential arguments passed to methods.
2.lapply函数
2.1 函数功能
lapply returns a list of the same length as X,
each element of which is the result of applying FUN to
the corresponding element of X.
lapply返回一个与X长度相同的列表,其中每个元素都是将FUN应用于X的相应元素的结果。
2.2 函数语法
lapply(X, FUN, ...)
2.3 函数参数
X
a vector (atomic or list) or an expression object.
Other objects (including classed objects) will be coerced by base::as.list.
向量或者列表,或者表达式,其他对象将强制转化为列表
FUN
the function to be applied to each element of X:
In the case of functions like +, %*%, the function name must be backquoted or quoted.
对数据进行统计的函数。对于+,%*%之类的函数,必须对函数名称进行反引号或加引号。
...
optional arguments to FUN.
# 求平均成绩
# 按照性别、班级分组
mean_sub_data3 <- lapply(sub_data3,FUN=function(a){mean(a$English)})
mean_sub_data3
3.cbind函数
# 求平均成绩
# 按照性别、班级分组
mean_English <- lapply(sub_data3,FUN=function(a){mean(a$English)})
mean_English
mean_math <- lapply(sub_data3,FUN=function(a){mean(a$math)})
mean_math
# 合并
mean_data <- cbind(mean_English,mean_math)
mean_data