First thing to notice in your second example is that you have "factor"
for
what you think are strings. You should try:
u2<-as.difftime(c(as.character(a),as.character(b)), format ="%Y-%m-%d
%H:%M:%S", units="mins")
and see if this gives you what you are expecting. If you are reading this
in with 'read.table', you might want to specify those columns as
characters,
or use 'as.is=TRUE' to prevent conversion to factors. If you are not
using
factors, then you probably want the character strings.
On Mon, Apr 5, 2010 at 9:19 AM, steve012 <stephenadrake@gmail.com> wrote:
>
> I'm new to R and have the following problem with difftime:
>
> if I directly assign date/time strings in difftime I get the expected
> result:
>
> > a<-"2010-03-23 10:52:00"
> > a
> [1] "2010-03-23 10:52:00"
> > b<-"2010-03-23 11:53:00"
> > u2<-as.difftime(c(a,b), format ="%Y-%m-%d %H:%M:%S",
units="mins")
> > u2
> Time differences in mins
> [1] -18008 -17947
> attr(,"tzone")
>
>
> However, if I first assign the values of "a" and "b"
from a data frame
> within a loop I get a different result:
>
> a<-u[i]
> > a
> [1] 2010-03-23 10:52:00
> 6838 Levels: 2010-03-18 16:54:00 2010-03-18 16:55:00 ... 2010-03-23
> 11:53:00
> b<-u[i+1]
> > b
> [1] 2010-03-23 11:53:00
> 6838 Levels: 2010-03-18 16:54:00 2010-03-18 16:55:00 ... 2010-03-23
> 11:53:00
> u2<-as.difftime(c(a,b), format ="%Y-%m-%d %H:%M:%S",
units="mins")
> > u2
> Time differences in mins
> [1] 6837 6838
>
>
> So, how do use difftime in the context of a loop? Thanks.
> --
> View this message in context:
> http://n4.nabble.com/using-difftime-tp1751607p1751607.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help@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<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?
[[alternative HTML version deleted]]