|
|
|
DispersionMap
|
Ive got a big column of dates (also some fields dont have a date so they have NA instead),
that i have converted into date format as so... dates<-as.character(data[,"date_commissioned"]); # converted dates to characters dates[1:10] [1] "19910101" "19860101" "19910101" "19860101" "19910101" "19910101" "19910101" "19910101" "19910101" "19910101" dateObs <- as.Date(dates,format="%Y%m%d") dateObs[1:10] [1] "1991-01-01" "1986-01-01" "1991-01-01" "1986-01-01" "1991-01-01" "1991-01-01" "1991-01-01" "1991-01-01" "1991-01-01" "1991-01-01" Now i need to turn the dates into AGE, how do i do it? Im not worried about fractions of years, whole years would do. |
|
jholtman
|
What is the frame of reference to determine the age? Check out 'difftime'.
On Sun, Nov 8, 2009 at 1:50 PM, frenchcr <[hidden email]> wrote: > > Ive got a big column of dates (also some fields dont have a date so they have > NA instead), > that i have converted into date format as so... > > > dates<-as.character(data[,"date_commissioned"]); # converted dates to > characters > dates[1:10] > [1] "19910101" "19860101" "19910101" "19860101" "19910101" "19910101" > "19910101" "19910101" "19910101" "19910101" > > dateObs <- as.Date(dates,format="%Y%m%d") > dateObs[1:10] > [1] "1991-01-01" "1986-01-01" "1991-01-01" "1986-01-01" "1991-01-01" > "1991-01-01" "1991-01-01" "1991-01-01" "1991-01-01" "1991-01-01" > > > > Now i need to turn the dates into AGE, how do i do it? Im not worried about > fractions of years, whole years would do. > > > -- > View this message in context: http://old.nabble.com/Turn-dates-into-age-tp26256656p26256656.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > [hidden email] 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. > -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? ______________________________________________ [hidden email] 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. |
||||||||||||||||
|
DispersionMap
|
it sure does thank you! > will this work for you > > x <- c('19910101', '19950302', '20010502') > today <- Sys.Date() > x.date <- as.Date(x, format="%Y%m%d") > round(as.vector(difftime(today , x.date, units='day') / 365.25)) [1] 19 15 9 > On Sun, Nov 8, 2009 at 2:44 PM, <frenchcr@btinternet.com> wrote: > Hi Jim, > > Thanks for the quick reply...not sure what you mean by frame of reference(only been using R for 4 days)...to clarify, i need to turn my dates from 1999-10-01 into 1999 then i subtract 2009 -1999 to get an age of 10. The column im working on has 312,000 rows and some have NA in them as we have no dates for that item. > > To recap, the column is just a bunch of dates with some field empty, i want to change the column from "date of commision" to "age of asset" > > Cheers > Chris.
|
||||||||||||||||
|
DispersionMap
|
why do you use 365.25? dates<-as.character(data[,"date_commissioned"]); # convert dates to characters #dates[1:10] #[1] "19910101" "19860101" "19910101" "19860101" "19910101" "19910101" "19910101" "19910101" "19910101" "19910101" dateObs <- as.Date(dates,format="%Y%m%d") #dateObs[1:10] #[1] "1991-01-01" "1986-01-01" "1991-01-01" "1986-01-01" "1991-01-01" "1991-01-01" "1991-01-01" "1991-01-01" "1991-01-01" "1991-01-01" today <- Sys.Date() x.date <- as.Date(dateObs, format="%Y%m%d") AGE <- round(as.vector(difftime(today , x.date, units='day') / 365.25))
|
||||||||||||||||
|
Marc Schwartz-3
|
In reply to this post
by jholtman
As Jim has noted, if the dates you have below are an 'end date', you
need to define the time0 or start date for each to calculate the intervals. On the other hand, are the dates you have below the start dates and you need to calculate the time to today? In the latter case, see ?Sys.Date to get the current system date from your computer. Generically speaking you can use the following: (EndDate - StartDate) / 365.25 where EndDate and StartDate are of class Date. This will give you the time interval in years plus any fraction. You can then use round() which will give you a whole year with typical rounding up or down to the nearest whole integer. You can use floor(), which will give you the nearest whole integer less than the result or basically a round down result. Keep in mind that the above calculation does not honor a calendar year, but is an approximation. If you want to calculate age in years as we typically think of it using the calendar, you can use the following, where DOB is the Date of Birth (Start Date) and Date2 is the End Date: # Calculate Age in Years # DOB: Class Date # Date2: Class Date Calc_Age <- function(DOB, Date2) { if (length(DOB) != length(Date2)) stop("length(DOB) != length(Date2)") if (!inherits(DOB, "Date") | !inherits(Date2, "Date")) stop("Both DOB and Date2 must be Date class objects") start <- as.POSIXlt(DOB) end <- as.POSIXlt(Date2) Age <- end$year - start$year ifelse((end$mon < start$mon) | ((end$mon == start$mon) & (end$mday < start$mday)), Age - 1, Age) } HTH, Marc Schwartz On Nov 8, 2009, at 1:30 PM, jim holtman wrote: > What is the frame of reference to determine the age? Check out > 'difftime'. > > On Sun, Nov 8, 2009 at 1:50 PM, frenchcr <[hidden email]> > wrote: >> >> Ive got a big column of dates (also some fields dont have a date so >> they have >> NA instead), >> that i have converted into date format as so... >> >> >> dates<-as.character(data[,"date_commissioned"]); # converted dates to >> characters >> dates[1:10] >> [1] "19910101" "19860101" "19910101" "19860101" "19910101" "19910101" >> "19910101" "19910101" "19910101" "19910101" >> >> dateObs <- as.Date(dates,format="%Y%m%d") >> dateObs[1:10] >> [1] "1991-01-01" "1986-01-01" "1991-01-01" "1986-01-01" "1991-01-01" >> "1991-01-01" "1991-01-01" "1991-01-01" "1991-01-01" "1991-01-01" >> >> >> >> Now i need to turn the dates into AGE, how do i do it? Im not >> worried about >> fractions of years, whole years would do. ______________________________________________ [hidden email] 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. |
||||||||||||||||
|
David Winsemius
|
In reply to this post
by DispersionMap
On Nov 8, 2009, at 3:11 PM, frenchcr wrote: > > > why do you use 365.25? > As opposed to what? -- David > > dates<-as.character(data[,"date_commissioned"]); # convert dates to > characters > #dates[1:10] > #[1] "19910101" "19860101" "19910101" "19860101" "19910101" "19910101" > "19910101" "19910101" "19910101" "19910101" > > dateObs <- as.Date(dates,format="%Y%m%d") > #dateObs[1:10] > #[1] "1991-01-01" "1986-01-01" "1991-01-01" "1986-01-01" "1991-01-01" > "1991-01-01" "1991-01-01" "1991-01-01" "1991-01-01" "1991-01-01" > > today <- Sys.Date() > x.date <- as.Date(dateObs, format="%Y%m%d") > > AGE <- round(as.vector(difftime(today , x.date, units='day') / > 365.25)) > > > > > > frenchcr wrote: >> >> >> it sure does thank you! >> >> >>> will this work for you >>> >>> x <- c('19910101', '19950302', '20010502') >>> today <- Sys.Date() >>> x.date <- as.Date(x, format="%Y%m%d") >>> round(as.vector(difftime(today , x.date, units='day') / 365.25)) >> [1] 19 15 9 >>> >> >> >> On Sun, Nov 8, 2009 at 2:44 PM, <[hidden email]> wrote: >>> Hi Jim, >>> >>> Thanks for the quick reply...not sure what you mean by frame of >>> reference(only been using R for 4 days)...to clarify, i need to >>> turn my >>> dates from 1999-10-01 into 1999 then i subtract 2009 -1999 to get >>> an age >>> of 10. The column im working on has 312,000 rows and some have NA >>> in them >>> as we have no dates for that item. >>> >>> To recap, the column is just a bunch of dates with some field >>> empty, i >>> want to change the column from "date of commision" to "age of asset" >>> >>> Cheers >>> Chris. >> >> >> >> >> jholtman wrote: >>> >>> What is the frame of reference to determine the age? Check out >>> 'difftime'. >>> >>> On Sun, Nov 8, 2009 at 1:50 PM, frenchcr <[hidden email]> >>> wrote: >>>> >>>> Ive got a big column of dates (also some fields dont have a date >>>> so they >>>> have >>>> NA instead), >>>> that i have converted into date format as so... >>>> >>>> >>>> dates<-as.character(data[,"date_commissioned"]); # converted >>>> dates to >>>> characters >>>> dates[1:10] >>>> [1] "19910101" "19860101" "19910101" "19860101" "19910101" >>>> "19910101" >>>> "19910101" "19910101" "19910101" "19910101" >>>> >>>> dateObs <- as.Date(dates,format="%Y%m%d") >>>> dateObs[1:10] >>>> [1] "1991-01-01" "1986-01-01" "1991-01-01" "1986-01-01" >>>> "1991-01-01" >>>> "1991-01-01" "1991-01-01" "1991-01-01" "1991-01-01" "1991-01-01" >>>> >>>> >>>> >>>> Now i need to turn the dates into AGE, how do i do it? Im not >>>> worried >>>> about >>>> fractions of years, whole years would do. >>>> >>>> >>>> -- >>>> View this message in context: >>>> http://old.nabble.com/Turn-dates-into-age-tp26256656p26256656.html >>>> Sent from the R help mailing list archive at Nabble.com. >>>> >>>> ______________________________________________ >>>> [hidden email] 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. >>>> >>> >>> >>> >>> -- >>> Jim Holtman >>> Cincinnati, OH >>> +1 513 646 9390 >>> >>> What is the problem that you are trying to solve? >>> >>> ______________________________________________ >>> [hidden email] 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. >>> >>> >> >> > > -- > View this message in context: http://old.nabble.com/Turn-dates-into-age-tp26256656p26257435.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > [hidden email] 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. David Winsemius, MD Heritage Laboratories West Hartford, CT ______________________________________________ [hidden email] 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. |
||||||||||||||||
|
Jim Burke
|
To clarify.
Lets turn a date into an age. Given 05/29/1971 in mm/dd/yyyy format. What is the year difference between then and today? This would be the "age" requested that starts 05/29/1971 as one. Thanks, Jim David Winsemius wrote: > > On Nov 8, 2009, at 3:11 PM, frenchcr wrote: > >> >> >> why do you use 365.25? >> > > As opposed to what? > > -- David >> >> dates<-as.character(data[,"date_commissioned"]); # convert dates to >> characters >> #dates[1:10] >> #[1] "19910101" "19860101" "19910101" "19860101" "19910101" "19910101" >> "19910101" "19910101" "19910101" "19910101" >> >> dateObs <- as.Date(dates,format="%Y%m%d") >> #dateObs[1:10] >> #[1] "1991-01-01" "1986-01-01" "1991-01-01" "1986-01-01" "1991-01-01" >> "1991-01-01" "1991-01-01" "1991-01-01" "1991-01-01" "1991-01-01" >> >> today <- Sys.Date() >> x.date <- as.Date(dateObs, format="%Y%m%d") >> >> AGE <- round(as.vector(difftime(today , x.date, units='day') / 365.25)) >> >> >> >> >> >> frenchcr wrote: >>> >>> >>> it sure does thank you! >>> >>> >>>> will this work for you >>>> >>>> x <- c('19910101', '19950302', '20010502') >>>> today <- Sys.Date() >>>> x.date <- as.Date(x, format="%Y%m%d") >>>> round(as.vector(difftime(today , x.date, units='day') / 365.25)) >>> [1] 19 15 9 >>>> >>> >>> >>> On Sun, Nov 8, 2009 at 2:44 PM, <[hidden email]> wrote: >>>> Hi Jim, >>>> >>>> Thanks for the quick reply...not sure what you mean by frame of >>>> reference(only been using R for 4 days)...to clarify, i need to >>>> turn my >>>> dates from 1999-10-01 into 1999 then i subtract 2009 -1999 to get >>>> an age >>>> of 10. The column im working on has 312,000 rows and some have NA >>>> in them >>>> as we have no dates for that item. >>>> >>>> To recap, the column is just a bunch of dates with some field empty, i >>>> want to change the column from "date of commision" to "age of asset" >>>> >>>> Cheers >>>> Chris. >>> >>> >>> >>> >>> jholtman wrote: >>>> >>>> What is the frame of reference to determine the age? Check out >>>> 'difftime'. >>>> >>>> On Sun, Nov 8, 2009 at 1:50 PM, frenchcr <[hidden email]> >>>> wrote: >>>>> >>>>> Ive got a big column of dates (also some fields dont have a date >>>>> so they >>>>> have >>>>> NA instead), >>>>> that i have converted into date format as so... >>>>> >>>>> >>>>> dates<-as.character(data[,"date_commissioned"]); # converted dates to >>>>> characters >>>>> dates[1:10] >>>>> [1] "19910101" "19860101" "19910101" "19860101" "19910101" "19910101" >>>>> "19910101" "19910101" "19910101" "19910101" >>>>> >>>>> dateObs <- as.Date(dates,format="%Y%m%d") >>>>> dateObs[1:10] >>>>> [1] "1991-01-01" "1986-01-01" "1991-01-01" "1986-01-01" "1991-01-01" >>>>> "1991-01-01" "1991-01-01" "1991-01-01" "1991-01-01" "1991-01-01" >>>>> >>>>> >>>>> >>>>> Now i need to turn the dates into AGE, how do i do it? Im not worried >>>>> about >>>>> fractions of years, whole years would do. >>>>> >>>>> >>>>> -- >>>>> View this message in context: >>>>> http://old.nabble.com/Turn-dates-into-age-tp26256656p26256656.html >>>>> Sent from the R help mailing list archive at Nabble.com. >>>>> >>>>> ______________________________________________ >>>>> [hidden email] 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. >>>>> >>>> >>>> >>>> >>>> -- >>>> Jim Holtman >>>> Cincinnati, OH >>>> +1 513 646 9390 >>>> >>>> What is the problem that you are trying to solve? >>>> >>>> ______________________________________________ >>>> [hidden email] 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. >>>> >>>> >>> >>> >> >> -- >> View this message in context: >> http://old.nabble.com/Turn-dates-into-age-tp26256656p26257435.html >> Sent from the R help mailing list archive at Nabble.com. >> >> ______________________________________________ >> [hidden email] 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. > > David Winsemius, MD > Heritage Laboratories > West Hartford, CT > > ______________________________________________ > [hidden email] 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. > > [[alternative HTML version deleted]] ______________________________________________ [hidden email] 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. |
||||||||||||||||
|
Marc Schwartz-3
|
> Sys.Date()
[1] "2009-11-08" > as.Date("05/29/1971", format = "%m/%d/%Y") [1] "1971-05-29" > as.numeric((Sys.Date() - as.Date("05/29/1971", format = "%m/%d/ %Y")) / 365.25) [1] 38.44764 or perhaps more clearly: EndDate <- Sys.Date() StartDate <- as.Date("05/29/1971", format = "%m/%d/%Y") > as.numeric((EndDate - StartDate) / 365.25) [1] 38.44764 We coerce to numeric here, to return a standard numeric value, rather than the result being of class difftime with an attribute of 'days' for units: > str((Sys.Date() - as.Date("05/29/1971", format = "%m/%d/%Y")) / 365.25) Class 'difftime' atomic [1:1] 38.4 ..- attr(*, "units")= chr "days" HTH, Marc Schwartz On Nov 8, 2009, at 5:22 PM, Jim Burke wrote: > To clarify. > > Lets turn a date into an age. Given 05/29/1971 in mm/dd/yyyy > format. What is the year difference between then and today? > This would be the "age" requested that starts 05/29/1971 as > one. > > Thanks, > Jim > > > > > David Winsemius wrote: >> >> On Nov 8, 2009, at 3:11 PM, frenchcr wrote: >> >>> >>> >>> why do you use 365.25? >>> >> >> As opposed to what? >> >> -- David >>> >>> dates<-as.character(data[,"date_commissioned"]); # convert dates to >>> characters >>> #dates[1:10] >>> #[1] "19910101" "19860101" "19910101" "19860101" "19910101" >>> "19910101" >>> "19910101" "19910101" "19910101" "19910101" >>> >>> dateObs <- as.Date(dates,format="%Y%m%d") >>> #dateObs[1:10] >>> #[1] "1991-01-01" "1986-01-01" "1991-01-01" "1986-01-01" >>> "1991-01-01" >>> "1991-01-01" "1991-01-01" "1991-01-01" "1991-01-01" "1991-01-01" >>> >>> today <- Sys.Date() >>> x.date <- as.Date(dateObs, format="%Y%m%d") >>> >>> AGE <- round(as.vector(difftime(today , x.date, units='day') / >>> 365.25)) >>> >>> >>> >>> >>> >>> frenchcr wrote: >>>> >>>> >>>> it sure does thank you! >>>> >>>> >>>>> will this work for you >>>>> >>>>> x <- c('19910101', '19950302', '20010502') >>>>> today <- Sys.Date() >>>>> x.date <- as.Date(x, format="%Y%m%d") >>>>> round(as.vector(difftime(today , x.date, units='day') / 365.25)) >>>> [1] 19 15 9 >>>>> >>>> >>>> >>>> On Sun, Nov 8, 2009 at 2:44 PM, <[hidden email]> wrote: >>>>> Hi Jim, >>>>> >>>>> Thanks for the quick reply...not sure what you mean by frame of >>>>> reference(only been using R for 4 days)...to clarify, i need to >>>>> turn my >>>>> dates from 1999-10-01 into 1999 then i subtract 2009 -1999 to get >>>>> an age >>>>> of 10. The column im working on has 312,000 rows and some have NA >>>>> in them >>>>> as we have no dates for that item. >>>>> >>>>> To recap, the column is just a bunch of dates with some field >>>>> empty, i >>>>> want to change the column from "date of commision" to "age of >>>>> asset" >>>>> >>>>> Cheers >>>>> Chris. >>>> >>>> >>>> >>>> >>>> jholtman wrote: >>>>> >>>>> What is the frame of reference to determine the age? Check out >>>>> 'difftime'. >>>>> >>>>> On Sun, Nov 8, 2009 at 1:50 PM, frenchcr <[hidden email]> >>>>> wrote: >>>>>> >>>>>> Ive got a big column of dates (also some fields dont have a date >>>>>> so they >>>>>> have >>>>>> NA instead), >>>>>> that i have converted into date format as so... >>>>>> >>>>>> >>>>>> dates<-as.character(data[,"date_commissioned"]); # converted >>>>>> dates to >>>>>> characters >>>>>> dates[1:10] >>>>>> [1] "19910101" "19860101" "19910101" "19860101" "19910101" >>>>>> "19910101" >>>>>> "19910101" "19910101" "19910101" "19910101" >>>>>> >>>>>> dateObs <- as.Date(dates,format="%Y%m%d") >>>>>> dateObs[1:10] >>>>>> [1] "1991-01-01" "1986-01-01" "1991-01-01" "1986-01-01" >>>>>> "1991-01-01" >>>>>> "1991-01-01" "1991-01-01" "1991-01-01" "1991-01-01" "1991-01-01" >>>>>> >>>>>> >>>>>> >>>>>> Now i need to turn the dates into AGE, how do i do it? Im not >>>>>> worried >>>>>> about >>>>>> fractions of years, whole years would do. >>>>>> >>>>>> >>>>>> -- >>>>>> View this message in context: >>>>>> http://old.nabble.com/Turn-dates-into-age- >>>>>> tp26256656p26256656.html >>>>>> Sent from the R help mailing list archive at Nabble.com. >>>>>> >>>>>> ______________________________________________ >>>>>> [hidden email] 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. >>>>>> >>>>> >>>>> >>>>> >>>>> -- >>>>> Jim Holtman >>>>> Cincinnati, OH >>>>> +1 513 646 9390 >>>>> >>>>> What is the problem that you are trying to solve? >>>>> >>>>> ______________________________________________ >>>>> [hidden email] 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. >>>>> >>>>> >>>> >>>> >>> >>> -- >>> View this message in context: >>> http://old.nabble.com/Turn-dates-into-age-tp26256656p26257435.html >>> Sent from the R help mailing list archive at Nabble.com. >>> >>> ______________________________________________ >>> [hidden email] 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. >> >> David Winsemius, MD >> Heritage Laboratories >> West Hartford, CT >> >> ______________________________________________ >> [hidden email] 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. >> >> > > > [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] 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. |
||||||||||||||||
| Free Embeddable Forum Powered by Nabble | Help |