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

stephen sefick ssefick at gmail.com
Tue Feb 16 14:23:19 CET 2010


library(chron)
#untested
as.chron(paste(tdata[,"date"], tdata[,"time"]), "%Y/%m/%d %H:%M")

On Tue, Feb 16, 2010 at 4: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.
>



-- 
Stephen Sefick

Let's not spend our time and resources thinking about things that are
so little or so large that all they really do for us is puff us up and
make us feel like gods.  We are mammals, and have not exhausted the
annoying little problems of being mammals.

								-K. Mullis



More information about the R-help mailing list