[R] Alternative to mapply to select samples

Chao Liu p@ych@o||u @end|ng |rom gm@||@com
Tue Feb 2 15:31:53 CET 2021


Dear R-Help,

I created a mapply function to select samples from a dataset but are there
any faster ways to do it by avoiding mapply because it is slow and I have a
larger dataset? My goal is to use more matrix / vector operations and less
in terms of lists (the format of the output can be flexible). Ideally, I
would like to stick to base R methods without the aid of parallel process
or packages. Any ideas will be appreciated!

#A list of a set of data to be selected
bl <- list(list(c(1, 2),c(2, 3), c(3, 4), c(4, 5), c(5, 6), c(6, 7), c(7,
8), c(8, 9)),
         list(c(1, 2, 3), c(2, 3, 4), c(3, 4, 5), c(4, 5, 6), c(5, 6, 7),
c(6, 7, 8)),
         list(c(1, 2, 3, 4, 5), c(2, 3, 4, 5, 6), c(3, 4, 5, 6, 7), c(4, 5,
6, 7, 8), c(5, 6, 7, 8, 9)))
#Number of elements to be selected
kn <- c(5, 4, 3)
#Total number of elements in each set
nb <- c(8, 6, 5)
#This output a list but preferably I would like a matrix
bl_func <- function() mapply(function(x, y, z) {
  x[sample.int(y, z, replace = TRUE)]
}, bl, nb, kn, SIMPLIFY = FALSE)

Best,

Chao
ᐧ

	[[alternative HTML version deleted]]



More information about the R-help mailing list