drawing a stratified sample in R

前端 未结 4 1253
南旧
南旧 2020-12-10 08:53

Designing my stratified sample

library(survey)
design <- svydesign(id=~1,strata=~Category,  data=billa, fpc=~fpc)

So far so good, but ho

4条回答
  •  失恋的感觉
    2020-12-10 09:04

    here's a quick way to sample three records per distinct 'carb' value from the mtcars data frame without replacement

    # choose how many records to sample per unique 'carb' value
    records.per.carb.value <- 3
    
    # draw the sample
    your.sample <- 
        mtcars[ 
            unlist( 
                tapply( 
                    1:nrow( mtcars ) , 
                    mtcars$carb , 
                    sample , 
                    records.per.carb.value 
                ) 
            ) , ]
    
    # print the results to the screen
    your.sample
    

    note that the survey package is mostly used for analyzing complex sample survey data, not creating it. @Iterator is right that you should check out the sampling package for more advanced ways to create complex sample survey data. :)

提交回复
热议问题