Convert a date vector into Julian day in R
Try the following to convert from class character
(i.e. text) to class POSIXlt
, and then extract Julian day (yday
):
tmp <- as.POSIXlt("16Jun10", format = "%d%b%y")
tmp$yday
# [1] 166
For more details on function settings:
?POSIXlt
?DateTimeClasses
Another option is to use a Date
class, and then use format
to extract a julian day (notice that this class define julian days between 1:366, while POSIXlt is 0:365):
tmp <- as.Date("16Jun10", format = "%d%b%y")
format(tmp, "%j")
# [1] "167"
Converting time vectors into Julian days
The second column seems to be the number of seconds since the current day started. 24 hours / day * 3600 seconds per hour is 86400 seconds per day. So SecondColumn/86400
gives the decimals of the Julian date. The second columns doesn't hold information on the date itself.
Julian day to date vector
That is interesting puzzle. We know that as.POSIXlt
contains the day of the year number and that some date libraries convert to it, but I could not immediately find a parser that dealt with it.
Then again, date arithmentic is all we need. We always get the date of January 1. And the desired date is then simply the Jan 1 plus the 'day-of-year' number minus 1.
Codeyearyearday <- function(yr, yd) {
base <- as.Date(paste0(yr, "-01-01")) # take Jan 1 of year
day <- base + yd - 1
}
set.seed(42) # make it reproducible
sample <- data.frame(year=1980:2020, doy=as.integer(rnorm(41,mean=91.1,sd=9.65)))
sample$date <- yearyearday(sample$year, sample$doy)
head(sample)
OutputR> yearyearday <- function(yr, yd) {
+ base <- as.Date(paste0(yr, "-01-01")) # take Jan 1 of year
+ day <- base + yd - 1
+ }
R>
R> set.seed(42) # make it reproducible
R> sample <- data.frame(year=1980:2020,
+ doy=as.integer(rnorm(41, mean=91.1, sd=9.65)))
R>
R> sample$date <- yearyearday(sample$year, sample$doy)
R>
R> head(sample)
year doy date
1 1980 104 1980-04-13
2 1981 85 1981-03-26
3 1982 94 1982-04-04
4 1983 97 1983-04-07
5 1984 95 1984-04-04
6 1985 90 1985-03-31
R>
As so often with date calculation, nothings besides base R is needed.
Join year and julian day as a date column in R and plot it
To convert a Julian date to a Date object use the %j format. For example:
temp <- rep(runif(730,15,40))
doy <- rep(c(1:365),2)
year <- c("2015","2016")
jdate<-as.Date(paste(year, doy, sep="-"),"%Y-%j")
df<- data.frame(jdate, temp)
library(ggplot2)
f<-ggplot(df, aes(x=jdate, y=temp))
f+ geom_point() + scale_x_date(date_breaks="6 week", date_labels="%Y-%j")
Generate first and last Julian day of each month
Call your date sequence of the firsts of each month starts
. Then define ends = starts - 1
. Then the julian days are format(c(starts, ends), "%j")
. Use 2002 for the sequence so that the previous year isn't a leap year.
starts = seq(as.Date("2002/1/1"), by = "month", length.out = 12)
ends = starts - 1
x = as.numeric(format(c(starts, ends), "%j"))
sort(x)
# [1] 1 31 32 59 60 90 91 120 121 151 152 181 182 212 213 243 244 273 274 304 305
# [22] 334 335 365
Related Topics
How to Install a Package from a Download Zip File
How to Delete Rows from a Data.Frame, Based on an External List, Using R
How to Return Number of Decimal Places in R
One-Hot Encoding in [R] | Categorical to Dummy Variables
Use Ggpairs to Create This Plot
R: Lm() Result Differs When Using 'Weights' Argument and When Using Manually Reweighted Data
How to Redirect Console Output to a Variable
How to Add a Factor Column to Dataframe Based on a Conditional Statement from Another Column
R - When Trying to Install Package: Internetopenurl Failed
Rcpp Function Check If Missing Value
Rstudio Shiny List from Checking Rows in Datatables
R: How to Run Some Code on Load of Package
Sparse Matrix to a Data Frame in R
Ggplot Replace Count with Percentage in Geom_Bar
R Grep: Is There an and Operator
Protect/Encrypt R Package Code for Distribution