R treating time
There is an alternative through strsplit() when the desired output format is numeric (fractional hours on 0-24 scale).
aa <- c("3:00","11:42")
bb <- strsplit(aa,":")
bb
[[1]] [1] "3" "00" [[2]] [1] "11" "42"
cc <- sapply(bb,function(x){as.numeric(x[1])+as.numeric(x[2])/60})
cc
[1] 3.0 11.7 AM/PM can be dealt with similarly.
aa <- c("3:00pm","11:42am")
pm <- grepl("pm",aa)
pm
[1] TRUE FALSE
bb <- strsplit(sub("[ap]m$","",aa),":")
bb
[[1]] [1] "3" "00" [[2]] [1] "11" "42"
cc <- sapply(bb,function(x){as.numeric(x[1])+as.numeric(x[2])/60})+12*pm
cc
[1] 15.0 11.7 Cheers, M. Kulich
On 7.1.2010 8:26, Dennis Murphy wrote:
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 at 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 at 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]]
______________________________________________ R-help at 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.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Michal Kulich, PhD Dept. of Probability and Statistics Charles University Sokolovska 83 186 75 Praha 8 Czech Republic Phone +420-221-913-229 Fax: +420-283-073-341, +420-222-323-316 Email: kulich at karlin.mff.cuni.cz