[R-sig-eco] difftime in R

Daniel Pritchard d.pritchard at qub.ac.uk
Fri Dec 7 08:10:50 CET 2012


Hi Kristen,

Thomas is right on both counts.  Though, as a further word of caution, it is not guaranteed that difftime() will return data in 'hours'.  This is probably the reason why it returns a special class whose format() method tries so very hard to include the units.  

I'd suggest either specifying your intended units in the call to difftime() and/or storing the unit information alongside T3.

~~~~~~~
T3<- difftime(T2,T1, units='hours')
## and/or
DataSetb.2<- cbind(DataSetb,'T3'=as.numeric(T3), 'T3_units' = rep(units(T3), length(T3)))
~~~~~~~

Daniel Pritchard

On 7 Dec 2012, at 16:52, Thomas Petzoldt <Thomas.Petzoldt at tu-dresden.de> wrote:

> try:
> 
> as.numeric(T3)
> 
> Note that this is a typical "r-help" question.
> 
> Thomas Petzoldt
> 
> ------------------------------------------
> DataSetb <- data.frame(
>   T1 = rep("2012-06-01 1:00", 5),
>   T2 = rep("2012-06-02 0:00", 5)
> )
> 
> 
> T1<- as.POSIXlt(DataSetb$T1)
> T2<- as.POSIXlt(DataSetb$T2)
> 
> T3<- as.numeric(difftime(T2, T1))
> T3
> 
> 
> DataSetb.2<- cbind(DataSetb,T3)
> write.table(DataSetb.2, file="DataSetb.2.csv", col.names=NA, sep=",")
> 
> _______________________________________________
> R-sig-ecology mailing list
> R-sig-ecology at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-ecology


Daniel Pritchard
Research Fellow
Marine Research Group
School of Planning, Architecture and Civil Engineering (SPACE)
Queens University Belfast



More information about the R-sig-ecology mailing list