r - Add rows for missing data in a sequence -
i have data frame 3 columns give below:
- a catalog code
- spend on catalog code in given week
- the number of week in period
some dummy data below.
cat.code<-c("a","a","a","b","b","b","c","d") dol<-c(4,10,2,5,6,8,9,10) wk.purch<-c(1,2,3,1,5,2,3,4) fk.data<-data.frame(cat.code,dol,wk.purch) > fk.data cat.code dol wk.purch 1 4 1 2 10 2 3 2 3 4 b 5 1 5 b 6 5 6 b 8 2 7 c 9 3 8 d 10 4 i want add rows have dol = 0 when there no purchase in given week.
so, looking @ cat.code= a, want transform data frame add 2 more rows weeks 4 , 5 have 0 dol. have tried using expand.grid() , merge together, isn't working me.
thank you, all!
you can use merge/expand.grid
transform(merge(expand.grid(cat.code= unique(fk.data$cat.code), wk.purch=unique(fk.data$wk.purch)), fk.data, all.x=true), dol= replace(dol, is.na(dol), 0)) # cat.code wk.purch dol #1 1 4 #2 2 10 #3 3 2 #4 4 0 #5 5 0 #6 b 1 5 #7 b 2 8 #8 b 3 0 #9 b 4 0 #10 b 5 6 #11 c 1 0 #12 c 2 0 #13 c 3 9 #14 c 4 0 #15 c 5 0 #16 d 1 0 #17 d 2 0 #18 d 3 0 #19 d 4 10 #20 d 5 0 or using data.table, can use similar approach cj. convert 'data.frame' 'data.table' (setdt(fk.data), , set key column 'cat.code' , 'wk.purch'. join output of cj , change na values in 'dol' '0'.
library(data.table)#v1.9.5+ setdt(fk.data, key= c('cat.code', 'wk.purch'))[cj(cat.code=unique(cat.code), wk.purch=unique(wk.purch))][is.na(dol), dol:=0][]
Comments
Post a Comment