0

trying to establish a compare date column by looking at the existing row data in an existing Date column, generate the new column BenchDate by fixing the month and ate the same but refer to a specified year. my results should look like set year as a specific value "2018" COLUMN BenchDate and Diff is newly created

structure(list(Date = structure(c(17226, 17226, 17226, 17226, 
17226, 17226, 17226, 17226, 17226, 17226, 17226, 17226, 17226, 
17226, 17226, 17226, 17226, 17318, 17318, 17318, 17318, 17318, 
17318, 17318, 17318, 17318), class = "Date"), Value = c(1609600, 
914600, 3304800, 722200, 216500, 705200, 332900, 182200, 337700, 
237300, 100400, 6946000, 698100, 779200, 148600, 3304800, 297700, 
1677700, 899600, 3405100, 746600, 204000, 749800, 340700, 181300, 
355500)), row.names = c(NA, 26L), class = "data.frame")
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • Please read about [how to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and update your question accordingly. Include a sample of your data by pasting the output of `dput()` into your post or `dput(head())` if you have a large data frame. Also include code you have tried, any relevant errors, and expected output. If you cannot post your data, then post code for creating representative data. – LMc Aug 02 '23 at 17:07

1 Answers1

0
df$BenchDate = df$Date
library(lubridate)
year(df$BenchDate) = 2018
df
#          Date   Value  BenchDate
# 1  2017-03-01 1609600 2018-03-01
# 2  2017-03-01  914600 2018-03-01
# 3  2017-03-01 3304800 2018-03-01
# 4  2017-03-01  722200 2018-03-01
# 5  2017-03-01  216500 2018-03-01
# 6  2017-03-01  705200 2018-03-01
# 7  2017-03-01  332900 2018-03-01
# 8  2017-03-01  182200 2018-03-01
# 9  2017-03-01  337700 2018-03-01
# 10 2017-03-01  237300 2018-03-01
# 11 2017-03-01  100400 2018-03-01
# 12 2017-03-01 6946000 2018-03-01
# 13 2017-03-01  698100 2018-03-01
# 14 2017-03-01  779200 2018-03-01
# 15 2017-03-01  148600 2018-03-01
# 16 2017-03-01 3304800 2018-03-01
# 17 2017-03-01  297700 2018-03-01
# 18 2017-06-01 1677700 2018-06-01
# 19 2017-06-01  899600 2018-06-01
# 20 2017-06-01 3405100 2018-06-01
# 21 2017-06-01  746600 2018-06-01
# 22 2017-06-01  204000 2018-06-01
# 23 2017-06-01  749800 2018-06-01
# 24 2017-06-01  340700 2018-06-01
# 25 2017-06-01  181300 2018-06-01
# 26 2017-06-01  355500 2018-06-01
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294