Search code examples
rtidyverseaddition

How can i add a row vector in a tibble in R?


I have a tibble in R that has 11 observations of every month.Apart from June that has 0. My data frame (tibble) looks like this :

library(tidyverse)
A = c(1,2,3,4,5,7,8,9,10,11,12)
B = rnorm(11,0,1)
Data = tibble(A,B);Data

But i want to add the 0 observation of June of this timeseries. Something like :

d = c(6,0);d
newdata = rbind(Data,d)
order(newdata$A)

but the 12 (december) appears.Any help?


Solution

  • Two approaches:

    (1) We can use add_row for this. However, d must be named and we need to splice it into add_row with the tribble bang !!! operator. Then we can arrange the data so that the month are sorted from 1 to 12. Of course you can specify add_row directly like in @Chris answer without the need of an external vector.

    library(dplyr)
    
    A = c(1,2,3,4,5,7,8,9,10,11,12)
    B = rnorm(11,0,1)
    Data = tibble(A,B)
    
    d = c(A = 6, B = 0)
    
    newdata <- Data %>% 
      add_row(!!! d) %>% 
      arrange(A)
    
    # check
    newdata
    #> # A tibble: 12 x 2
    #>        A        B
    #>    <dbl>    <dbl>
    #>  1     1  1.22   
    #>  2     2  0.0729 
    #>  3     3  0.597  
    #>  4     4 -1.26   
    #>  5     5  0.928  
    #>  6     6  0      
    #>  7     7 -1.08   
    #>  8     8  0.704  
    #>  9     9 -0.119  
    #> 10    10 -0.462  
    #> 11    11 -0.00388
    #> 12    12  1.56
    
    order(newdata$A) 
    #>  [1]  1  2  3  4  5  6  7  8  9 10 11 12
    

    (2) We can use tidyr::complete, as suggested by @Ronak in the comments, although we use a slightly different specification with full_seq:

    library(tidyr)
    
    Data %>% 
      complete(A = full_seq(A, 1), fill = list(B = 0))
    
    #> # A tibble: 12 x 2
    #>        A      B
    #>    <dbl>  <dbl>
    #>  1     1 -0.258
    #>  2     2 -1.18 
    #>  3     3 -0.165
    #>  4     4  0.775
    #>  5     5  0.926
    #>  6     6  0    
    #>  7     7  0.343
    #>  8     8  1.10 
    #>  9     9  0.359
    #> 10    10  0.934
    #> 11    11 -0.444
    #> 12    12  0.184
    

    Created on 2021-09-21 by the reprex package (v2.0.1)