Expand a survey sample with R

1.3k views Asked by At

I would like to expand a sample survey and simulate a population. For example, if I have the following data sample(very small for explain my question) like

control weight  sex age  race
      1      2    F   23    W
      2    3.1    M   21    B
      3    5.3    F   19    W

In this case, control represents the interviewed people. For example, I would like get a dataframe where the control 1 (some person, sex female , 23 yeard old and white) repeats 2 times(2 rows). The dificult arises when I try to repeats 3.1 times the control number 2 and 5.3 the contol number 3, preserving the sex, age and race.

There is the "survey" package, but I don't know if there is some function for this situation.

How can I find a solution for this problem?

1

There are 1 answers

2
akrun On

If you need the expand the rows of the dataset, based on the value in the 'weight' column, one option would be expandRows from splitstackshape. This will be similar to df1[rep(1:nrow(df1), weight),].

 library(splitstackshape)
 expandRows(df1, 'weight')