r两位数分为四年

发布于 2025-02-13 23:59:44 字数 287 浏览 1 评论 0原文

以最简单的方式添加当前一年的列(只有最后两位数字21、22、23),以包括前两个数字(在这种情况下,只有“ 20”)。我尝试过str_glue和past0(“ 20”,可变),但没有运气。任何功能/帮助都将不胜感激。另外,当前年份的列是字符类型,而不是当前的日期。

test&lt; - tibble(rep =“ John Smith”,四分之一=“ Q2”,年= 19:23,Ideal_year = 2019:2023)%&gt;%突变(年= as.Character(Year))< /代码>

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))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃兔兔 2025-02-20 23:59:44

最简单的方法似乎是:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文