vendredi 20 août 2021

Sampling a proportion from a population data frame in R (random sampling in stratified sampling)

i have a data frame (population) with 3 groups. I want :

A) to take the 0.05 % of each category and B) to take different proportion from each group.

my data frame population is :

category = c(rep("a",15),rep("b",30),rep("c",50))
num = c(rnorm(15,0,1),rnorm(30,5,1),rnorm(50,10,1))
pop = data.frame(category,num);pop

i am thinking of the sample_n() function from dplyr but how can i take the 0.05% of each group?

in the code below i take 5 elements at random from each group.

pop%>%
  group_by(category)%>%
  sample_n(size = 5)

and how i can change the prop allocation say 0.05% from category a, 0.1 % from b and 20% from c?




Aucun commentaire:

Enregistrer un commentaire