Skip to content
Prev 386849 / 398502 Next

Converting "day of year" to "year", "month" and "day"

Hello,

Thanks for the data, it makes things easier.

df1 <- read.table("Jibrin_data.txt", header = TRUE)
#'data.frame':	168 obs. of  4 variables:
# $ year: int  1998 1998 1998 1998 1998 1998 1998 1998 1998 1998 ...
# $ day : int  1 2 3 4 5 6 7 8 9 10 ...
# $ Hr  : int  0 0 0 0 0 0 0 0 0 0 ...
# $ SWS : num  344 346 356 332 302 329 395 359 471 392 ...

Here is a simple way of converting the year and day of year columns to a 
column of class "Date".
Like others have said, there are also CRAN packages to handle date/time 
data, my favorite being package lubridate, but base R can do it.


df1$date <- as.Date(paste(df1$year, df1$day),
                     format = "%Y %j",
                     origin = "1998-01-01")

head(df1)
#  year day Hr SWS       date
#1 1998   1  0 344 1998-01-01
#2 1998   2  0 346 1998-01-02
#3 1998   3  0 356 1998-01-03
#4 1998   4  0 332 1998-01-04
#5 1998   5  0 302 1998-01-05
#6 1998   6  0 329 1998-01-06


Hope this helps,

Rui Barradas


?s 06:48 de 16/01/21, Jibrin Alhassan escreveu: