[R] difftime arguments

Christoph Buser buser at stat.math.ethz.ch
Wed Mar 15 10:04:17 CET 2006


Dear Fred

You should change your code from
 >   x <- strptime(ts, "%m/%d/%y %I:%M:%S %p")

to
 >   x <- strptime(ts, "%m/%d/%Y %I:%M:%S %p")

"Y" instead of "y", since your year includes the century 
(2006 and not 06)

Then it should work.

Regards,

Christoph

--------------------------------------------------------------
Christoph Buser <buser at stat.math.ethz.ch>
Seminar fuer Statistik, LEO C13
ETH (Federal Inst. Technology)	8092 Zurich	 SWITZERLAND
phone: x-41-44-632-4673		fax: 632-1228
http://stat.ethz.ch/~buser/
--------------------------------------------------------------



Fred J. writes:
 >   Hi
 >     I just started using RGui.exe under widnows.
 >   I have a text file containing date arranged in columns and rows, each column has the same format, each row with different formats. 3 of the columns are something like this 1/12/2006 3:59:45 PM
 >   I need to calculate the different in seconds between 2 selected periods using their row’s index
 >    
 >   My solution:
 >   Read the file in a data frame and collect the columns in concern in a variable.
 >   data1 <- read.table("C:\\path\\data.txt", header=TRUE)
 >   ts <- paste(data1[[1]], data1[[3]], data1[[7]])   
 >    
 >   #ts now looks like this 
 >   #... "1/12/2006 3:59:45 PM"  "1/12/2006 3:59:57 PM" ...
 >    
 >   #now convert between character representations and object of classes "POSIXct"
 >   x <- strptime(ts, "%m/%d/%y %I:%M:%S %p")
 >    
 >   this last code line is putting out NA, what did I do wrong?
 >    
 >   After this then I do
 >   z <- as.POSIXct(x)
 >    
 >   Is my whole approach efficient?
 >   Then I can use difftime, because using it without the above preparations gives this
 >    
 >   > difftime(c("1/12/2006 3:59:45 PM", "1/12/2006 3:59:57 PM"), format ="%m/%d/%y %I:%M:%S %p")
 >   Error in difftime(c("1/12/2006 3:59:45 PM", "1/12/2006 3:59:57 PM"), format = "%m/%d/%y %I:%M:%S %p") : 
 >           unused argument(s) (format ...)
 >    
 >   Thanks
 >   
 > __________________________________________________
 > 
 > 
 > 
 > 	[[alternative HTML version deleted]]
 > 
 > ______________________________________________
 > R-help at stat.math.ethz.ch mailing list
 > https://stat.ethz.ch/mailman/listinfo/r-help
 > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html




More information about the R-help mailing list