Re: [R] Time data
On 12/20/05, Marc Bernard <[EMAIL PROTECTED]> wrote: > Dear All, I wonder how to compute the age from the date of birth and the date > of examination. Suppose that have the following data: > > df <- as.data.frame(rbind(c(1,"10/08/1950","15/03/1998"), > c(1,"10/08/1950","20/07/1998"), c(1,"10/08/1950","23/10/1998"))) > > names(df) <- c("ID", "date_birth", "date_exam") > > where: > date_birth: is the date of birth > date_exam: date of examination > > I used the following program to compute the value of the age : > First ensure that df stores its dates using "Date" class in the first place. If your data is stored in the correct representation then everything becomes easier subsequently: fmt <- "%d/%m/%Y" df[,2] <- as.Date(df[,2], fmt) df[,3] <- as.Date(df[,3], fmt) # converting them to numeric gives the number of days since # the Epoch and one can just subtact those: (as.numeric(df$date_exam) - as..numeric(df$date_birth)) / 365 R News 4/1 Help Desk article has more info on dates. > difftime(strptime(as.character(df$date_exam), '%d/%m/%Y'), > strptime(as.character(df$date_birth), '%d/%m/%Y'))/365.25 > > which gives me as an output: > > > Time differences of 47.59491, 47.94251, 48.20260 days > > theses values are actually the 3 ages (but > > My questions are: > > 1- Why in the output it says "days" instead of "years") > > 2- How can I obtain the output as a numeric vector, without the statement > "Time difference of ". This is in order to use it in my calculations. > > 3- Is there a way quicker and less redondant to compute the age form the > date_birth and date_exam? > > Thanks a lot, > > Bernard, > > > > - > >[[alternative HTML version deleted]] > > __ > R-help@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 > __ R-help@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
Re: [R] Time data
Hi On 20 Dec 2005 at 11:12, Marc Bernard wrote: Date sent: Tue, 20 Dec 2005 11:12:28 +0100 (CET) From: Marc Bernard <[EMAIL PROTECTED]> To: r-help@stat.math.ethz.ch Subject: [R] Time data > Dear All, I wonder how to compute the age from the date of birth and > the date of examination. Suppose that have the following data: > > df <- as.data.frame(rbind(c(1,"10/08/1950","15/03/1998"), > c(1,"10/08/1950","20/07/1998"), c(1,"10/08/1950","23/10/1998"))) > > names(df) <- c("ID", "date_birth", "date_exam") > > where: > date_birth: is the date of birth > date_exam: date of examination > > I used the following program to compute the value of the age : > > difftime(strptime(as.character(df$date_exam), '%d/%m/%Y'), > strptime(as.character(df$date_birth), '%d/%m/%Y'))/365.25 > > which gives me as an output: > > > Time differences of 47.59491, 47.94251, 48.20260 days > > theses values are actually the 3 ages (but > > My questions are: > > 1- Why in the output it says "days" instead of "years") this is in attributes of an output and is not changed by calculations > > 2- How can I obtain the output as a numeric vector, without the > statement "Time difference of ". This is in order to use it in > my calculations. as.numeric(as.Date(df$date_exam)-as.Date(df$date_birth))/365 However you can use it in calculations without problem as I suppose that "Time difference of" results from print method of a difftime object. > > 3- Is there a way quicker and less redondant to compute the age > form the date_birth and date_exam? as.Date(df$date_exam)-as.Date(df$date_birth) HTH Petr > > Thanks a lot, > > Bernard, > > > > - > > [[alternative HTML version deleted]] > > __ > R-help@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 Petr Pikal [EMAIL PROTECTED] __ R-help@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
[R] Time data
Dear All, I wonder how to compute the age from the date of birth and the date of examination. Suppose that have the following data: df <- as.data.frame(rbind(c(1,"10/08/1950","15/03/1998"), c(1,"10/08/1950","20/07/1998"), c(1,"10/08/1950","23/10/1998"))) names(df) <- c("ID", "date_birth", "date_exam") where: date_birth: is the date of birth date_exam: date of examination I used the following program to compute the value of the age : difftime(strptime(as.character(df$date_exam), '%d/%m/%Y'), strptime(as.character(df$date_birth), '%d/%m/%Y'))/365.25 which gives me as an output: > Time differences of 47.59491, 47.94251, 48.20260 days theses values are actually the 3 ages (but My questions are: 1- Why in the output it says "days" instead of "years") 2- How can I obtain the output as a numeric vector, without the statement "Time difference of ". This is in order to use it in my calculations. 3- Is there a way quicker and less redondant to compute the age form the date_birth and date_exam? Thanks a lot, Bernard, - [[alternative HTML version deleted]] __ R-help@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
Re: [R] time data
__ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] time data
I have a data set with date, time and rainfall (sample below) my questions are (a) is there a command like as.date for time? and (b) Can R smooth data 6 hourly with such unevenly spaced data? Thanks Carla 09/29/02 19:33 0 09/29/02 19:34 0 09/29/02 19:35 0 09/29/02 19:36 0.1 09/29/02 19:37 0 09/29/02 19:38 0 09/29/02 19:39 0 09/29/02 19:40 0.1 09/29/02 19:41 0 09/29/02 19:42 0 09/30/02 00:00 0 09/30/02 01:50 0.1 09/30/02 02:20 0.1 09/30/02 02:40 0.2 09/30/02 02:50 0.4 09/30/02 05:00 0.7 __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html