[R] Use mapply or lapply to a nested list

Ben Tupper btupper @end|ng |rom b|ge|ow@org
Tue Dec 22 00:49:38 CET 2020


You might be able to get mapply to work, but because your lists differ
in lengths you will get some recycling which may lead to mayhem for
you.  Below I have tried to simplify and clarify by naming the
elements of your lists, but I don't get 20 results.  I get 4 elements
(one for each k).  Each kth element has 2 elements (one for b1 and one
for b2).  And each of those varies by the size of the sample you have
requested. Does this get you closer?

k <- list(k1 = 1, k2 = 2, k3 = 4, k4 = 3)
b1 <- list(c(1,2,3),c(2,3,4),c(3,4,5),c(4,5,6))
b2 <- list(c(1,2),c(2,3),c(3,4),c(4,5), c(5,6))
bb <- list(b1  = b1, b2 = b2)

x <- sapply(k,
            function(ki, bb = NULL){
              sapply(bb,
                     function(b) {
                        lapply(b, function(x){
                           sample.int(x, size = ki, replace = TRUE)
                        })
                     }, simplify = FALSE, USE.NAMES = TRUE)
            }, bb = bb, simplify = FALSE, USE.NAMES = TRUE)

str(x)
# List of 4
#  $ k1:List of 2
#   ..$ b1:List of 4
#   .. ..$ : int 1
#   .. ..$ : int 1
#   .. ..$ : int 3
#   .. ..$ : int 4
#   ..$ b2:List of 5
#   .. ..$ : int 1
#   .. ..$ : int 1
#   .. ..$ : int 2
#   .. ..$ : int 1
#   .. ..$ : int 5
#  $ k2:List of 2
#   ..$ b1:List of 4
#   .. ..$ : int [1:2] 1 1
#   .. ..$ : int [1:2] 1 1
#   .. ..$ : int [1:2] 3 3
#   .. ..$ : int [1:2] 2 1
#   ..$ b2:List of 5
#   .. ..$ : int [1:2] 1 1
#   .. ..$ : int [1:2] 1 1
#   .. ..$ : int [1:2] 1 1
#   .. ..$ : int [1:2] 4 2
#   .. ..$ : int [1:2] 2 4
#  $ k3:List of 2
#   ..$ b1:List of 4
#   .. ..$ : int [1:4] 1 1 1 1
#   .. ..$ : int [1:4] 2 1 2 1
#   .. ..$ : int [1:4] 1 3 3 2
#   .. ..$ : int [1:4] 4 3 1 3
#   ..$ b2:List of 5
#   .. ..$ : int [1:4] 1 1 1 1
#   .. ..$ : int [1:4] 1 1 2 1
#   .. ..$ : int [1:4] 2 2 3 1
#   .. ..$ : int [1:4] 4 2 3 3
#   .. ..$ : int [1:4] 5 1 5 3
#  $ k4:List of 2
#   ..$ b1:List of 4
#   .. ..$ : int [1:3] 1 1 1
#   .. ..$ : int [1:3] 2 1 1
#   .. ..$ : int [1:3] 2 2 3
#   .. ..$ : int [1:3] 3 4 1
#   ..$ b2:List of 5
#   .. ..$ : int [1:3] 1 1 1
#   .. ..$ : int [1:3] 2 1 1
#   .. ..$ : int [1:3] 1 3 2
#   .. ..$ : int [1:3] 3 1 1
#   .. ..$ : int [1:3] 1 1 1

Cheers,
Ben

On Mon, Dec 21, 2020 at 2:35 PM Chao Liu <psychaoliu using gmail.com> wrote:
>
> I want to apply a sample function to a nested list (I will call this list
> `bb`) and I also have a list of numbers (I will call this list `k`) to be
> supplied in the sample function. I would like each of the numbers in k to
> iterate through all the values of each list in bb. How to do this using
> `mapply` or `lapply`?
>
> Here are the data:
> k <- list(1,2,4,3) #this is the list of numbers to be supplied in the `
> sample.int` function
> b1 <- list(c(1,2,3),c(2,3,4),c(3,4,5),c(4,5,6)) #The first list of bb
> b2 <- list(c(1,2),c(2,3),c(3,4),c(4,5), c(5,6)) #The second list of bb
> bb <- list(b1,b2) #This is list bb containing b1 and b2 whose values are to
> be iterated through
> ```
> I created this `mapply` function but it didn't get the expected outcome:
> mapply(function(x, y) {
>    x[sample.int(y,y, replace = TRUE)]
> }, bb,k, SIMPLIFY = FALSE)
> This only returns 10 output values but I would like each number of k to
> loop through all values of the two lists in `bb` and so there should be
> 10*2 outputs for the two lists in `bb`. I might be using `mapply` in the
> wrong way and so I would appreciate if anyone can point me to the right
> direction!
>
> Best,
> Chao
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



-- 
Ben Tupper
Bigelow Laboratory for Ocean Science
East Boothbay, Maine
http://www.bigelow.org/
https://eco.bigelow.org



More information about the R-help mailing list