[R] calculate time from dates

arun smartpink111 at yahoo.com
Thu Jul 11 12:51:19 CEST 2013


Hi,
May be this helps:


dat1<- read.table(text="
ID date
1 4/12/2008
1 4/13/2008
1 5/11/2008
2 3/21/2009
2 4/22/2009
2 8/05/2009
",sep="",header=TRUE,stringsAsFactors=FALSE)
library(mondate)
M1<- mondate(dat1[,2])
M2<- mondate("01/01/2008")
dat1$month<-as.numeric(abs(floor(MonthsBetween(M1,M2))))
 dat1
#  ID      date month
#1  1 4/12/2008     4
#2  1 4/13/2008     4
#3  1 5/11/2008     5
#4  2 3/21/2009    15
#5  2 4/22/2009    16
#6  2 8/05/2009    20
A.K.



----- Original Message -----
From: Gallon Li <gallon.li at gmail.com>
To: r-help <r-help at stat.math.ethz.ch>
Cc: 
Sent: Thursday, July 11, 2013 5:56 AM
Subject: [R] calculate time from dates

My data are from 2008 to 2010, with repeated measures for same subjects. I
wish to compute number of months since january 2008.

The data are like the following:

ID date
1 4/12/2008
1 4/13/2008
1 5/11/2008
2 3/21/2009
2 4/22/2009
2 8/05/2009
...

the date column are in the format "%m/%d/%y". i wish to obtain

ID month
1 4
1 4
1 5
2 15
2 16
2 20
...

also, for the same ID with two identical month, I only want to keep the
last one. can any expert help with this question?

    [[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.




More information about the R-help mailing list