[R] Problem with strptime

jim holtman jholtman at gmail.com
Fri Oct 3 14:01:36 CEST 2008


Your example works fine for me:

> x <- read.table(textConnection("13199 2008-03-19 03:55:46
+ 13200 2008-03-19 04:00:46
+ 13201 2008-03-19 04:05:46
+ 13202 2008-03-19 04:10:46
+ 13203 2008-03-19 04:15:46"), as.is=TRUE)
> closeAllConnections()
> x$time <- as.POSIXct(strptime(paste(x$V2, x$V3), "%Y-%m-%d %H:%M:%S"))
> x
     V1         V2       V3                time
1 13199 2008-03-19 03:55:46 2008-03-19 03:55:46
2 13200 2008-03-19 04:00:46 2008-03-19 04:00:46
3 13201 2008-03-19 04:05:46 2008-03-19 04:05:46
4 13202 2008-03-19 04:10:46 2008-03-19 04:10:46
5 13203 2008-03-19 04:15:46 2008-03-19 04:15:46
> difftime(tail(x$time, -1), head(x$time, -1), units='sec')
Time differences in secs
[1] 300 300 300 300
attr(,"tzone")
[1] "GMT"
>
>


On Fri, Oct 3, 2008 at 3:41 AM, Solene Goy <olsen-olsen at hotmail.fr> wrote:
>
> Hello,
>
> I have a column with dates in the form %Y-%m-%d %H:%M:%S; I want to substract the line i+1 to the line i and get a numeric result (in seconds for instance).
> This is what I did (I take the data from a database):
>
> res<- dbSendQuery (con, "SELECT Date_Heure FROM data.meteo ")Time<-fetch(res, n=-1)TimeDifferencetime<-as.numeric(diff(strptime(Time, format='%Y-%m-%d %H:%M:%S')) )
> Differencetime
>
> The colum Time is ok, I get something like (~20 000 values):
>
> ...
> 13199 2008-03-19 03:55:4613200 2008-03-19 04:00:4613201 2008-03-19 04:05:4613202 2008-03-19 04:10:4613203 2008-03-19 04:15:46
> ...
>
> But for Differencetime I get:
> numeric(0)
>
> Well... I don't see the problem in the code lines I wrote... any ideas??
>
> Thanks in advance for your help!
>
> Solene
> _________________________________________________________________
>
> [[elided Hotmail spam]]
>
>        [[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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list