Search code examples
rgregorian-calendar

R convert date column into gregorian calendar


I would like to convert a column of a dataframe into gregorian date. For example the following dataframe:

       Date_R Tmax
1  1985-01-01  9.0
2  1985-02-02  9.0
3  1985-12-31 11.0
4  1986-01-04  8.5
5  1986-01-05 11.0

Into:

  Date_R Tmax
1      1  9.0
2     33  9.0
3    365 11.0
4      4  8.5
5      5 11.0

Or add a new column with this information.

Thanks in advance


Solution

  • Or you can use as.POSIXlt

    as.POSIXlt(df$Date_R)$yday + 1L
    ## [1]   1  33 365   4   5