Sam Albers
2011-May-10 17:16 UTC
[R] Converting ordinal dates and time into sensible formats
Hello all, I am having a little trouble working with "strptime" and I was hoping someone might be able to give me a hand. I have an instrument that outputs an ordinal date and time in two columns something like this: day.hour min.sec 1 12525 2050 2 12518 2029 3 12524 2023 4 12524 2028 5 12507 2035 Now the problem I am having is converting these numbers into dates and times. I am able to convert these into their respective POSIXlt formats but I am left with two columns where hour is left with the date (data$Only.Date) and the date is left with the time (data$Only.Time). Can anyone recommend a good way to convert these ordinal dates into something like the following? day.hour min.sec Date Time 12511 2033 2011-05-05 11:20:33 ## A trivial example ##a data frame day.hour <-as.integer(runif(5, 12500, 12523)) #First 3 digits are the day of the year, last 2 are the hour of the day data <- as.data.frame(day.hour) data$min.sec <-as.integer(runif(5, 2000, 2060)) #First 2 digits are the minute, last 2 are the seconds ##example of how things get a little jumbled. strptime was easy enough to use. data$Date <- strptime(data$day.hour, format="%j%H") data$Time <- strptime(data$min.sec, format="%M%S") data Using Ubuntu 10.10 and R 2.11.1. Thanks in advance!!!! Sam -- ***************************************************** Sam Albers Geography Program University of Northern British Columbia 3333 University Way Prince George, British Columbia Canada, V2N 4Z9 ***************************************************** [[alternative HTML version deleted]]
Steven Kennedy
2011-May-10 22:01 UTC
[R] Converting ordinal dates and time into sensible formats
How about something like:> data$DateTime<- strptime(paste(data$day.hour,data$min.sec,sep=" "),format="%j%H %M%S") > data$Date <- strftime(data$DateTime,format="%Y-%m-%d") > data$Time <- strftime(data$DateTime,format="%H:%M:%S") > dataday.hour min.sec DateTime Date Time 1 12517 2034 2011-05-05 17:20:34 2011-05-05 17:20:34 2 12520 2004 2011-05-05 20:20:04 2011-05-05 20:20:04 3 12507 2045 2011-05-05 07:20:45 2011-05-05 07:20:45 4 12502 2000 2011-05-05 02:20:00 2011-05-05 02:20:00 5 12500 2004 2011-05-05 00:20:04 2011-05-05 00:20:04