1

New to R, I'm struggling to change the reference date of one of my variables to that of another date. Any help would be much appreciated.

The current reference year is 2002 and I need to change the reference year so that it is 2015. Also, all my values in this column needs to be updated in accordance to the changed reference year. Thanks.

tsunami_data colnames(tsunami_data)[1] <- "years_before"
Currently the years before are in relation to the date 2002 and I need to update to that of 2015, so that the years before become larger because of the shift in the date. Aslo, I am using a large data set at present. thanks.

e.g.

years_before

  • 7
  • 56
  • 87
  • 45
  • 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 21 '21 at 22:05

1 Answers1

2

it is hard to understand what you want. Here I created a new variable and added the difference between 2002 and 2015.

library(dplyr)
df <- data_frame(years_before_2002 = c(7,56,87,45))
df_new <- df %>% mutate(years_before_2015 = years_before_2002 + (2015 - 2002))

#for your example 
tsunami_data <- tsunami_data %>% mutate(years_before_2015 = years_before + (2015 - 2002))

#if you would like to keep "years_before", but just adjust for 13 years, run this
tsunami_data <- tsunami_data %>% mutate(years_before = years_before + (2015 - 2002))
Bloxx
  • 1,495
  • 1
  • 9
  • 21
  • tsunami_data colnames(tsunami_data)[1] <- "years_before" Currently the years before are in relation to the date 2002 and I need to update to that of 2015, so that the years before become larger because of the shift in the date. Aslo, I am using a large data set at present. thanks. – Benson.Hobbs Oct 21 '21 at 22:39
  • If you run my code, df_new has 2 columns. Column years_befor has numbers that are higher for 13 (years), compared to year 2002. I will add the code under so, it will work for your names. – Bloxx Oct 21 '21 at 22:56