I have a ts column that saves years in decimal format ie
1988.0
1988.25
1988.5
1988.75
and so on. I need to export it to a .csv file with the date in a "DD-MM-YYYY"
format.
How do I do this?
I have a ts column that saves years in decimal format ie
1988.0
1988.25
1988.5
1988.75
and so on. I need to export it to a .csv file with the date in a "DD-MM-YYYY"
format.
How do I do this?
To deal with those year-fractions,
> date_string <- '1988.99'
> date_num <- as.numeric(date_string)
> year <- floor(date_num)
> year_beginning <- as.POSIXct(paste0(year, '-01-01'))
> year_end <- as.POSIXct(paste0(year+1, '-01-01'))
> year_length <- year_end - year_beginning
> year_fraction <- date_num %% 1
> date <- year_beginning + year_length * year_fraction
> format(date, format='%Y-%m-%d')
[1] "1988-12-28"
For writing a csv, try help(write.table)
The
lubridate
package has a function,date_decimal
that you can use for this.Then you can write it to a csv with
You'll probably want to check the output first and adjust some of the arguments to whatever format you want.