Search code examples
rvisualizationmultiple-columns

How can I change my financial year column to continuous data in r?


I am very new to r and coding in general but hope someone can help, even if this is super basic.

I have a large data set that includes a year column which I want to use in a histogram. The issue is, the years are submitted as calendar years (2010/11, 2011/12, 2012/13 etc.) and are therefore picked up as col_character() / discrete data.

Is there an easy way to fix this column to avoid it being discrete data?

I looked up the fy package which I could not get to work and some of the other questions about financial years on here.

I also tried separating the column into "Start Year" and "End Year". The issue then was turning it back into one column.


Solution

  • One way is to use read.delim with the appropiate parameters:

    # example df
    df <- data.frame(year=c("2010/11", "2011/12", "2012/13"), id = 1:3)
    
    df$year <- read.delim(text=df$year, sep="/",header=F, col.names = c("from", "to"))
    df <- as.data.frame(df)  # to unnest "year" columns
    df
    #>   year.from year.to id
    #> 1      2010      11  1
    #> 2      2011      12  2
    #> 3      2012      13  3
    

    if you want to convert year.to in full years simply do something like:df$year.to <- df$year.to + ifelse(df$year.to > 70, 1900, 2000)