Applying a function

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Applying a function

Joanie Van De Walle

   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,



   Joanie Van de Walle

   Ã‰tudiante à la Maîtrise en Biologie
   Département de Biologie
   1045, avenue de la Médecine Pavillon Vachon,
   bureau 2044 Université Laval
   Québec, Canada, G1V 0A6
______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Applying a function

Steinar
First, it may be a good idea to use 1 for “yes” and 0 for “no” in the
motherrecovery column.

Then if you name your table e.g tab1,  you may try something like this..

sum(tab1[,2]==1*tab1[,4])/sum(tab1[,2]==1)

2011/12/23 Joanie Van De Walle <[hidden email]>

>
>   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,
>
>
>
>   Joanie Van de Walle
>
>   Étudiante à  la Maîtrise en Biologie
>   Département de Biologie
>   1045, avenue de la Médecine Pavillon Vachon,
>   bureau 2044 Université Laval
>   Québec, Canada, G1V 0A6
> ______________________________________________
> [hidden email] 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.
>
        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Applying a function

Rui Barradas
In reply to this post by Joanie Van De Walle
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,


______________________________________________
[hidden email] 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
Reply | Threaded
Open this post in threaded view
|

Re: Applying a function

Rui Barradas
In reply to this post by Joanie Van De Walle
Sorry, in the function body, NO 'tab1', use 'x' only:

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

The error is because 'tab1' existed in the environment and the function would find it.

This time, tested after removing 'tab1'.

Rui Barradas