[R] difftime arguments

Christoph Buser buser at stat.math.ethz.ch
Fri Mar 17 08:02:55 CET 2006


Hi Fred

First of all, I have put the email back on the R-list. Please
answer by reply to all (including the list). Others might have
similar problems and others might have additional proposals,
respectively. 

You should not confound typeof() and class()
Try 
> class(g)
and you will see that your object has the class that you have
assumed. 

tyepof() has to do with the internal storage mode of an
object. See ?class and ?typeof for more details.

Finally use
> as.numeric(g)

But if you want to remove the units you should use the argument
"unit" in difftime(). Otherwise you can get strange results if
the unit is chosen automatically and removed afterwards.

Regards,

Christoph Buser

--------------------------------------------------------------
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/
--------------------------------------------------------------

--text follows this line--
Fred J. writes:
 > z <- as.POSIXct(strptime(ts, "%m/%d/%Y %I:%M:%S %p")) 
 > z then has the formate #"2006-01-12 16:00:00 Pacific Standard Time"
 > one would expect > typeof (z) would be an object of class POSIXct but notice
 > > typeof(z)
 > [1] "double"
 > 
 > now when I put
 > > g <- difftime(z[11745], z[11746])
 > > g
 > Time difference of 43.61667 mins
 > > typeof(g)
 > [1] "double"
 > > 
 > how can I just have 43.61667 with out the text stuff around it in the object "g"?
 > 
 > 
 > 
 > Christoph Buser <buser at stat.math.ethz.ch> wrote: "format" is not an argument of the function difftime(). It is an
 > argument of the function as.difftime().
 > Therefore I think that the direct way without converting the
 > dates can not be done, but I am not a specialist in time
 > question in R :-)
 > 
 > Fred J. writes:
 >  > what about 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 ...)
 >  > >
 >  > 
 >  > Christoph Buser  wrote: 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 
 >  > 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
 >  > 
 >  > 
 >  >   
 >  > ---------------------------------
 >  > Brings words and photos together (easily) with
 >  >  PhotoMail  - it's free and works with Yahoo! Mail.what about 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 ...)
 > >
 > 
 > Christoph Buser <buser at stat.math.ethz.ch> wrote: 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 
 > 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
 > 
 > 
 >  >   
 > 
 > ---------------------------------
 > Brings words and photos together (easily) with
 >  
 >  > PhotoMail  - it's free and works with Yahoo! Mail.
 > 
 > 		
 > ---------------------------------
 > Yahoo! Mail
 > Bring photos to life! New PhotoMail  makes sharing a breeze. z &lt;- as.POSIXct(strptime(ts, "%m/%d/%Y %I:%M:%S %p")) <br>z then has the formate #"2006-01-12 16:00:00 Pacific Standard Time"<br>one would expect &gt; typeof (z) would be an object of class POSIXct but notice<br>&gt; typeof(z)<br>[1] "double"<br><br>now when I put<br>&gt; g &lt;- difftime(z[11745], z[11746])<br>&gt; g<br>Time difference of 43.61667 mins<br>&gt; typeof(g)<br>[1] "double"<br>&gt; <br>how can I just have 43.61667 with out the text stuff around it in the object "g"?<br><br><br><br><b><i>Christoph Buser &lt;buser at stat.math.ethz.ch&gt;</i></b> wrote:<blockquote class="replbq" style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px;"> "format" is not an argument of the function difftime(). It is an<br>argument of the function as.difftime().<br>Therefore I think that the direct way without converting the<br>dates can not be done, but I am not a specialist in time<br>question in R :-)<br><br>Fred J. writes:<br> &gt; what about this<br> &gt; <br>
 >  &gt; &gt; difftime(c("1/12/2006 3:59:45 PM", "1/12/2006 3:59:57 PM"), format ="%m/%d/%Y %I:%M:%S %p")<br> &gt; 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") : <br> &gt;         unused argument(s) (format ...)<br> &gt; &gt;<br> &gt; <br> &gt; Christoph Buser <buser at stat.math.ethz.ch> wrote: Dear Fred<br> &gt; <br> &gt; You should change your code from<br> &gt;  &gt;   x &lt;- strptime(ts, "%m/%d/%y %I:%M:%S %p")<br> &gt; <br> &gt; to<br> &gt;  &gt;   x &lt;- strptime(ts, "%m/%d/%Y %I:%M:%S %p")<br> &gt; <br> &gt; "Y" instead of "y", since your year includes the century <br> &gt; (2006 and not 06)<br> &gt; <br> &gt; Then it should work.<br> &gt; <br> &gt; Regards,<br> &gt; <br> &gt; Christoph<br> &gt; <br> &gt; --------------------------------------------------------------<br> &gt; Christoph Buser <br> &gt; Seminar fuer Statistik, LEO C13<br> &gt; ETH (Federal Inst. Technology) 8092 Zurich  SWITZERLAND<br> &gt; phone:
 >  x-41-44-632-4673  fax: 632-1228<br> &gt; http://stat.ethz.ch/~buser/<br> &gt; --------------------------------------------------------------<br> &gt; <br> &gt; <br> &gt; <br> &gt; Fred J. writes:<br> &gt;  &gt;   Hi<br> &gt;  &gt;     I just started using RGui.exe under widnows.<br> &gt;  &gt;   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<br> &gt;  &gt;   I need to calculate the different in seconds between 2 selected periods using their row�s index<br> &gt;  &gt;    <br> &gt;  &gt;   My solution:<br> &gt;  &gt;   Read the file in a data frame and collect the columns in concern in a variable.<br> &gt;  &gt;   data1 &lt;- read.table("C:\\path\\data.txt", header=TRUE)<br> &gt;  &gt;   ts &lt;- paste(data1[[1]], data1[[3]], data1[[7]])   <br> &gt;  &gt;    <br> &gt;  &gt;   #ts now looks like this <br> &gt;  &gt;   #... "1/12/2006
 >  3:59:45 PM"  "1/12/2006 3:59:57 PM" ...<br> &gt;  &gt;    <br> &gt;  &gt;   #now convert between character representations and object of classes "POSIXct"<br> &gt;  &gt;   x &lt;- strptime(ts, "%m/%d/%y %I:%M:%S %p")<br> &gt;  &gt;    <br> &gt;  &gt;   this last code line is putting out NA, what did I do wrong?<br> &gt;  &gt;    <br> &gt;  &gt;   After this then I do<br> &gt;  &gt;   z &lt;- as.POSIXct(x)<br> &gt;  &gt;    <br> &gt;  &gt;   Is my whole approach efficient?<br> &gt;  &gt;   Then I can use difftime, because using it without the above preparations gives this<br> &gt;  &gt;    <br> &gt;  &gt;   &gt; difftime(c("1/12/2006 3:59:45 PM", "1/12/2006 3:59:57 PM"), format ="%m/%d/%y %I:%M:%S %p")<br> &gt;  &gt;   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") : <br> &gt;  &gt;           unused argument(s) (format ...)<br> &gt;  &gt;    <br> &gt;  &gt;   Thanks<br> &gt;  &gt;   <br> &gt;  &gt;
 >  __________________________________________________<br> &gt;  &gt; <br> &gt;  &gt; <br> &gt;  &gt; <br> &gt;  &gt;  [[alternative HTML version deleted]]<br> &gt;  &gt; <br> &gt;  &gt; ______________________________________________<br> &gt;  &gt; R-help at stat.math.ethz.ch mailing list<br> &gt;  &gt; https://stat.ethz.ch/mailman/listinfo/r-help<br> &gt;  &gt; PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html<br> &gt; <br> &gt; <br> &gt;   <br> &gt; ---------------------------------<br> &gt; Brings words and photos together (easily) with<br> &gt;  PhotoMail  - it's free and works with Yahoo! Mail.what about this<br><br>&gt; difftime(c("1/12/2006 3:59:45 PM", "1/12/2006 3:59:57 PM"), format ="%m/%d/%Y %I:%M:%S %p")<br>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") : <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unused argument(s) (format ...)<br>&gt;<br><br><b><i>Christoph Buser
 >  &lt;buser at stat.math.ethz.ch&gt;</i></b> wrote:</buser at stat.math.ethz.ch><blockquote class="replbq" style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px;"> Dear Fred<br><br>You should change your code from<br> &gt;   x &lt;- strptime(ts, "%m/%d/%y %I:%M:%S %p")<br><br>to<br> &gt;   x &lt;- strptime(ts, "%m/%d/%Y %I:%M:%S %p")<br><br>"Y" instead of "y", since your year includes the century <br>(2006 and not 06)<br><br>Then it should work.<br><br>Regards,<br><br>Christoph<br><br>--------------------------------------------------------------<br>Christoph Buser <buser at stat.math.ethz.ch><br>Seminar fuer Statistik, LEO C13<br>ETH (Federal Inst.<br> &gt;  Technology) 8092 Zurich  SWITZERLAND<br>phone: x-41-44-632-4673  fax: 632-1228<br>http://stat.ethz.ch/~buser/<br>--------------------------------------------------------------<br><br><br><br>Fred J. writes:<br> &gt;   Hi<br> &gt;     I just started using RGui.exe under widnows.<br> &gt;   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<br> &gt;   I need to calculate the different in seconds between 2 selected periods using their row�s index<br> &gt;    <br> &gt;   My solution:<br> &gt;   Read the file in a data frame and collect the columns in concern in a variable.<br> &gt;   data1 &lt;- read.table("C:\\path\\data.txt", header=TRUE)<br> &gt;   ts &lt;- paste(data1[[1]], data1[[3]], data1[[7]])   <br> &gt;    <br> &gt;   #ts now looks like this <br> &gt;   #... "1/12/2006 3:59:45 PM"  "1/12/2006 3:59:57 PM" ...<br> &gt;    <br> &gt;  <br> &gt;  #now convert between character representations and object of classes "POSIXct"<br> &gt;   x &lt;- strptime(ts, "%m/%d/%y %I:%M:%S %p")<br> &gt;    <br> &gt;   this last code line is putting out NA, what did I do wrong?<br> &gt;    <br> &gt;   After this then I do<br> &gt;   z &lt;- as.POSIXct(x)<br>
 >  &gt;    <br> &gt;   Is my whole approach efficient?<br> &gt;   Then I can use difftime, because using it without the above preparations gives this<br> &gt;    <br> &gt;   &gt; difftime(c("1/12/2006 3:59:45 PM", "1/12/2006 3:59:57 PM"), format ="%m/%d/%y %I:%M:%S %p")<br> &gt;   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") : <br> &gt;           unused argument(s) (format ...)<br> &gt;    <br> &gt;   Thanks<br> &gt;   <br> &gt; __________________________________________________<br> &gt; <br> &gt; <br> &gt; <br> &gt;  [[alternative HTML version deleted]]<br> &gt; <br> &gt; ______________________________________________<br> &gt;<br> &gt;  R-help at stat.math.ethz.ch mailing list<br> &gt; https://stat.ethz.ch/mailman/listinfo/r-help<br> &gt; PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html<br></buser at stat.math.ethz.ch></blockquote><br><div><br> &gt;   </div><hr size="1">Brings words and photos together
 >  (easily) with<br> <br> &gt; <a href="http://us.rd.yahoo.com/mail_us/taglines/PMall/*http://photomail.mail.yahoo.com">PhotoMail </a> - it's free and works with Yahoo! Mail.</blockquote><br><p>
 > 		<hr size=1>Yahoo! Mail<br>
 > Bring photos to life! <a href="http://pa.yahoo.com/*http://us.rd.yahoo.com/evt=39174/*http://photomail.mail.yahoo.com">New PhotoMail </a> makes sharing a breeze.




More information about the R-help mailing list