How to add an specific set of atomic vectors to an specific df column?

Viewed 61

I am trying to add an specific vector of rows to an specific df column, This is what I've done so far:

real_gdp_time_series <- real_gdp_time_series %>%
                            rbind(growth_pib_real_per_capita = c(-0.006, -0.086)) %>%
                            rbind(growth_pib_real_per_capita = c('2019-01-01', '2020-01-01')) 

And this throws the next error:

Error in as.Date.numeric(value) : 'origin' must be supplied

The desired outcome would look like this:

# año  growth_pib_real_per_capita
# ...          ...
# 2019-01-01   -0.006
# 2020-01-01   -0.086

data

structure(list(año = structure(c(6940, 8035, 8766, 9496, 10227, 
10957, 11688, 12418, 12784, 13149, 13879, 14610, 15340, 16071, 
16801, 17532), class = "Date"), growth_pib_real_per_capita = c(-0.221000495330589, 
0.00285801450939386, 0.00264240512093975, -0.00299493284604054, 
0.00423776690674324, 0.0048819479659131, -8.51835182683933e-05, 
0.00670710736839509, 0.010584695348906, 0.00241749748695944, 
0.000203934290972816, -0.00298146398803882, 0.00454536882635523, 
0.000317493839324935, 0.00399645473642857, 0.00149656209777629
)), class = "data.frame", row.names = c(NA, -16L))
1 Answers

The rbind should have all the columns of the original dataset. If we need to use only selected number of columns, then use bind_rows (from dplyr). Based on the code, it seems that the OP wanted to add both columns in rbind. In that case, either rbind a data.frame (a data.frame is a list as well) or just a list)

real_gdp_time_series %>%
            rbind(list(año = c('2019-01-01', '2020-01-01'), 
               growth_pib_real_per_capita = c(-0.006, -0.086)))

-output

         año growth_pib_real_per_capita
1  1989-01-01              -2.210005e-01
2  1992-01-01               2.858015e-03
3  1994-01-01               2.642405e-03
4  1996-01-01              -2.994933e-03
5  1998-01-01               4.237767e-03
6  2000-01-01               4.881948e-03
7  2002-01-01              -8.518352e-05
8  2004-01-01               6.707107e-03
9  2005-01-01               1.058470e-02
10 2006-01-01               2.417497e-03
11 2008-01-01               2.039343e-04
12 2010-01-01              -2.981464e-03
13 2012-01-01               4.545369e-03
14 2014-01-01               3.174938e-04
15 2016-01-01               3.996455e-03
16 2018-01-01               1.496562e-03
17 2019-01-01              -6.000000e-03
18 2020-01-01              -8.600000e-02
Related