0

I've data on several macroeconomic variables for 14 Indian states pertaning to the years 1999-2020. I want to multiply each of these variables by a column of GDP deflator, i.e for each of the 21 observations for a state I want to multiply these to the GDP deflator to their corresponding year and do the same exercise for each state. How should I do this in R?

Edit1: My data is in long format.

Year State          Var1  
1999 Andhra Pradesh x     
2000 Andhra Pradesh y     
.    .              .     
.    .              .      
.    .              .     
2020 Andhra Pradesh z     
1999 Bihar          u
2000 Bihar          v
.    .              .
.    .              .
.    .              .
2020 Bihar          w


A different data frame of GDP deflator common for all states
 Year GDP_defaltor
 1999 80
 2000 100
 .    .
 .    .
 .    . 
 2020 150

Hope it helps.

  • It would be easier to help if you create a small reproducible example (dataset) along with your expected output. So everyone can test their ideas and see which one might be an answer. Without an example dataset there might be confusions about the structure of your data (long or wide-format, data types ...) which might lead to answers not fitting with your original dataset. Here are some information about reproducible examples: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – tamtam Mar 05 '21 at 12:23
  • Hi. As asked, I've added a example of the data frame I'm working with. – Sanket_Shrium Mar 05 '21 at 12:39

1 Answers1

0

You should join the two datasets and then do the multiplication. Your example is not reproducible so I just put some fake code here, either adapt it or make your post reproducible:

library(dplyr)

# Supposing data_1 is your panel data, and data_2 is the GDP dataset
new_data <- left_join(data_1, data_2, by = "Year") %>%
  mutate(var_2 = var_1*gdp_deflator)
bretauv
  • 7,756
  • 2
  • 20
  • 57