My problem is similar to this previous question Fastest way to add rows for missing values in a data.frame?
I can't figure out how to add rows padded with "NA" when the min/max is different by group.
> red<-data.frame(project = c(6, 6, 6, 6, 6, 9, 9, 9), period =c(1, 2, 5:7, 2, 4, 5), v3=letters[1:8], v4=c("red", "yellow", recursive = T))
> red
project period v3 v4
1 6 1 a red
2 6 2 b yellow
3 6 5 c red
4 6 6 d yellow
5 6 7 e red
6 9 2 f yellow
7 9 4 g red
8 9 5 h yellow
I want it to look like:
project period v3 v4
6 1 a red
6 2 b yellow
6 3 NA NA
6 4 NA NA
6 5 c red
6 6 d yellow
6 7 e red
9 2 f yellow
9 3 NA NA
9 4 g red
9 5 h yellow
When I used
library(data.table)
DT=as.data.table(red)
setkey(DT, project, period)
DT[CJ(unique(project), seq(min(period), max(period)))]
it made each project group have 7 periods; Project 6 should have periods 1-7, but Project 9 should have periods 2-5.
I've tried fiddling with .SD[ which.max(period)], by=project]
but no cigar.
I thought it should be something simple in the seq(), but I tried seq(min(period, by=project))
with no luck
Thank you!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…