Skip to content
Prev 262657 / 398502 Next

Expand DF with all levels of a variable

here is a solution with expand.grid(),

a <- c(2,2,3,4,5,6,7,8,9)
a.cut <- cut(a, breaks=c(0,2,6,9,12,13,15,16), right=FALSE )
x <- data.frame(a, a.cut)

out <- expand.grid(a = 0, a.cut = setdiff(levels(a.cut), unique(a.cut)))
rbind(out, x)


I hope it helps.

Best,
Dimitris
On 6/14/2011 1:32 PM, Patrick Hausmann wrote: