0

I have a dataframe A with 2 columns namely "Amount" and "Number of times". I would like to create a new dataframe B which repeats the "Amount" in dataframe A according to "Number of times". For example, if the first row in dataframe A has "Ammount"=50 and "Number of times"=4 and the second row has "Amount"=80 and "Number of times"=2, I want to create a new dataframe B as shown below:

50  50  50  50
80  80

So, the rows of dataframe B will have different length. Please help!

ccc
  • 189
  • 1
  • 3
  • 12
  • Please make this question [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by showing actual data structures (`matrix`, `data.frame`, etc). As is, it's a little unclear. With your example here, what should I get if I ask for `x[2,3]`? – r2evans May 30 '18 at 21:55

2 Answers2

0

Here's one way to do it using apply:

A <- data.frame(Amount = c(50, 80), Times = c(4, 2))
#   Amount Times
# 1     50     4
# 2     80     2

x <- apply(A, 1, function(r) {
         c(rep(r[['Amount']], r['Times']), rep(NA, max(A['Times'] - r['Times'])))
     })
t(x)
#      [,1] [,2] [,3] [,4]
# [1,]   50   50   50   50
# [2,]   80   80   NA   NA

Here's an alternative that's slightly less wordy:

> do.call(rbind, lapply(mapply(rep, A$Amount, A$Times), `length<-`, max(A$Times)))
     [,1] [,2] [,3] [,4]
[1,]   50   50   50   50
[2,]   80   80   NA   NA
C. Braun
  • 5,061
  • 19
  • 47
0

Option#1:

An option using splitstackshape::cSplit package as:

df <- data.frame(Amt = c(50, 80), NoTime = c(4,2))

library(splitstackshape)


df_B <- data.frame(col = apply(df, 1, function(x)paste(rep(x[1],x[2]),collapse = ",")))

cSplit(df_B, "col", sep = ",")

#    col_1 col_2 col_3 col_4
# 1:    50    50    50    50
# 2:    80    80    NA    NA

Option#2: A tidyverse based solution can be as:

library(tidyverse)
df$rn <- 1:nrow(df)

df_B <- df[rep(seq(nrow(df)), df$NoTime),]

df_B %>% select(-NoTime) %>%
  group_by(rn) %>%
  mutate(rowN = row_number()) %>%
  spread(rowN, Amt)
# # A tibble: 2 x 5
# # Groups: rn [2]
#      rn   `1`   `2`   `3`   `4`
# *   <int> <dbl> <dbl> <dbl> <dbl>
# 1     1  50.0  50.0  50.0  50.0
# 2     2  80.0  80.0  NA    NA  
MKR
  • 19,739
  • 4
  • 23
  • 33