Year Winner 2019-2020 100 2019-2020 500 2019-2020 10000 2020-2021 100559 2020-2021 114431 2020-2021 121461 2021-2022 200233
I have a dataset similar to the above, am seeking help onn how to extract a calendar year from the fiscal year using R
CodePudding user response:
Are you trying to extract the first (or second) part of the year column to get a cal_yr column like this? Or have I misunderstood the question?
library(tidyverse)
tribble(
~year, ~winner,
"2019-2020", 100,
"2019-2020", 500,
"2019-2020", 10000,
"2020-2021", 100559,
"2020-2021", 114431,
"2020-2021", 121461,
"2021-2022", 200233
) |>
mutate(cal_yr = str_remove(year, "-.*"))
#> # A tibble: 7 × 3
#> year winner cal_yr
#> <chr> <dbl> <chr>
#> 1 2019-2020 100 2019
#> 2 2019-2020 500 2019
#> 3 2019-2020 10000 2019
#> 4 2020-2021 100559 2020
#> 5 2020-2021 114431 2020
#> 6 2020-2021 121461 2020
#> 7 2021-2022 200233 2021
Created on 2022-04-20 by the reprex package (v2.0.1)