[R] Day or Month difference between dates???

Eik Vettorazzi E.Vettorazzi at uke.de
Fri Jun 1 17:27:55 CEST 2012


Hi Tammy,
I suspect that the first 4 digits compose the year, so with

Year_Month<-c(2010*100+9:11,2011*100+1:2)
#check composition
Year_Month

ym<-(Year_Month%/%100*12+Year_Month%%100)
ym-ym[1]

should give the difference in months.

Differences in Days are a bit trickier, since not all months have an
equal number of days. Something like

xd<-as.Date(paste(Year_Month%/%100,Year_Month%%100,"01",sep="-"))
xd-xd[1]

deals with that.

Cheers


Am 01.06.2012 13:31, schrieb Tammy Ma:
> 
> HI, R-Users:
> 
> I got a questions. have been struggling so long time....
> 
> I have this data:
> 
>> m1$Year_Month
>   201009 201010 201011 201101 201102
>> min(m1$Year_Month)
>   201009
> 
> I want to calculate the following two answers, how do I program it?
> 
>> difference in Month?????
>  [1] 0 1 2 4 5 
> 
>> difference in Days?????
>  0  31  61 ....
> 
> Thank you in advance!!!
> 
> Tammy
>  		 	   		  
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.


-- 
Eik Vettorazzi

Department of Medical Biometry and Epidemiology
University Medical Center Hamburg-Eppendorf

Martinistr. 52
20246 Hamburg

T ++49/40/7410-58243
F ++49/40/7410-57790

--
Pflichtangaben gemäß Gesetz über elektronische Handelsregister und Genossenschaftsregister sowie das Unternehmensregister (EHUG):

Universitätsklinikum Hamburg-Eppendorf; Körperschaft des öffentlichen Rechts; Gerichtsstand: Hamburg

Vorstandsmitglieder: Prof. Dr. Guido Sauter (Vertreter des Vorsitzenden), Dr. Alexander Kirstein, Joachim Prölß, Prof. Dr. Dr. Uwe Koch-Gromus 



More information about the R-help mailing list