Re: [R] reformat time from hhmm

From: William Dunlap <wdunlap_at_tibco.com>
Date: Thu, 03 Jun 2010 18:10:31 -0700

> -----Original Message-----
> From: r-help-bounces_at_r-project.org
> [mailto:r-help-bounces_at_r-project.org] On Behalf Of Peter Moore
> Sent: Thursday, June 03, 2010 2:22 PM
> To: r-help_at_r-project.org
> Subject: [R] reformat time from hhmm
>
> Hi,
> I'm newish to R, a recent convert from Matlab... So far I'm
> impressed, and
> determined to solve the following problem, which seems like
> it should be
> easy:
> I have a long (millions of points) data series recorded with
> a datalogger
> that produced a timestamp in 4 columns: Year, Day of Year,
> Time in (H)HMM
> and Seconds. I would like to have R interpret these columns as a time
> object and have made some progress (e.g., using paste() to
> create a single
> column and then strptime() to interpret -- is that too
> one thing is throwing me off and I can't seem to conquer it. The
> hour-minute column in the raw data has no colon, so noon
> looks like "1200".
> Morning times have only 3 characters and afternoon times have
> 4. I've been
> playing around with a fake set of times:
> times <- c(110, 230, 459, 1001, 1238, 1922)
>
> When I use
> strptime(data, "%k%M"

You must have done this with 'times', not 'data'. strptime's first argument should be character data, not numeric and the default conversion of numeric to character changes 110->"110", not "0110". I like to use sprintf() (with its C syntax) to control the conversion:

> strptime(sprintf("%04d", times), "%k%M")
[1] "2010-06-03 01:10:00" "2010-06-03 02:30:00" "2010-06-03 04:59:00" "2010-06-03 10:01:00" "2010-06-03 12:38:00" [6] "2010-06-03 19:22:00"

You could put the year-month-day part into the sprintf's format argument as well if you don't want it to use today's date for that.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com

> the last three are interpreted fine but the first three are messed up
> because, for some reason, (even though I use %k for hour
> format?) the first
> two characters are assumed to be hour and the remaining one
> is minutes. For
> times[3] I get NA because R doesn't know what to do with 45 hours...
> [1] "2010-06-03 11:00:00" "2010-06-03 23:00:00" NA
> [4] "2010-06-03 10:01:00" "2010-06-03 12:38:00"
> "2010-06-03 19:22:00"
>
> Fair enough, so I tried a different angle, using an if...else
> statement:
> hours <- if(nchar(times)>3) strtrim(times,2) else strtrim(times,1)
>
> This worked great when times was only a vector of length=1,
> but when I try
> to apply it to something larger, I get the following warning:
> Warning message:
> In if(nchar(times)>3) strtrim(times,2) else strtrim(times,1) :
> the condition has length > 1 and only the first element
> will be used
> and the output hours are only the first character. Not
> entirely sure if I
> understand this.
>
> Any advice on how to do this? Are there packages or commands
> that I'm not
> aware of that know how to deal with (h)hmm times?
>
> Thanks much,
> -Pete
> ---------------------------------------------
> platform i486-pc-linux-gnu
> arch i486
> os linux-gnu
> system i486, linux-gnu
> status
> major 2
> minor 10.1
> year 2009
> month 12
> day 14
> svn rev 50720
> language R
> version.string R version 2.10.1 (2009-12-14)
>
> --
> Pete Moore
> Postdoctoral Research Associate
> Dept. Geological & Atmospheric Sciences
> Iowa State University
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help