Hi,
I have to recode dates into specific ranges (for example financial year). I've done my coding but running that on my large data file id the most time consuming R process since I started playing with R. Is any easier/quicker way of running my code?
data.source <- data.frame(stringsAsFactors=FALSE,
ReferenceNumber = c("08d4-ea84-4cca", "08d4-ea84-4cca", "08d4-ea84-4cca",
"08d4-ea84-4cca", "137f-dd7f-4120",
"3302-f362-4244", "3302-f362-4244",
"3302-f362-4244", "69f4-af0b-4640", "69f4-af0b-4640",
"69f4-af0b-4640", "ac31-5bab-4798",
"ac31-5bab-4798", "ac31-5bab-4798", "ac31-5bab-4798",
"ac31-5bab-4798", "ac31-5bab-4798",
"ac31-5bab-4798", "ac31-5bab-4798", "e0e5-7e8b-497c",
"e0e5-7e8b-497c", "e0e5-7e8b-497c",
"e0e5-7e8b-497c"),
RoDate = c("2014-06-10", "2013-03-15", "2013-03-05", "2012-12-17",
"2019-06-26", "2017-10-20", "2018-10-24",
"2017-01-27", "2015-10-07", "2015-06-03",
"2014-02-27", "2016-11-24", "2015-11-24",
"2015-01-09", "2014-11-28", "2013-11-26",
"2012-11-27", "2011-11-29", "2011-11-24",
"2014-06-05", "2014-06-05", "2014-03-21",
"2014-01-15")
)
library(dplyr)
date.range <- data.source %>% mutate(
ReportingYear = case_when(
RoDate >= "2011-10-05" & RoDate < "2012-10-05" ~ 1,
RoDate >= "2012-10-05" & RoDate < "2013-10-05" ~ 2,
RoDate >= "2013-10-05" & RoDate < "2014-10-05" ~ 3,
RoDate >= "2014-10-05" & RoDate < "2015-10-05" ~ 4,
RoDate >= "2015-10-05" & RoDate < "2016-10-05" ~ 5,
RoDate >= "2016-10-05" & RoDate < "2017-10-05" ~ 6,
RoDate >= "2017-10-05" & RoDate < "2018-10-05" ~ 7,
RoDate >= "2018-10-05" & RoDate < "2019-10-05" ~ 8))
Thank you in advance