Reputation: 233
I have a matrix with separate columns for year (YYYY), Julian day, and time (HHMM). I would like to attach a column vector with the full Gregorian date ("MM/DD/YYYY") and also a column vector which will give me the elapsed time in days. How would I go about this?
To be clear, I am referring to these Julian days: http://landweb.nascom.nasa.gov/browse/calendar.html
Thanks for your help.
C7 <- read.table(text="Year Day Time
2015 193 915
2015 193 930
2015 195 1400
2015 195 1415", header=T)
I would like to add these two columns:
Year Day Time Date Elapsed Time (Days)
2015 193 915 07/12/2015 0.00
2015 193 930 07/12/2015 0.01
2015 195 1400 07/14/2015 2.20
2015 195 1415 07/14/2015 2.21
Upvotes: 0
Views: 1238
Reputation: 206421
First, I'd convert the values into proper POSIXt date/time values. Here I paste everything in to a string, separating out the hours and minutes using some math
pdates <- with(C7, strptime(paste(Year,Day,Time %/% 100, Time %% 100), "%Y %j %H %M"))
Now we can get your desired columns by downcasting to Date and using difftime()
.
format(pdates, "%m/%d/%Y")
# [1] "07/12/2015" "07/12/2015" "07/14/2015" "07/14/2015"
round(c(difftime(pdates, min(pdates), units="days")),2)
# [1] 0.00 0.01 2.20 2.21
Upvotes: 2