【问题标题】:Changing the format of pairs of years in a vector in R更改 R 中向量中成对年份的格式
【发布时间】:2022-11-18 10:15:12
【问题描述】:

我想知道是否可以将我的Input 的格式更改为我的Desired_output

Input= c("20192020","20202021","20212022")

Desired_output= c("2019-20","2020-21","2021-22")

【问题讨论】:

    标签: r regex string substring stringr


    【解决方案1】:

    我们可以在这里使用sub()

    Input <- c("20192020", "20202021", "20212022")
    out <- sub("\d{2}(\d{2})$", "-\1", Input)
    out
    
    [1] "2019-20" "2020-21" "2021-22"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-11
      • 1970-01-01
      • 2021-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多