Reputation: 851
I have a year column in my dataframe, which is formatted as financial year (e.g. 2015-16, 2016-17, etc). I want to change them to just 4-digit year in such a way that 2015-16 becomes 2016; 2016-17 becomes 2017, etc. How can I do it?
Upvotes: 1
Views: 70
Reputation: 886938
We can use sub
to capture the first two digits while leaving the next two digits and the -
, and in the replacement, specify the backreference (\\1
) of the captured group
as.numeric(sub("^(\\d{2})\\d{2}-", "\\1", v1))
#[1] 2016 2017
Or more compactly match the two digits followed by the -
, and replace with blank (''
)
sub("\\d{2}-", "", v1)
[1] "2016" "2017"
Or using substr
paste0(substr(v1,1, 2), substr(v1, 6, 7))
#[1] "2016" "2017"
NOTE: None of the solutions require any external packages. Also, it doesn't implicitly assume there is always an increment of 1 year. It can be any year range as below and it works
v2 <- c("2015-18", "2014-15", "2012-19")
sub("\\d{2}-", "", v2)
#[1] "2018" "2015" "2019"
v1 <- c("2015-16", "2016-17")
Upvotes: 0
Reputation: 388807
You can use parse_number
from readr
:
x <- c('2015-16', '2016-17')
readr::parse_number(x) + 1
#[1] 2016 2017
parse_number
drops any non-numeric characters before or after the first number. So in this example, everything after the first number is dropped and turned to numeric. We then add 1 to to it to get next year.
Upvotes: 2
Reputation: 51582
A possible solution can be,
as.numeric(sub('-.*', '', '2015-16')) + 1
#[1] 2016
Upvotes: 1