[R] Applying a function

Rui Barradas ruipbarradas at sapo.pt
Sat Dec 24 00:21:44 CET 2011


Joanie Van De Walle wrote
> 
> Hi,
> 
>    I need help writing a function
> 
>    I capture seal pups mutliple times during the lactation season in order
> to
>    monitor their growth rate. When I release them, the recovery
> (mother-pup)
>    time is not the same for all individuals. I want to know if individuals
> that
>    recover their mother the fastest are the ones with the highest growth
> rates.
> 
>    So, I noted at every release if the pup reovered his mother before we
> leave
>    (yes or no). My dataframe looks like this
> 
>    Capture nb individual individual capture motherrecovery growth rate
>    1          1          1                  n              0.5
>    2          1          2                  y              0.5
>    3          1          3                  y              0.5
>    4          1          4                  y              0.5
>    5          1          5                  n              0.5
>    6          2          1                  y              0.3
>    7          2          2                  y              0.3
>    8          3          1                  y              0.4
>    9          3          2                  n              0.4
>    10         3          3                  y              0.4
>    ...
> 
>    I want to calculate a rate of mother recovery by individual, i.e. nb of
>    recoveries (y)/nb of captures. So for indivial 1 it would be 3/5 = 0.6.
> I
>    want  to write a function that does this for all the individuals in my
>    dataframe,  i.e. around 400 individuals (this is why I want to write a
>    function, it would be too long by hand)
> 
>    Thank you,
> 
> 
> ______________________________________________
> R-help@ 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.
> 

Hello,

Here is a one line function

recovery.rate <- function(x) unlist(lapply(split(tab1, tab1[,2]),
function(x) mean(x[[4]]=="y")))

It works with that table. I hope it helps

Merry Christmas

Rui Barradas


--
View this message in context: http://r.789695.n4.nabble.com/Applying-a-function-tp4229277p4230100.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list