How to convert DoY column to date column

Hello,

I want to convert a column, day of year (DoY) of the dataframe df to date format into a new column called date for the year 2015. However, my code is giving me error "Error in charToDate(x): character string in not in a unambiguous format".

# Here is my code
df$date<-rep(0, nrow(df))
df$date<- as.Date(df$DoY,  origin = "2015-01-01")

Something like this should work. as.Date(104, origin = "2015-01-01")

What format is df$DoY in?

df <- data.frame(DoY = seq(1:10))
df$date <- as.Date(df$DoY, origin = "2015-01-01")

> df
   DoY       date
1    1 2015-01-02
2    2 2015-01-03
3    3 2015-01-04
4    4 2015-01-05
5    5 2015-01-06
6    6 2015-01-07
7    7 2015-01-08
8    8 2015-01-09
9    9 2015-01-10
10  10 2015-01-11

Thank you, I had a syntax error. I code works fine.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.