rvegan

specnumber() function from vegan package mixes up the species richness


I have 2 dataframes, one containing observations of species and their abundance in different sites (1 line = 1 species for each site) called obs_sp

                   species Count   Site 
1                    A        2  Site_1
3                    A        3  Site_2
4                    C        1  Site_2
5                    A        4  Site_3  
6                    B        6  Site_3
7                    C        1  Site_3

> dput(obs_sp) 
structure(list(Species = c("A", "A", "C", "A", "B", "C"), Count = c(2L, 
3L, 1L, 4L, 6L, 1L), Site = c("Site_1", "Site_2", "Site_2", "Site_3", 
"Site_3", "Site_3")), class = "data.frame", row.names = c(NA, 
-6L))

The second one gives characteristics about each site (habitat, management, etc.) called caract_sites

Site   Habitat 
Site_1 C1
Site_2 E3
Site_3 C2

> dput(caract_site)
structure(list(Site = c("Site_1", "Site_2", "Site_3"), Habitat = c("C1", 
"E3", "C2")), class = "data.frame", row.names = c(NA, -3L))

I have converted the first table into a community data matrix called site_sp_ok using dcast() function, each line being a site and each column a species called


        A   B   C 
Site_1  2   0   0
Site_2  3   0   1
Site_3  4   6   1 

> dput(site_sp_ok)
structure(list(A = 2:4, B = c(0, 0, 6), C = c(0, 1, 1)), class = "data.frame", row.names = c("Site_1", 
"Site_2", "Site_3"))

I am now trying to count the species richness per site and insert it into the caract_sites table, by creating a new column. To do so, I am using the specnumber() function as follow :

caract_sites <- caract_sites %>%
  add_column(specnumber(site_sp_ok))

And in return the algorithm creates the function, however it seems like it is mixing the different sites, returning something like :

Site   Habitat sp_richness
Site_1 C1      3
Site_2 E3      1
Site_3 C2      2

Where it is supposed to return

Site   Habitat sp_richness
Site_1 C1      1
Site_2 E3      2
Site_3 C2      3

And I was wondering where could the issue comes from ? I did not find indications on the specnumnber help page indicating how to connect rows from **site_sp_ok ** with Site column in caract_site

The problem seems trivial, and I am sorry if it is, but this could become a big problem for the following data processings and interpretations.

Edit - Sorry for initially posting it on Meta it was a mistake, I did not notice I was no longer on Stack Overflow !


Solution

  • I don't exactly know how your error is occurring, as I cannot seem to replicate it myself. I'm getting your desired dataframe as output even with your code. That said, the below works as well, and relies on dplyr from the tidyverse

    library(tidyverse)
    library(vegan)
    
    caract_sites <- caract_sites |> #|> is the same as %>%, but preferred by the r style guide
      mutate(specnumber = specnumber(site_sp_ok))
    

    When adding or changing rows in a dataframe I find dplyr::mutate() works well for me.