apply、lapply、sapply 与parApply、parApply、parSapply的区别

parApply、parApply、parSapply是parallel包里面的

apply、lapply、sapply是base包里面自带的。

parApply、parApply、parSapply分别是apply、lapply、sapply的并行运算版本。对应的方法输出结果和使用方式相同,是不过par-需要设定核数(或者使用默认核数计算)

margin

目录

1.apply     对象为数组

Apply Functions Over Array Margins

Description

Usage

Arguments

 2.lapply    对象为向量

Apply a Function over a List or Vector

3.sapply,vapplysapply(X, FUN, ..., simplify = TRUE, USE.NAMES = TRUE)

vapply(X, FUN, FUN.VALUE, ..., USE.NAMES = TRUE)

replicate(n, expr, simplify = "array")

simplify2array(x, higher = TRUE)

 4.parLapply

Apply Operations using Clusters

Description

Usage

parApply

parLapplyparLapply returns a list the length of X.

Arguments


1.apply     对象为数组

apply {base}R Documentation

 

Apply Functions Over Array Margins

Description

Returns a vector or array or list of values obtained by applying a function to margins of an array or matrix.

Usage

apply(X, MARGIN, FUN, ...)

 apply(对象,行/列,功能函数)

Arguments

X

an array, including a matrix.    要处理的数组对象

MARGIN

a vector giving the subscripts which the function will be applied over. E.g., for a matrix 1 indicates rows, 2 indicates columns, c(1, 2) indicates rows and columns. Where X has named dimnames, it can be a character vector selecting dimension names.    1表示行,2表示列,c(1,2)表示行列

FUN

the function to be applied: see ‘Details’. In the case of functions like +%*%, etc., the function name must be backquoted or quoted.    可以是自己定义的功能函数

...

optional arguments to FUN.   省略号中的内容为fun函数自己的参数。所以会不一样

 example:

a<-array(1:90000,dim = c(100,900))
apply(a,1,sd)   #对每一行求sd
system.time(apply(a,1,sd))   #计算运行时间,可以用来比较速度

 

 

 2.lapply    对象为向量

lapply {base}R Documentation

Apply a Function over a List or Vector

 

lapply(X, FUN, ...)   #因为对象是列表或向量,所以不需要用margin来指定是行或者是列了

x <- list(a = 1:10, beta = exp(-3:3), logic = c(TRUE,FALSE,FALSE,TRUE)) 
#x是一个包含多项向量的列表对象
#TRUE=1   FALSE=0
# compute the list mean for each list element
lapply(x, mean)   
返回结果:
$a
[1] 5.5

$beta
[1] 4.535125

$logic
[1] 0.5



3.sapply,vapply
sapply(X, FUN, ..., simplify = TRUE, USE.NAMES = TRUE)

vapply(X, FUN, FUN.VALUE, ..., USE.NAMES = TRUE)

replicate(n, expr, simplify = "array")

simplify2array(x, higher = TRUE)

sapply is a user-friendly version and wrapper of lapply by default returning a vector, matrix or, if simplify = "array", an array if appropriate, by applying simplify2array()sapply(x, f, simplify = FALSE, USE.NAMES = FALSE) is the same as lapply(x, f).

 vapply is similar to sapply, but has a pre-specified type of return value, so it can be safer (and sometimes faster) to use.

 

 4.parLapply

clusterApply {parallel}R Documentation

Apply Operations using Clusters

Description

These functions provide several ways to parallelize computations using a cluster.

有多种方式可以用来并行计算 

我们这里主要介绍parLapply和parApply的用法 

Usage

clusterCall(cl = NULL, fun, ...)
clusterApply(cl = NULL, x, fun, ...)
clusterApplyLB(cl = NULL, x, fun, ...)
clusterEvalQ(cl = NULL, expr)
clusterExport(cl = NULL, varlist, envir = .GlobalEnv)
clusterMap(cl = NULL, fun, ..., MoreArgs = NULL, RECYCLE = TRUE,
           SIMPLIFY = FALSE, USE.NAMES = TRUE,
           .scheduling = c("static", "dynamic"))
clusterSplit(cl = NULL, seq)

parLapply(cl = NULL, X, fun, ..., chunk.size = NULL)   #向量对象
parSapply(cl = NULL, X, FUN, ..., simplify = TRUE,
          USE.NAMES = TRUE, chunk.size = NULL)
parApply(cl = NULL, X, MARGIN, FUN, ..., chunk.size = NULL)#数组对象
parRapply(cl = NULL, x, FUN, ..., chunk.size = NULL)
parCapply(cl = NULL, x, FUN, ..., chunk.size = NULL)

parLapplyLB(cl = NULL, X, fun, ..., chunk.size = NULL)
parSapplyLB(cl = NULL, X, FUN, ..., simplify = TRUE,
            USE.NAMES = TRUE, chunk.size = NULL)

parApply

a<-array(1:90000,dim = c(100,900))
library(parallel)
detectCores()#检查核数
cl<-makeCluster(12)   #用makeCluster函数定义核数
results <- parApply(cl,a,1,sd)   #将运算结果赋值给一个变量
class(results)   #"numeric"

 #parApply的结果和apply一样,如果每个call返回一个向量,那么结果都是数组;如果每个call返回多个向量,那么结果就是一个多维数组

parLapply
parLapply returns a list the length of X.

library(parallel)
cl <- makeCluster(19)
x <- list(a = 1:10, beta = exp(-3:3), logic = c(TRUE,FALSE,FALSE,TRUE))
# compute the list mean for each list element
parLapply(cl,x, mean)

Arguments

cl

a cluster object, created by this package or by package snow. If NULL, use the registered default cluster.   如果cl为空,则使用默认值。

fun, FUN

function or character string naming a function.   #功能函数名

expr

expression to evaluate.评估表达式

seq

vector to split.

varlist

character vector of names of objects to export.

envir

environment from which t export variables

x

a vector for clusterApply and clusterApplyLB, a matrix for parRapply and parCapply.

...

additional arguments to pass to fun or FUN: beware of partial matching to earlier arguments.

MoreArgs

additional arguments for fun.

RECYCLE

logical; if true shorter arguments are recycled.

X

A vector (atomic or list) for parLapply and parSapply, an array for parApply.

chunk.size

scalar number; number of invocations of fun or FUN in one chunk; a chunk is a unit for scheduling.标量值;在一个chunk中调用fun或fun的次数;chunk是用于调度的单元。

MARGIN

vector specifying the dimensions to use.

simplify, USE.NAMES

logical; see sapply.

SIMPLIFY

logical; see mapply.

.scheduling

should tasks be statically allocated to nodes or dynamic load-balancing used?

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

uforia

感谢打赏!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值