0

I am trying to count the number of occurrences of species (distributed along an altitude gradient) in 'class' of altitudes.

Here is the original data frame :

tab
    R sp N Hauteur Alt Plot Quadrat Microhab Cover
1  R2  B 1  0-50cm 350   P1      Q1       TA    50
2  R2  D 1  0-50cm 350   P1      Q1       TA    50
3  R3  A 2  0-50cm 550   P1      Q1       TA    95
4  R3  C 1  0-50cm 550   P1      Q1       TA    95
5  R3  B 3  0-50cm 550   P1      Q1       TA    95
6  R3  D 4  0-50cm 550   P1      Q1       TA    95
7  R4  C 3  0.5-1m 350   P1      Q1       TB    50
8  R4  A 4  0.5-1m 350   P1      Q1       TB    50
9  R4  E 2  0.5-1m 350   P1      Q1       TB    50
10 R4  D 3  0.5-1m 350   P1      Q1       TB    50
11 R4  B 4  0.5-1m 350   P1      Q1       TB    50

and here is what I would like to obtain :

head(parametres)
SP        Altmin  Altmax   Altmoy  nb_350.549 nb_550.749 [...]   %_350.549m
A          350     550      450        1          0       ...         50
B          350     550      416.67     2          1       ...         33.3
C          350     550      450        1          0       ...         50

For that I manage to get the firts 4 columns ("SP","Altmin","Altmax","Altmoy"). But I don't manage to get the number of occurrence by 'class of altitude' (e.g. "nb_350.549"), nor the percentage of representativeness of each altitude class among all, regarding the number of occurrence of each species (e.g. "%_350.549) - which is in fact = nb.occ.SP(A) / nb_350.549 *100

Here is my script :

parametres<-ddply(tab,.(sp),function(esp){
+     summarise(esp,
+               Altmin=min(Alt),
+               Altmax=max(Alt),
+               Altmoy=mean(Alt),
+   )})

Any idea?

Thanks!

R.

  • Every `Alt` value in the original dataset has 350 as value. Why the first line reports 800 as `Altmin`? I guess that the example data is wrong. Please provide something more "realistic", so everybody can check the expected result. – nicola Jul 23 '15 at 07:26
  • Hi, it's modified! For sure it's better like that! Thanks for your advise! –  Jul 23 '15 at 07:34
  • Here are some hints on how to think in terms of a reproducible example: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Roman Luštrik Jul 23 '15 at 08:23

1 Answers1

0

Here is a dplyr solution

tab <- read.table(text="R sp N Hauteur Alt Plot Quadrat Microhab Cover
R2  B 1  0-50cm 350   P1      Q1       TA    50
R2  D 1  0-50cm 350   P1      Q1       TA    50
R3  A 2  0-50cm 550   P1      Q1       TA    95
R3  C 1  0-50cm 550   P1      Q1       TA    95
R3  B 3  0-50cm 550   P1      Q1       TA    95
R3  D 4  0-50cm 550   P1      Q1       TA    95
R4  C 3  0.5-1m 350   P1      Q1       TB    50
R4  A 4  0.5-1m 350   P1      Q1       TB    50
R4  E 2  0.5-1m 350   P1      Q1       TB    50
R4  D 3  0.5-1m 350   P1      Q1       TB    50
R4  B 4  0.5-1m 350   P1      Q1       TB    50",head=TRUE)

library(dplyr)

tab %>% group_by(sp) %>%
  summarize(n=n(),
            Altmin=min(Alt),
            Altmax=max(Alt),
            Altmoy=mean(Alt),
            nb_350.549=sum(ifelse(Alt >= 350 & Alt < 550,1,0)),
            nb_550.749=sum(ifelse(Alt >= 550 & Alt < 749,1,0))) %>%
  mutate(f_350.549=nb_350.549/n*100,
         f_550.749=nb_550.749/n*100)

Source: local data frame [5 x 9]

  sp n Altmin Altmax   Altmoy nb_350.549 nb_550.749 f_350.549 f_550.749
1  A 2    350    550 450.0000          1          1  50.00000  50.00000
2  B 3    350    550 416.6667          2          1  66.66667  33.33333
3  C 2    350    550 450.0000          1          1  50.00000  50.00000
4  D 3    350    550 416.6667          2          1  66.66667  33.33333
5  E 1    350    350 350.0000          1          0 100.00000   0.00000
scoa
  • 19,359
  • 5
  • 65
  • 80
  • @RémyPoncet be sure to use `library(dplyr)`, this function is included (though it's originally from `library(magrittr)`. I am not sure the same code works with ddply – scoa Jul 23 '15 at 08:51
  • (in reply to your now deleted comment) – scoa Jul 23 '15 at 08:52
  • Yes, I have deleted it, as your proposal worked in fact ;) - but as I tried it with ddply it didn't. Many thanks! –  Jul 23 '15 at 08:58