Reputation: 558
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
Upvotes: 6
Views: 3951
Reputation: 333
You can use the following:
as.Date(paste("1976", 53), format="%Y %j")
[1] "1976-02-22"
The strptime
function produces a list rather than a date value, which might be problematic sometimes.
Upvotes: 0
Reputation: 5424
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"
Upvotes: 10