Search code examples
rlistsample

How to pass sample size through a vector?


I have a list and I want to have samples. The number of samples should correspond to the number in a vector. The problem is that he only takes the first number in sample_size for all list entries. I hope anyone can help me!


liste_mpg<-plyr::dlply(mtcars,"mpg")
sample_size<-c(2,6,7,1,4,8,9,0,7,9,3,4,6,1,8,4,3,2,6,7,4,6,9,1,4)
test_list<-lapply(liste_mpg, function(x) sample(x,size = sample_size))

Solution

  • We can use Map from base R to do the sampling based on corresponding elements of list and vector (or another list)

    Map(function(x, y) sample(x, size = y), liste_mpg, sample_size)
    

    With lapply, the issue is that it loops through the elements of 'liste_mpg', while the 'sample_size' is the full vector. Instead, can loop over the sequence of list, extract the corresponding elements based on that index and do the sample

    lapply(seq_along(liste_mpg), function(i) 
        sample(liste_mpg[[i]], size = sample_size[i]))
    

    Or equivalent to map2 from purrr

    map2(liste_mpg, sample_size, ~ sample(.x, size = .y))