[R-sig-eco] difftime in R

Kristen Gorman kgorman at sfu.ca
Fri Dec 7 06:43:39 CET 2012


Dear all, 
I am trying to calculate the difference between 2 times, each expressed as a Date and associated Time, see the below test example:

Event            T1            T2
1        12-06-01 1:00 12-06-02 0:00
2        12-06-01 1:00 12-06-02 0:00
3        12-06-01 1:00 12-06-02 0:00
4        12-06-01 1:00 12-06-02 0:00
5        12-06-01 1:00 12-06-02 0:00

when I run the difftime fxn I get the appropriate hours:

T1<- as.POSIXlt(DataSetb$T1)
T2<- as.POSIXlt(DataSetb$T2)

T3<- difftime(T2,T1)
T3

Time differences in hours
 [1] 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23

But then when I write this to the dataframe, it looks like:

DataSetb.2<- cbind(DataSetb,T3)

write.table(DataSetb.2, file="DataSetb.2.csv", col.names=NA, sep=",")


Event            T1            T2       T3
1        12-06-01 1:00 12-06-02 0:00 23 hours
2        12-06-01 1:00 12-06-02 0:00 23 hours
3        12-06-01 1:00 12-06-02 0:00 23 hours
4        12-06-01 1:00 12-06-02 0:00 23 hours
5        12-06-01 1:00 12-06-02 0:00 23 hours

I would like the T3 column to just have the values (23) and not (23 hours). Does anyone know how to remove the hours from being written to the dataframe? Maybe there is a better way to do this than using difftime?

Thanks in advance for your help.

Kristen



More information about the R-sig-ecology mailing list