0

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     
MrFlick
  • 195,160
  • 17
  • 277
  • 295
user507
  • 223
  • 1
  • 6
  • 14
  • 3
    Please create a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Include some sample input data and show the desired output. It's much easier to help that way. – MrFlick Jul 21 '15 at 19:43
  • Thanks for the edit. Couldn't figure out how to format it to display that way. As you can tell, I am quite new to R. – user507 Jul 21 '15 at 20:23

1 Answers1

2

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
MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • This is an old question of mine, but I was wondering if there is a way I can use the above suggested code but with an established minimum date when calculating elapsed time? I need to use a minimum date that is not already in the data frame (10/12/2014 at 12:00pm). Thanks for your help. – user507 Nov 25 '15 at 02:12