Search code examples
rlistsampling

how to sample from multiple lists (numbers?


I would like to sample, let's say the ages of 100 persons above 65 years old, and the probabilities for the age groups are as follows:

65-74<- 0.56
75-84<- 0.30
85<- 0.24

I know the existence of the sample function and I tried it as follows,but that didn't work unfortunately

list65_74<-range(65,74)
list75_84<-range(75,84)
list85<-range(85,100)

age<-sample(c(list65_74,list75_84,list85),size=10,replace=TRUE,prob =c(0.56,0.30,0.24 ))I get the following error

I got then the following error

 Error in sample.int(length(x), size, replace, prob) : 
      incorrect number of probabilities

So I was wondering what is the proper way to sample from multiple lists. Thank you very much in advance!


Solution

  • First, let's I'll call those three objects groups instead since they don't use the list function.

    The way you define them could be fine, but it's somewhat more direct to go with, e.g., 65:74 rather than c(65, 74). So, ultimately I put the three groups in the following list:

    groups <- list(group65_74 = 65:74, group75_84 = 75:84, group85 = 85:100)
    

    Now the first problem with the usage of sample was your x argument value, which is

    either a vector of one or more elements from which to choose, or a positive integer. See ‘Details.’

    Meanwhile, you x was just

    c(list65_74, list75_84, list85)
    # [1]  65  74  75  84  85 100
    

    Lastly, the value of prob is inappropriate. You supply 3 number to a vector of 6 candidates to sample from. Doesn't sound right. Instead, you need to assign an appropriate probability to each age from each group as in

    rep(c(0.56, 0.30, 0.24), times = sapply(groups, length))
    

    So that the result is

    sample(unlist(groups), size = 10, replace = TRUE, 
           prob = rep(c(0.56, 0.30, 0.24), times = sapply(groups, length)))
    # [1] 82 72 69 74 72 72 69 70 74 70