These functions convert a community data to compressed sparse matrix, dense matrix and long format (e.g. species records).

long2sparse(x, grids = "grids", species = "species")

sparse2long(x)

dense2sparse(x)

sparse2dense(x)

long2dense(x)

dense2long(x)

Arguments

x

A community data which one wants to transform

grids

column name of the column containing grid cells

species

column name of the column containing the species / taxa names

Value

A compressed sparse community matrix of sites by species

Examples

data(africa)
africa$comm[1:5, 1:20]
#> 5 x 20 sparse Matrix of class "dgCMatrix"
#>   [[ suppressing 20 column names ‘Abutilon_angulatum_OM1934’, ‘Abutilon_sonneratianum_LTM034’, ‘Acalypha_chirindica_OM2341’ ... ]]
#>                                              
#> v3635 . . . . . . . . . 1 . . . . . . . . . .
#> v3636 . . . . . . . . . 1 . . . . . . . . . 1
#> v3637 . . . . . . . . . 1 . . . . . . . . . 1
#> v3638 . . . . . . . . . 1 . . . . . . . . . .
#> v3639 . . . . . . . . . 1 . . . . . . . . . .
long <- sparse2long(africa$comm)
long[1:5, ]
#>   grids                   species
#> 1 v3757 Abutilon_angulatum_OM1934
#> 2 v3758 Abutilon_angulatum_OM1934
#> 3 v3759 Abutilon_angulatum_OM1934
#> 4 v3826 Abutilon_angulatum_OM1934
#> 5 v3827 Abutilon_angulatum_OM1934
sparse <- long2sparse(long)
all.equal(africa$comm, sparse)
#> [1] "Component “Dimnames”: Component 2: 10 string mismatches"
#> [2] "Mean relative difference: 2"                            

dense_comm <- matrix(c(1,0,1,1,0,0,
                1,0,0,1,1,0,
                1,1,1,1,1,1,
                0,0,1,1,0,1), 6, 4,
              dimnames=list(paste0("g",1:6), paste0("sp", 1:4)))
dense_comm
#>    sp1 sp2 sp3 sp4
#> g1   1   1   1   0
#> g2   0   0   1   0
#> g3   1   0   1   1
#> g4   1   1   1   1
#> g5   0   1   1   0
#> g6   0   0   1   1
sparse_comm <- dense2sparse(dense_comm)
sparse_comm
#> 6 x 4 sparse Matrix of class "dgCMatrix"
#>    sp1 sp2 sp3 sp4
#> g1   1   1   1   .
#> g2   .   .   1   .
#> g3   1   .   1   1
#> g4   1   1   1   1
#> g5   .   1   1   .
#> g6   .   .   1   1
sparse2long(sparse_comm)
#>    grids species
#> 1     g1     sp1
#> 2     g3     sp1
#> 3     g4     sp1
#> 4     g1     sp2
#> 5     g4     sp2
#> 6     g5     sp2
#> 7     g1     sp3
#> 8     g2     sp3
#> 9     g3     sp3
#> 10    g4     sp3
#> 11    g5     sp3
#> 12    g6     sp3
#> 13    g3     sp4
#> 14    g4     sp4
#> 15    g6     sp4