[R] length of object in repeated measures

jim holtman jholtman at gmail.com
Mon Feb 9 13:45:41 CET 2009


This should do it for you:

> x <- read.table('clipboard', header=TRUE)
> x
  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
> x$NoCap <- ave(x$Id, x$Id, FUN=length)
> x
  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:
>
> 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
>
>
>
>
>
>
> --
> View this message in context: http://www.nabble.com/length-of-object-in-repeated-measures-tp21911978p21911978.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?




More information about the R-help mailing list