Controlling for a variable's initial year in r

Greetings to you all,

How can I control for the initial year in r?
I have an econometrics regression with some variables where I want to add an initial year "2000" using [R].

Let's say my data sample covers the period between 2000 - 2019. Assume I am to measure of Green Filed FDI inward investments on GDP growth. I will add my variables, including the controlling variables to the right side of the equation, but among the controlling variables, I would like to add an initial year for the GDP growth "2000".

Thanks

I think you are asking how to make a variable which is a date, whose year is 2000 ?
I use lubridate for my date related work.

lubridate::ymd("2000-01-01")
1 Like

Thanks a lot for your reply and answer.
Yes that is true. I want the to add a variable that covers all countries in my dataset but during one year "2000".

I am not sure I understand the question completely but I believe it is one of the options below.

library(gapminder)
library(tidyverse)

# Create initial year for all countries
gapminder %>% 
  group_by(country) %>% 
  mutate(init_year = year[1])
#> # A tibble: 1,704 x 7
#> # Groups:   country [142]
#>    country     continent  year lifeExp      pop gdpPercap init_year
#>    <fct>       <fct>     <int>   <dbl>    <int>     <dbl>     <int>
#>  1 Afghanistan Asia       1952    28.8  8425333      779.      1952
#>  2 Afghanistan Asia       1957    30.3  9240934      821.      1952
#>  3 Afghanistan Asia       1962    32.0 10267083      853.      1952
#>  4 Afghanistan Asia       1967    34.0 11537966      836.      1952
#>  5 Afghanistan Asia       1972    36.1 13079460      740.      1952
#>  6 Afghanistan Asia       1977    38.4 14880372      786.      1952
#>  7 Afghanistan Asia       1982    39.9 12881816      978.      1952
#>  8 Afghanistan Asia       1987    40.8 13867957      852.      1952
#>  9 Afghanistan Asia       1992    41.7 16317921      649.      1952
#> 10 Afghanistan Asia       1997    41.8 22227415      635.      1952
#> # ... with 1,694 more rows

# Filter the observation when the year is equal to initial year (e.g. 1952)
gapminder %>% 
  filter(year == "1952")
#> # A tibble: 142 x 6
#>    country     continent  year lifeExp      pop gdpPercap
#>    <fct>       <fct>     <int>   <dbl>    <int>     <dbl>
#>  1 Afghanistan Asia       1952    28.8  8425333      779.
#>  2 Albania     Europe     1952    55.2  1282697     1601.
#>  3 Algeria     Africa     1952    43.1  9279525     2449.
#>  4 Angola      Africa     1952    30.0  4232095     3521.
#>  5 Argentina   Americas   1952    62.5 17876956     5911.
#>  6 Australia   Oceania    1952    69.1  8691212    10040.
#>  7 Austria     Europe     1952    66.8  6927772     6137.
#>  8 Bahrain     Asia       1952    50.9   120447     9867.
#>  9 Bangladesh  Asia       1952    37.5 46886859      684.
#> 10 Belgium     Europe     1952    68    8730405     8343.
#> # ... with 132 more rows

If you have date and you want just the year you can use :

lubridate::year("2000-01-01")
#> [1] 2000

Thanks for the reply. Not exactly. I will clearly.

From the above dataset I want to create a column that has the gdp % for the year 1952 only. So, the init_year column will have the value of gdp % during 1952 " 0.779" for all Afghanistan country cells (same value). As I movie down to another country, the column will display the gdp % value in 1952 in all other years for the second country.

use the same pattern

library(gapminder)
library(tidyverse)

gapminder %>% 
  group_by(country) %>% 
  mutate(init_year = year[1],
         gdp_at_init_year = gdpPercap[1])

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.