rtimenas

fill in NAs for time variable - R


I have the following dataset:

Variable | Year | Value
A        | 2000 | 0.0003
B        | 2000 | 0.31
B        | 2001 | 0.09
C        | 2002 | 0.03

And I want to create a new dataset such as:

Variable | Year | Value  
A        | 2000 | 0.0003  
A        | 2001 | NA         
A        | 2002 | NA             
B        | 2000 | 0.31  
B        | 2001 | 0.09   
B        | 2022 | NA      
C        | 2000 | NA             
C        | 2001 | NA     
C        | 2002 | 0.03    

I have more columns in my actual dataset. Does anyone know of a quick way to do this?

Thanks!


Solution

  • You can use tidyr::complete()

    tidyr::complete(df,Variable, Year)
    

    Output:

      Variable  Year   Value
      <chr>    <dbl>   <dbl>
    1 A         2000  0.0003
    2 A         2001 NA     
    3 A         2002 NA     
    4 B         2000  0.31  
    5 B         2001  0.09  
    6 B         2002 NA     
    7 C         2000 NA     
    8 C         2001 NA     
    9 C         2002  0.03  
    

    Input:

    structure(list(Variable = c("A", "B", "B", "C"), Year = c(2000, 
    2000, 2001, 2002), Value = c(3e-04, 0.31, 0.09, 0.03)), class = "data.frame", row.names = c(NA, 
    -4L))