0

I have a data frame with names by type and their frequencies. I'd like to expand this data frame so that the names are repeated according to their name-type frequency. For example, this:

> df = data.frame(name=c('a','b','c'),type=c(0,1,2),freq=c(2,3,2))
name type freq
1    a    0    2
2    b    1    3
3    c    2    2

would become this:

> df_exp
name type
1    a    0
2    a    0
3    b    1
4    b    1
5    b    1
6    c    2
7    c    2

Appreciate any suggestions on a easy way to do this.

Hedgehog
  • 5,487
  • 4
  • 36
  • 43

1 Answers1

3

You can just use rep to "expand" your data.frame rows:

df[rep(sequence(nrow(df)), df$freq), c("name", "type")]
#     name type
# 1      a    0
# 1.1    a    0
# 2      b    1
# 2.1    b    1
# 2.2    b    1
# 3      c    2
# 3.1    c    2

And there's a function expandRows in the splitstackshape package that does exactly this. It also has the option to accept a vector specifying how many times to replicate each row, for example:

expandRows(df, "freq")
Sam Firke
  • 21,571
  • 9
  • 87
  • 105
A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485