简体   繁体   English

R 两位数年份为四

[英]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").很难用最简单的方法来附加当前年份列(只有最后两位数字 21、22、23)以包括前两位数字(在这种情况下只有“20”)。 I've tried str_glue and past0("20", variable) but had no luck.我试过 str_glue 和 past0("20", variable) 但没有运气。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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