1

Hi everyone I am struggling with something that I think should be easy.

I have a dataset that looks as follows

Var1 Var2 Var3 Var4 Count
 a    b    c     d    10
 z    a    c     f    3

I just need a function that replicates the rows based on the count. So I would end up with a new data frame or data table that would have 13 rows.

I am trying to use the rep() function to do this, but it isn't working.

dat <- read.table(text="Var1 Var2 Var3 Var4 Count
 a    b    c     d    10
 z    a    c     f    3", header=TRUE)
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
blast00
  • 559
  • 2
  • 8
  • 18

1 Answers1

4

Something like this is standard:

dat[rep(1:nrow(dat), dat[["Count"]]), ]

##     Var1 Var2 Var3 Var4 Count
## 1      a    b    c    d    10
## 1.1    a    b    c    d    10
## 1.2    a    b    c    d    10
## 1.3    a    b    c    d    10
## 1.4    a    b    c    d    10
## 1.5    a    b    c    d    10
## 1.6    a    b    c    d    10
## 1.7    a    b    c    d    10
## 1.8    a    b    c    d    10
## 1.9    a    b    c    d    10
## 2      z    a    c    f     3
## 2.1    z    a    c    f     3
## 2.2    z    a    c    f     3
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519