[R] calculating age from year of birth and date?
Dimitri Liakhovitski
ld7631 at gmail.com
Wed Apr 21 16:18:29 CEST 2010
Give what you've given us, you can try:
x<-data.frame(date=c(39804,9527,39917),birth=c(1988,1911,1969)) # your
years have to be in 4-digit format
# formatting date as Date. What you have is, I think, number of days
since 1900 - can anyone correct me?
x$date<-as.Date(x$date,origin="1900-01-01")
x$birth<-as.character(x$birth) # has to be formatted as characters first
x$birth<-as.Date(x$birth,"%Y") # now "birth" is also in date format
x$age<-x$date-x$birth # gives you age in days
x
# I am not sure how to translate x$age - which is in days - into
years. Here is how I would do it:
x$age<-as.numeric(x$age)
x$age<-x$age/365
x
Dimitri
On Wed, Apr 21, 2010 at 10:06 AM, Dimitri Liakhovitski <ld7631 at gmail.com> wrote:
> And what is the format for year of birth in Excel?
> Can't you use "concatenate" to concatenate a new column (that contains
> only 19) and the column that contains a 2-digit year of birth?
> And then change all those who are born (in the new column) before 1910
> to XX+100 - which will give you 2009 (or 2001), etc.?
>
> On Wed, Apr 21, 2010 at 9:57 AM, Vlatka Matkovic Puljic
> <v.matkovic.puljic at gmail.com> wrote:
>> Hi,
>>
>> well...when imported to R (from xls file) it transform to number
>> print(D1)
>> [1] 39804 39527 39917 39860 39489
>> ???
>> but in xls is dd/mm/yyyy
>>
>> and year of birth is as yy
>> those born after 1999 are removed
>>
>>
>> Dana 21. travnja 2010. 15:52 Dimitri Liakhovitski <ld7631 at gmail.com> je
>> napisao/la:
>>>
>>> In what format are you dates?
>>> Are they always like this: two digits for day, then one or two digits
>>> for month, and four digits for the year?
>>> Also - are any of your people born after 1999?
>>>
>>> Dimitri
>>>
>>>
>>> 2010/4/21 Vlatka Matković Puljić <vlatka.m at gmail.com>:
>>> > Hi,
>>> >
>>> > I have v1 (date of test) and v2 (year of birth).
>>> > v1 v2
>>> > 15.5.2008 88
>>> > 18.6.2009 54
>>> >
>>> > I want R to use only year in v1; and v2 see as 1988 and calculate age in
>>> > v3.
>>> > any ideas how to do that?
>>> >
>>> > --
>>> > **************************
>>> > Vlatka Matković Puljić
>>> > 095/8618 171
>>> >
>>> > [[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.
>>> >
>>> >
>>>
>>>
>>>
>>> --
>>> Dimitri Liakhovitski
>>> Ninah.com
>>> Dimitri.Liakhovitski at ninah.com
>>
>>
>>
>> --
>> **************************
>> Vlatka Matkovic Puljic
>> 095/8618 171
>>
>
>
>
> --
> Dimitri Liakhovitski
> Ninah.com
> Dimitri.Liakhovitski at ninah.com
>
--
Dimitri Liakhovitski
Ninah.com
Dimitri.Liakhovitski at ninah.com
More information about the R-help
mailing list