Hello, Given a vector of characters, or factors, denoting the date in the following way: 28/03/2000, is there a method of 1) Computing the earliest of these dates; 2) Using this as a base, then converting all the other dates into merely the number of days after this minimum date Many thanks Richard Hillary
Use POSIX. To convert: my.dates <- strptime(your.characters, format='%d/%m/%Y') once you have that, you can use 'min' to find the minimum. 'difftime' will give you the differences. Jim __________________________________________________________ James Holtman "What is the problem you are trying to solve?" Executive Technical Consultant -- Convergys Labs james.holtman at convergys.com +1 (513) 723-2929 Richard Hillary <r.hillary at imperial.a To: r-help at stat.math.ethz.ch c.uk> cc: Sent by: Subject: [R] Manipulating dates r-help-bounces at stat.m ath.ethz.ch 06/14/2005 10:07 Please respond to r.hillary Hello, Given a vector of characters, or factors, denoting the date in the following way: 28/03/2000, is there a method of 1) Computing the earliest of these dates; 2) Using this as a base, then converting all the other dates into merely the number of days after this minimum date Many thanks Richard Hillary ______________________________________________ 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
try this: x <- c("28/03/2000", "27/08/2001", "29/05/2002", "15/12/2003") y <- as.Date(x, "%d/%m/%Y") y - min(y) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/16/336899 Fax: +32/16/337015 Web: http://www.med.kuleuven.ac.be/biostat/ http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Richard Hillary" <r.hillary at imperial.ac.uk> To: <r-help at stat.math.ethz.ch> Sent: Tuesday, June 14, 2005 4:07 PM Subject: [R] Manipulating dates> Hello, > Given a vector of characters, or factors, denoting the date > in > the following way: 28/03/2000, is there a method of > 1) Computing the earliest of these dates; > 2) Using this as a base, then converting all the other dates into > merely > the number of days after this minimum date > Many thanks > Richard Hillary > > ______________________________________________ > 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 >
On 6/14/05, Richard Hillary <r.hillary at imperial.ac.uk> wrote:> Hello, > Given a vector of characters, or factors, denoting the date in > the following way: 28/03/2000, is there a method of > 1) Computing the earliest of these dates; > 2) Using this as a base, then converting all the other dates into merely > the number of days after this minimum dateConvert dates to chron (whose default date format is the one needed here); convert that to numeric (which will be the number of days since some origin) and then do the indicated subtraction: library(chron) x <- as.character(x) # can omit if already character x.num <- as.numeric(chron(x)) x - x.num - min(x.num) Alternately convert it to Date and use julian (or convert x.Date to numeric and use subtraction, as with chron): x <- as.character(x) # can omit in latest R 2.1.0 patched x.Date <- as.Date(x, "%m/%d/%Y") julian(x.Date, min(x.Date)) The as.Date.factor method in the latest R 2.1.0 patched supports the format string (second argument of as.Date) for factors but older versions of R did not. If your x is a factor but you do not want to upgrade right now start off with the statement: x <- as.character(x) More info on dates is in R News 4/1.