0

I am creating an R function that needs to adjust the income values for specific years to the 2018 USD value. The previous years are 1972, 1980, 1990, 2000, and 2010. I have a variable in my dataset that is called "year" and a variable called "income".

In the function, I need to use loops to cycle through the years and adjust the income value for each observation within each year to the 2018 USD value. I have the conversion rates for each year, but I am not sure how to create a loop that cycles through each year and does the specific conversion for each year.

This is my first time creating a function and my first time really using loops. I tried to do this but it is not producing any values in my dataframe:

incomeadj=NA
uslop2 <- cbind(uslop, incomeadj)
  adjust_income <- function(uslop2){
    if (uslop2$year == 1972){
      uslop2$incomeadj <- uslop2$income*(251.23/41.8)
    } else {NA}
  }
uslop2$incomeadj

I feel like I can eventually grasp what I need to do, but I am struggling right now. Any help is greatly appreciated.

BDizzle
  • 21
  • 3
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Oct 26 '20 at 19:46
  • [Here's my answer](https://stackoverflow.com/a/64450749/903061) to a very similar question, see if it helps you. (Also notice how the question nicely provided sample data, which made it easier to help!) – Gregor Thomas Oct 26 '20 at 19:50
  • @GregorThomas Thank you, I believe that thread puts me on the right track. The only issue I foresee running into is creating a function in a separate script to call and run the script I create. Not sure how to do that, but it has to be done this way for the class I'm taking. – BDizzle Oct 26 '20 at 19:57
  • `source()` runs all the code in an R file, e.g., `source("my_file.r")` – Gregor Thomas Oct 26 '20 at 20:27

0 Answers0