I have a data set that looks like this:
ByYear <- data.frame( V1 = c(2005,2006,2007,2008,2005,2006,2008,2006,2007,2005,2006,2007,2008),
V2 = c(0.5,0.2,1,1.6,2,5,8,4,3,6,8,6,5),
V3 = c('A','A','A','A','B','B','B','C','C','D','D','D','D'))
Which gives me
> ByYear
V1 V2 V3
1 2005 0.5 A
2 2006 0.2 A
3 2007 1.0 A
4 2008 1.6 A
5 2005 2.0 B
6 2006 5.0 B
7 2008 8.0 B
8 2006 4.0 C
9 2007 3.0 C
10 2005 6.0 D
11 2006 8.0 D
12 2007 6.0 D
13 2008 5.0 D
Some of the years are missing from V1. This is due to an error in how the data were entered. I know this is a touchy subject, but I know for a fact that in this case a missing year in V1 means the value in V2 should be 0.
Is there a way I can create a new data set that adds a row with a zero value to any missing year like so:
> ByYear
V1 V2 V3
2005 0.5 A
2006 0.2 A
2007 1.0 A
2008 1.6 A
2005 2.0 B
2006 5.0 B
2007 0.0 B
2008 8.0 B
2005 0.0 C
2006 4.0 C
2007 3.0 C
2008 0.0 C
2005 6.0 D
2006 0.0 D
2007 6.0 D
2008 5.0 D
Thanks for everyone for all your help!
Use
tableto find the missing year/group combinations.Set the value of
V2to be0(orNAor whatver you want), then append this to your original dataset.