Skip to content

length of object in repeated measures

5 messages · clion, jim holtman, Dimitris Rizopoulos +1 more

#
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
#
This should do it for you:
Id age mass
1  1   1  5.4
2  1   3  6.2
3  1  15 10.0
4  2   3  8.1
5  2  10 12.8
6  3   2  5.9
7  3  10  7.1
8  3  15 15.4
9  3  17 16.2
Id age mass NoCap
1  1   1  5.4     3
2  1   3  6.2     3
3  1  15 10.0     3
4  2   3  8.1     2
5  2  10 12.8     2
6  3   2  5.9     4
7  3  10  7.1     4
8  3  15 15.4     4
9  3  17 16.2     4

        
On Mon, Feb 9, 2009 at 7:36 AM, clion <birte_2 at hotmail.com> wrote:

  
    
#
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:

  
    
#
this is good, but it doesn't solve my main problem (which I unfortunately
din't post - very sorry )
I would like to filter may data , for example by:

dat.sub<-dat[dat$age>10 & dat$NoCaps>2,]

So I need a column where the number of Captures is repeated for all rows of
each captured animal.
(or is there a better way to get a subset?)

thanks.
Dimitris Rizopoulos-4 wrote:
:wistle::wistle::rules::rules::rules: