samedi 25 avril 2020

Mutate character column to adjust values for inflation with else if statement in R

I am trying to mutate a column for salary in my data frame to adjust for inflation since I have a multi-year sample, called adj_SALARY. The salary column is a character vector (indicated by unadj_SALARY), and I need to multiply the values by a ratio of Consumer Price Indices(shown below as a fraction) to convert all values to 2017 dollars. I also have columns as dummy variables indicating YEAR_2014, YEAR_2015, YEAR_2016, YEAR_2017, and YEAR_2018. I have tried running the code below and am still being met with an error message that "In if (YEAR_2014 == 1) { : the condition has length > 1 and only the first element will be used". Would love some help on the best way to set this up! Here's my code right now:

enter code here NHIS_test <- NHIS1 %>% 
   mutate(adj_SALARY = if(YEAR_2014 == 1) {
as.numeric(as.character(NHIS1$unadj_SALARY))*(242.839/230.280) }
else if(YEAR_2015 == 1) { 
  as.numeric(as.character(NHIS1$unadj_SALARY))*(242.839/233.916) }
else if (YEAR_2016 == 1) {
  as.numeric(as.character(NHIS1$unadj_SALARY))*(242.839/233.707) }
else if (YEAR_2017 == 1) {
  as.numeric(as.character(NHIS1$unadj_SALARY))*(242.839/236.916)}
else if (YEAR_2018 == 1) { 
  as.numeric(as.character(NHIS1$unadj_SALARY))*(1)})

Aucun commentaire:

Enregistrer un commentaire