I have a data frame with a sequence in \'col1\' and values in \'col2\':
col1 col2
2 0.02
5 0.12
9 0.91
13 1.13
I want to exp
I didn't see a simple merge
solution, so here is one:
res <- merge(data.frame(col1=1:max(df$col1)),df,by="col1",all.x=TRUE)
res$col2 <- ifelse(is.na(res$col2),0,res$col2)
The second line is replacing the NA
's from the merge
(left outer join) with zeros. As @Axeman points out, this can also be accomplished by:
res$col2[is.na(res$col2)] <- 0
The result is:
res
## col1 col2
##1 1 0.00
##2 2 0.02
##3 3 0.00
##4 4 0.00
##5 5 0.12
##6 6 0.00
##7 7 0.00
##8 8 0.00
##9 9 0.91
##10 10 0.00
##11 11 0.00
##12 12 0.00
##13 13 1.13