Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a date from day of year

Tags:

date

r

lubridate

I am working with a data set that looks a bit like this:

    Year Date       Day_nr Value
    1976 19-02-1976 50     167
    1976 19-03-1976 79     140
    1978 05-03-1978 64     200
    1978 05-04-1978 95     200
    1999 05-05-1999 125    89
    1999 20-06-1999 171    79

I am then interested to estimate a polynomial model for each year depending on the day number as a x value. I then run a predict function to estimate the values of the model. I do that with the day numbers. My Data for the predict data frame then looks a bit like this just with more entries per year

    Year Day_nr Value
    1976 53     167
    1976 80     140
    1978 69     300
    1978 130    200
    1999 140    89
    1999 160    79

What I now would like to do is get a date out of these day numbers and the year. I thought I could do it with lubridate but I could only find the opposite direction which I use to generate the day numbers in the first place in the original data file. Working with the whole date as a predictor is not possible for me as I need the day number for other calculations.

Is there a way to easily do that?

Cheers, Sarina

like image 778
Sarina Avatar asked Sep 09 '13 07:09

Sarina


1 Answers

Google appears to have an answer: https://stat.ethz.ch/pipermail/r-help/2012-March/308013.html

Using first line from second set above as an example:

> strptime(paste("1976", 53), format="%Y %j")
[1] "1976-02-22"
like image 56
r.bot Avatar answered Sep 22 '22 07:09

r.bot