Joe Ripberger
Joe Ripberger

Reputation: 569

Duplicate column values by value in second column

I have a data frame that looks like this:

a<-c('a', 'b', 'c', 'd', 'e')

b<-c(1, 2, 3, 2, 3)

df<-data.frame(a, b)

      a b
    1 a 1
    2 b 2
    3 c 3
    4 d 2
    5 e 3

I would like to duplicate the values in column a by the number in column b, so as to get a vector/data.frame that looks like this:

c<-c('a', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'e', 'e', 'e')

       c
    1  a
    2  b
    3  b
    4  c
    5  c
    6  c
    7  d
    8  d
    9  e
    10 e
    11 e

Thanks for your suggestions.

Upvotes: 1

Views: 2135

Answers (2)

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193527

If you wanted to expand you entire data.frame, you can use something like this:

df <- data.frame(a = c('a', 'b', 'c', 'd', 'e'),
                 b = c(1, 2, 3, 2, 3))
df[rep(rownames(df), df$b), ]
#     a b
# 1   a 1
# 2   b 2
# 2.1 b 2
# 3   c 3
# 3.1 c 3
# 3.2 c 3
# 4   d 2
# 4.1 d 2
# 5   e 3
# 5.1 e 3
# 5.2 e 3

Upvotes: 1

Gavin Simpson
Gavin Simpson

Reputation: 174813

Look at ?rep, as in:

> with(df, rep(a, times = b))
 [1] a b b c c c d d e e e
Levels: a b c d e

The factor bit is annoying as it is how R created b in df. Either do

> df <- data.frame(a, b, stringsAsFactors = FALSE)
> with(df, rep(a, times = b))
 [1] "a" "b" "b" "c" "c" "c" "d" "d" "e" "e" "e"

then the above answer, or just coerce the result to a character vector:

> with(df, as.character(rep(a, times = b)))
 [1] "a" "b" "b" "c" "c" "c" "d" "d" "e" "e" "e"

Upvotes: 3

Related Questions