5

I want to expand a data frame given some conditions. It is a bit similar to this question expand data frames inside data frame, but not quite the same.

I have a data frame:

df = data.frame(ID = c(3,3,3,3, 17,17,17, 74, 74, 210, 210, 210, 210), amount = c(101, 135, 101, 68,  196, 65 ,135, 76, 136, 15, 15, 15 ,15), week.number = c(4, 6, 8, 10, 2, 5, 7, 2, 6, 2, 3, 5, 6))

I want to expand the data frame for each ID, given a min and max week.number, and having 0 in the amount column for this expansion. Min week.number is 1 and max week.number is 10. The expected results would be:

df1 <- data.frame(ID = c(rep(3,10), rep(17, 10), rep(74, 10), rep(210, 10)),
              amount = c(0, 0, 0, 101, 0, 135, 0, 101, 0, 68, 0, 196,
                         0, 0, 65, 0, 135, 0, 0, 0, 0, 76, 0, 0, 0,
                         136, 0, 0, 0, 0, 0, 15, 15, 0, 15, 15, 0, 0,
                         0, 0))

(In reality, I have thousands of ID and week number goes from 1 to 160).

Is there a simple, fast way to do this?

Thank you!

Community
  • 1
  • 1
Andres
  • 281
  • 2
  • 13

2 Answers2

6

Here's how you could do it using tidyr:

library(tidyr)
complete(df, ID, weeek.number = 1:10, fill = list(amount = 0))
#Source: local data frame [40 x 3]
#
#      ID weeek.number amount
#   (dbl)        (dbl)  (dbl)
#1      3            1      0
#2      3            2      0
#3      3            3      0
#4      3            4    101
#5      3            5      0
#6      3            6    135
#7      3            7      0
#8      3            8    101
#9      3            9      0
#10     3           10     68
#..   ...          ...    ...

An approach in base R would be to use expand.grid and merge:

newdf <- merge(expand.grid(ID = unique(df$ID), weeek.number = 1:10), df, all.x = TRUE)
newdf$amount[is.na(newdf$amount)] <- 0   # replace NA with 0
talat
  • 68,970
  • 21
  • 126
  • 157
6

With data.table (tx to Frank for correcting the length of the result):

require(data.table)
dt<-as.data.table(df)
f<-function(x,y,len=max(y)) {res<-numeric(len);res[y]<-x;res}
dt[,list(amount=f(amount,weeek.number,10)),by=ID]
#     ID amount
# 1:   3      0
# 2:   3      0
# 3:   3      0
# 4:   3    101
# 5:   3      0
# 6:   3    135
# 7:   3      0
# 8:   3    101
# 9:   3      0
#10:   3     68
# ......

Edit

I just noticed that your amount and weeek.number actually define a sparseVector, i.e. a vector made mainly of zeroes where just the indices of the non-zero elements is kept. So, you can try with the Matrix package:

require(Matrix)
dt[,list(as.vector(sparseVector(amount,weeek.number,10))),by=ID]

to get the same result as above.

nicola
  • 24,005
  • 3
  • 35
  • 56
  • 1
    You are right, I'm gonna make an edit. Tx for noticing. – nicola Feb 29 '16 at 20:41
  • Thanks! Both solutions really elegant. I think that, for my case, the sparseVector is more useful, because I will have more columns that should remain unchanged when expanding the data frame. Thanks again! – Andres Feb 29 '16 at 21:58