简体   繁体   中英

R two digit year into four

Stumped on easiest way to append a current year column (only has last two digits 21, 22, 23) to include first two digits (in this case only "20"). I've tried str_glue and past0("20", variable) but had no luck. Any functions/help would be super appreciated. Also, the current year column is in a character type, not a date currently.

test <- tibble(rep= "John Smith", quarter= "q2", year= 19:23, ideal_year= 2019:2023) %>% mutate(year=as.character(year))

Easiest way seems to be:

tibble(rep= "John Smith", year= 19:23, ideal_year= 2019:2023) %>% 
    mutate(
        year = year + 2000
    )
# A tibble: 5 x 3
  rep         year ideal_year
  <chr>      <dbl>      <int>
1 John Smith  2019       2019
2 John Smith  2020       2020
3 John Smith  2021       2021
4 John Smith  2022       2022
5 John Smith  2023       2023

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM