Hi:

Let
x <- '3:00'

The R package for data that are only dates or only times is chron. For
times,
it appears that the strings need to be in the form hh:mm:ss on a 24-hour
clock, so for
example, 1:30 PM should be expressed as 13:30:00. [I didn't see any option
for a
12-hour clock with an optional argument to designate AM or PM...if wrong,
I'm
sure I'll be corrected...]


Using the x value above, we have

> library(chron)
> times(x)
> x <- '3:00'
> times(x)
Error in convert.times(times., fmt) : format h:m:s may be incorrect
In addition: Warning messages:
1: In unpaste(times, sep = fmt$sep, fnames = fmt$periods, nfields = 3) :
  wrong number of fields in entry(ies) 1
2: In convert.times(times., fmt) : NAs introduced by coercion
3: In convert.times(times., fmt) : NAs introduced by coercion
4: In convert.times(times., fmt) : NAs introduced by coercion

So '3:00' isn't enough; we need to add on some seconds...

> x <- paste(x, ':00', sep = '')
> x
[1] "3:00:00"
> times(x)                             # Now it works.
[1] 03:00:00

To show you that this works equally well with vectors, suppose the
input times were a vector
z <- c('3:00', '4:15', '12:25', '16:41')

# Use the same trick as above (paste() is also vectorized)..
z <- paste(z, ':00', sep = '')
times(z)
# [1] 03:00:00 04:15:00 12:25:00 16:41:00


Consult ?chron and the examples contained within. You can run the
examples on the help page with example(chron).

HTH,
Dennis


On Wed, Jan 6, 2010 at 9:32 PM, chrisli1223 <chrisli@austwaterenv.com.au>wrote:

>
> Hi all,
>
> I have imported a value 3:00 from Excel into R using read.csv. I want R to
> recognise it as 3:00am (time data). How do I do it?
>
> Thanks in advance,
> Chris
> --
> View this message in context:
> http://n4.nabble.com/R-treating-time-tp1008608p1008608.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]

