split-lapply-cbind模式--R语言

本文介绍了R语言中split、lapply和cbind三个函数的使用。split函数用于按指定因素将数据分组,lapply则用于对数据集的每个元素应用指定函数,cbind用于合并数据。示例中展示了如何按照性别和班级对数据进行分组,并计算平均成绩。通过调整drop参数,可以控制分组时是否保留未出现的级别。最后,利用lapply计算各组的平均成绩,并使用cbind进行结果整合。
摘要由CSDN通过智能技术生成

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

在这里插入图片描述

在这里插入图片描述

参考函数: splitlapply

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值