[R] Getting the numeric value of difftime

Barry Rowlingson B.Rowlingson at lancaster.ac.uk
Fri Jan 13 14:16:21 CET 2006


Christian Neumann wrote:
> Hi folks,
> 
> I have a small, maybe newbie, question concerning date operations.
> The follwing snippet
> 
> date1 = "2005-11-20";
> date2 = "2005-11-17";
> difftime(date1, date2)
> 
> results in "Time difference of 3 days". How can I extract just the 
> numerical value 3?
> As a workaround I apply mean() on the result which gives me just a 
> numerical value.

  If you want to get one thing _as_ another the R-ish solution is nearly 
always to do as.another(oneThing).

  You want it numeric? You got it!

 > date1 = "2005-11-20";
 > date2 = "2005-11-17";
 > difftime(date1, date2)
Time difference of 3 days
 > as.numeric(difftime(date1, date2))
[1] 3

Barry




More information about the R-help mailing list