0

I want to add a row of NAs in R. My initial data frame looks as follows:

df <- data.frame(year=c(2000:2005, 2003:2005), 
                 company=c(rep("a",6), rep("b",3)),
                 var1 = rnorm(9), 
                 var2 = rnorm(9))

df

  year company        var1       var2
1 2000       a -0.06332843 -1.5157491
2 2001       a  1.12602096  0.1053548
3 2002       a  1.27486075  0.7764425
4 2003       a -0.76522043 -0.4277408
5 2004       a -0.26060052  2.3909792
6 2005       a -0.46778331 -0.1224180
7 2003       b  1.24962114  0.9329322
8 2004       b -2.41139620 -1.4730372
9 2005       b -0.85442105 -0.8134254

So I have a year and a company index and two variables var1 and var2. I observe data from 2000 until 2005. We see that the years 2000 until 2002 are completely missing for company b. I want to add these missing values to my data frame in a way that I achieve the following result:

   year company        var1       var2
1  2000       a -0.06332843 -1.5157491
2  2001       a  1.12602096  0.1053548
3  2002       a  1.27486075  0.7764425
4  2003       a -0.76522043 -0.4277408
5  2004       a -0.26060052  2.3909792
6  2005       a -0.46778331 -0.1224180
7  2000       b          NA         NA
8  2001       b          NA         NA
9  2002       b          NA         NA
10 2003       b  1.24962114  0.9329322
11 2004       b -2.41139620 -1.4730372
12 2005       b -0.85442105 -0.8134254

Is there a way to achieve this? Thanks!

Dirk Buttke
  • 355
  • 1
  • 8

0 Answers0