[R] length of object in repeated measures

Dimitris Rizopoulos d.rizopoulos at erasmusmc.nl
Mon Feb 9 13:46:03 CET 2009


in this case you can use ave(), e.g., say 'dat' is the name of your data 
frame, then try this:

dat$NoCaps <- ave(dat$Id, dat$Id, FUN = length)
dat


I hope it helps.

Best,
Dimitris


clion wrote:
> Hi there.
> I collectad data of several animals (Id) that were caught and measured at
> several occasions.
> The dataframe looks like this:
> Grouped Data: mass ~ age | Id
> Id      age       mass
> 1        1           5.4
> 1        3           6.2
> 1       15         10.0
> 2        3           8.1
> 2       10         12.8
> 3        2           5.9
> 3       10          7.1
> 3       15         15.4
> 3       17         16.2
> 
> Now, I would like to add a column that shows the number of captures per
> Animal (so it'll look like this:) 
> Id  ....   NoCaps
> 1   ....     3
> 1   ....     3
> 1   ....     3
> 2   ....     2
> 2   ....     2
> 3   ....     4  
> 3   ....     4
> ....
>  
> I understand that with 
> tapply(Id,Id, length)
> I can find out how many times each animal was caught, but how do I get this
> information into an extra column? I'm sure, this is an easy question, but
> I'm lost, where to find the answer, or especially where to look it up. if
> this is not a new question, please give me the key words to look for it...
> 
> thanks in advance
> 
> 
> 
> 
> 
> 

-- 
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014




More information about the R-help mailing list