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!
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
# ...
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With