[R] converting character vector "hh:mm" to chron or strptime 24 clock time vectors

Gabor Grothendieck ggrothendieck at gmail.com
Wed Feb 17 06:10:29 CET 2010


Try this (and note that times must be less than 24 hours):

> Lines <- "LogData       date              time
+ 1    77.16     2008/04/24     02:00
+ 2    61.78     2008/04/24     04:00
+ 3    75.44     2008/04/24     06:00
+ 4    89.43     2008/04/24     08:00
+ 5    95.83     2008/04/24     10:00
+ 6    96.88     2008/04/24     24:00"
>
> DF <- read.table(textConnection(Lines))
>
> library(chron)
> DF2 <- transform(DF,
+ chron = as.chron(paste(date, time)),
+ POSIXct = as.POSIXct(paste(date, time)))
> DF2
  LogData       date  time               chron             POSIXct
1   77.16 2008/04/24 02:00 (04/24/08 02:00:00) 2008-04-24 02:00:00
2   61.78 2008/04/24 04:00 (04/24/08 04:00:00) 2008-04-24 04:00:00
3   75.44 2008/04/24 06:00 (04/24/08 06:00:00) 2008-04-24 06:00:00
4   89.43 2008/04/24 08:00 (04/24/08 08:00:00) 2008-04-24 08:00:00
5   95.83 2008/04/24 10:00 (04/24/08 10:00:00) 2008-04-24 10:00:00
6   96.88 2008/04/24 24:00             (NA NA)                <NA>



On Tue, Feb 16, 2010 at 5:47 AM, Alex Anderson
<complicado79 at yahoo.com.au> wrote:
> Hi All,
> I am attempting to work with some data from loggers. I have read in a .csv
> exported from MS Access that already has my dates and times (in 24 clock
> format), (with StringsAsFactors=FALSE).
>
>> head(tdata)
>
>  LogData       date              time
> 1    77.16     2008/04/24     02:00
> 2    61.78     2008/04/24     04:00
> 3    75.44     2008/04/24     06:00
> 4    89.43     2008/04/24     08:00
> 5    95.83     2008/04/24     10:00
> 6    96.88     2008/04/24     24:00
>
> I wish to be able to summarise the data using the character vectors $data
> and $time (daily, monthly averages, maxima of my $LogData for example) so I
> am trying to get R to recognise the $date and $time columns as valid dates
> and times. Using...
>
>> tdata$date2 = as.Date(as.character(tdata$date))
>
> I can get a new column of valid dates, but neither:
>
>> tdata$time2= strptime(tdata$time,"%k")
>
> Error in `$<-.data.frame`(`*tmp*`, "time2", value = list(sec = c(0, 0,  :
>  replacement has 9 rows, data has 10
>
> nor trying:
>
>>  tdata$time2=chron(times=as.character(tdata$time, format= "hh:mm"))
>
> 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, 3, 4, 5, 6, 7, 8, 9, 10
> 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
>
> gives me any valid times from my time vector.  the Chron documentation
> doesn't mention 24 clocks, strptime neither, and the Rnews issue 1/4 with an
> article about time is no help... Any thoughts would be much appreciated.
> regards
>
> Alex Anderson
> James Cook University
> Townsville, Australia
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list