Home > other >  Add month before the year
Add month before the year

Time:10-24

Having a data frame such as this:

d1 <- data.frame(id = c(1,2,3),
                 date1 = c("2014-2018","Jun 2009-2010","Jan 2009-Aug 2010"), 
                 date2 = c("Feb 2016-2018","2014-Dec 2018","Oct 2013-Dec 2018"))
#   id             date1             date2
# 1  1         2014-2018     Feb 2016-2018
# 2  2     Jun 2009-2010     2014-Dec 2018
# 3  3 Jan 2009-Aug 2010 Oct 2013-Dec 2018

How is it possible to add months if only year exist. More specifically if year is before "-" add Jan and if it is after add Dec.

data.frame(id = c(1,2,3),
           date1 = c("Jan 2014-Dec 2018","Jun 2009-Dec 2010","Jan 2009-Aug 2010"),
           date2 = c("Feb 2016-Dec 2018","Jan 2014-Dec 2018","Oct 2013-Dec 2018"))
#   id             date1             date2
# 1  1 Jan 2014-Dec 2018 Feb 2016-Dec 2018
# 2  2 Jun 2009-Dec 2010 Jan 2014-Dec 2018
# 3  3 Jan 2009-Aug 2010 Oct 2013-Dec 2018

CodePudding user response:

You can use gsub twice. When the string starts (^) with 4 digits, add Jan before, and when it finishes with - followed by 4 digits, add -Dec before the digits.

f <- function(x) {
  gsub("^(\\d{4})", "Jan \\1", x) |>
      gsub("-(\\d{4})$", "-Dec \\1", x = _)
}

output

d <- data.frame(id = c(1,2,3), date1 = c("2014-2018","Jun 2009-2010","Jan 2009-Aug 2010"), date2 = c("Feb 2016-2018","2014-Dec 2018","Oct 2013-Dec 2018"))

d[2:3] <- sapply(d[2:3], f)

  id             date1             date2
1  1 Jan 2014-Dec 2018 Feb 2016-Dec 2018
2  2 Jun 2009-Dec 2010 Jan 2014-Dec 2018
3  3 Jan 2009-Aug 2010 Oct 2013-Dec 2018
  • Related