Raúl Diaz
Raúl Diaz

Reputation: 3

Suggest needed aggregate R function

I have this data matrix:

"datos"

    n.rock species  altitude site
    R1     sp1      2000    s1
    R1     sp2      2000    s1
    R1     sp3      2000    s1
    R2     sp1      1000    s2
    R2     sp2      1000    s2
    R3     sp1      2700    s3
    R4     sp1      1800    s4
    R4     sp2      1800    s4
    R4     sp3      1800    s4

I want to calculate the number of species per rock, so I use:

nro_sp <- aggregate(datos[,2], by=list(datos[,1]), FUN=length)

I get:

nro_sp
        n.rock  x
        R1      3
        R2      2
        R3      1
        R4      3

I get the number of species per rock, that is exactly what I want, the problem is I loose the other variables I need.

I need:

    n.rock  x  altitude site      
    R1      3  2000     s1
    R2      2  1000     s2
    R3      1  2700     s3
    R4      3  1800     s4

I tried using cbind but my tables have different numbers of row so it does not work.

Upvotes: 0

Views: 86

Answers (2)

SeGa
SeGa

Reputation: 9819

Would that be sufficient?

aggregate(species ~ ., data = datos, FUN = length)

You loose the species names and get the "length" instead.

or a data.table solution:

library(data.table)
setDT(datos)
datos[  , .(n_species = length(species)), by = .(n.rock, site, altitude)]
  n.rock altitude site species
1     R1     2000   s1       3
2     R2     1000   s2       2
3     R3     2700   s3       1
4     R4     1800   s4       3

Upvotes: 3

user10917479
user10917479

Reputation:

It seems like n.rock, altitude, and site are all 1-to-1 with each other.

I like to do this with package dplyr. This does not have species in it because there is not a single unique value that can be grouped back with n.rock.

library(dplyr)

datos %>%
  group_by(n.rock, altitude, site) %>%
  summarise(count.species = n_distinct(species))

 n.rock altitude site  count.species
  <chr>     <dbl> <chr>         <int>
1 R1         2000 s1                3
2 R2         1000 s2                2
3 R3         2700 s3                1
4 R4         1800 s4                3

Upvotes: 3

Related Questions