Alex Anderson
2010-Feb-16 10:47 UTC
[R] converting character vector "hh:mm" to chron or strptime 24 clock time vectors
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
stephen sefick
2010-Feb-16 13:23 UTC
[R] converting character vector "hh:mm" to chron or strptime 24 clock time vectors
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
Jim Lemon
2010-Feb-17 03:28 UTC
[R] converting character vector "hh:mm" to chron or strptime 24 clock time vectors
On 02/16/2010 09:47 PM, Alex Anderson wrote:> ...This is the problem> 6 96.88 2008/04/24 24:00 > > Error in `$<-.data.frame`(`*tmp*`, "time2", value = list(sec = c(0, 0, : > replacement has 9 rows, data has 10Hi Alex, You have a problem with an invalid time. The line should read: 6 96.88 2008/05/24 00:00 However, there is something else going on here that puzzles me. When I tried this with your sample data: tdata$datetime<-strptime(paste(tdata$date,tdata$time,sep="-"),"%Y/%m/%d-%H:%M") Error in `$<-.data.frame`(`*tmp*`, "datetime", value = list(sec = c(0, : replacement has 9 rows, data has 6 Yet: datetime<-strptime(paste(tdata$date,tdata$time,sep="-"), "%Y/%m/%d-%H:%M") datetime [1] "2008-04-24 02:00:00" "2008-04-24 04:00:00" "2008-04-24 06:00:00" [4] "2008-04-24 08:00:00" "2008-04-24 10:00:00" "2008-05-24 00:00:00" length(datetime) [1] 9 This is the first time I have encountered a discrepancy between "length" and the printed extent of an object, and I can't work out what is going on. Jim
Gabor Grothendieck
2010-Feb-17 05:10 UTC
[R] converting character vector "hh:mm" to chron or strptime 24 clock time vectors
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)))> DF2LogData 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. >