R语言_apply系列函数

#apply
#get answer grouped by col/row
d = matrix(1:30,5,6)
apply(d,1,mean)  #row
apply(d,2,mean)  #col
M <- array( seq(32), dim = c(4,4,2))
apply(M, 1, sum) #row
apply(M, c(1,2), sum) #row % col
colMeans,rowMeans,colSums,rowSums

#lapply
#list to list
x <- list(a = 1, b = 1:3, c = 10:100) 
lapply(x, FUN = length) 
lapply(x, FUN = sum) 

#sapply
#list to vector
x <- list(a = 1, b = 1:3, c = 10:100)
sapply(x, FUN = length) 
sapply(x, FUN = sum) 
sapply(1:5,function(x) rnorm(3,x))
sapply(1:5,function(x) matrix(x,2,2))
sapply(1:5,function(x) matrix(x,2,2), simplify = "array")

#vapply
#speed up the sapply
x <- list(a = 1, b = 1:3, c = 10:100)
#Note that since the advantage here is mainly speed, this
# example is only for illustration. We're telling R that
# everything returned by length() should be an integer of 
# length 1. 
vapply(x, FUN = length, FUN.VALUE = 0L) 

#mapply
#Sums the 1st elements, the 2nd elements, etc. 
mapply(sum, 1:5, 1:5, 1:5) 
[1]  3  6  9 12 15
#To do rep(1,4), rep(2,3), etc.
mapply(rep, 1:4, 4:1)   

#Map
#A wrapper to mapply with SIMPLIFY = FALSE, so it is guaranteed to return a list.
Map(sum, 1:5, 1:5, 1:5)

#rapply
#Append ! to string, otherwise increment
myFun <- function(x){
    if (is.character(x)){
        return(paste(x,"!",sep=""))
    }
    else{
        return(x + 1)
    }
}
#A nested list structure
l <- list(a = list(a1 = "Boo", b1 = 2, c1 = "Eeek"), 
          b = 3, c = "Yikes", 
          d = list(a2 = 1, b2 = list(a3 = "Hey", b3 = 5)))
#Result is named vector, coerced to character           
rapply(l,myFun)
#Result is a nested list like l, with values altered
rapply(l, myFun, how = "replace")

#tapply
x <- 1:20
y <- factor(rep(letters[1:5], each = 4))
tapply(x, y, sum)  

参考资料

stackoverflow

R语言中,apply函数是一个非常有用的函数,它可以对矩阵、数组、列表等数据结构进行操作,并返回结果。apply函数有几个不同的变体,包括sapply函数。 sapply函数与lapply函数类似,都是将指定的函数应用于列表、向量、表达式等,并以向量或矩阵的形式返回结果。\[2\]与lapply函数不同的是,sapply函数会尝试将结果转换为更简单的数据结构,例如向量或矩阵。如果函数的返回值是一个向量,则sapply函数返回一个包含这些值的向量。如果函数的返回值是一个大于1的向量,则sapply函数会返回一个矩阵。\[1\] 例如,我们可以使用sapply函数对iris数据集的前四列进行操作,判断每个元素是否大于3,并返回一个矩阵。代码如下: x <- sapply(iris\[,1:4\], function(x) {x > 3}) class(x) 这段代码将返回一个逻辑矩阵,其中的每个元素表示对应位置的值是否大于3。\[1\] 除了sapply函数,还有其他类似的函数,如lapply函数和vapply函数,它们也可以对列表、向量等进行操作,并返回结果。\[3\]这些函数R语言中非常常用,可以帮助我们快速处理数据和进行统计分析。 #### 引用[.reference_title] - *1* *2* [R语言apply系列函数](https://blog.csdn.net/ouyangk1026/article/details/122535256)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [R语言:常用apply函数apply,tapply,sapply,lapply)用法介绍](https://blog.csdn.net/qq_43407763/article/details/91652918)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值