[R] zero-fill absent data

Dan Kortschak dan.kortschak at adelaide.edu.au
Tue Jan 5 23:39:50 CET 2010


Hello,

I have a set of data frames, generated by an SQL query that I am working
with. Because of the way the query was written, zero values for the
dependent variable (V2 in the example) are not recorded. Up until now
this has not been a problem.

I would like to be able to fill all absent data with 0. 

Current state of data (e.g.):
> frame<-as.data.frame(cbind(c(1:2,5:7),c(0.5,0.2,1,1.6,2)))
> frame
  V1  V2
1  1 0.5
2  2 0.2
3  5 1.0
4  6 1.6
5  7 2.0

So that frame returns:

  V1  V2
1  1 0.5
2  2 0.2
3  3 0.0
4  4 0.0
5  5 1.0
6  6 1.6
7  7 2.0


Since absent data may be beyond the last recorded point I'd like to be
able to use a terminating 0 

> frame<-as.data.frame(cbind(c(1:2,5:7,10),c(0.5,0.2,1,1.6,2,0)))
> frame
  V1  V2
1  1 0.5
2  2 0.2
3  5 1.0
4  6 1.6
5  7 2.0
6 10 0.0

So that values 7<V1<10 are zero filled.

Can anyone suggest a method to do this?

thank you for your time.
Dan



More information about the R-help mailing list