Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multicore::sapply?

Tags:

r

multicore

Is there anything like sapply in the multicore library? Or do I have to unlist(mclapply(..)) in order to achieve this?

If it does not exist: what would the reasoning be?

Thanks in advance and sorry if this is a stupid question!

like image 518
icz Avatar asked Sep 05 '25 03:09

icz


2 Answers

In the library parallel, you have mcmapply which, like mapply in base, takes a SIMPLIFY argument. It is TRUE by default. Here is an example use:

library(parallel)
mcmapply(sqrt,split(1:8,1:8))
#        1        2        3        4        5        6        7        8
# 1.000000 1.414214 1.732051 2.000000 2.236068 2.449490 2.645751 2.828427

Compare this to the use of mclapply.

mclapply(split(1:8,1:8),sqrt)
# $`1`
# [1] 1
# 
# $`2`
# [1] 1.414214
# ...
like image 129
nograpes Avatar answered Sep 07 '25 23:09

nograpes


If you look at the code of sapply (and its helper function simplify2array) you will see that unlist(obj) is exactly what will be done in the case where obj is a list of items all of length==1. sapply is a lot more complex than just unlisting lists, however. What is still very unclear is what problem you are trying to solve.

like image 26
IRTFM Avatar answered Sep 07 '25 23:09

IRTFM