使用R计算矩阵中最丰富的物种(例)

时间:2019-11-22 16:27:29

标签: r count

我想对数据集使用一些基本的探索性命令,但无法弄清楚这一点:我希望能够根据我的数据将十个(或任何其他数量)最丰富的物种归还给我-矩阵。

因此,所有位点(行)中存在的物种最终排名最高,仅一个位点中存在的物种最终排名较低(甚至可以从十种最丰富的物种中排除)。

此外,每个物种都有一个与其在站点(行)中的丰度相对应的数字。我还希望能够根据丰富的拉特然后出现的缺失来获取前十名最丰富的物种。

在这里,我用dput给了我一部分数据集。

structure(list(Hypnum.jutlandicum = c(1L, 0L, 18L, 0L, 18L, 43L, 
0L, 0L, 0L, 0L, 0L, 68L, 8L, 0L, 0L, 0L, 0L, 68L), Cladonia.floerkeana = c(0L, 
0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 2L, 0L, 0L, 0L, 0L, 0L, 
0L), Dicranum.scoparium = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 2L, 0L, 0L, 0L, 2L, 0L, 0L), Deschampsia.flexuosa = c(0L, 
0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L), Carex.pilulifera = c(1L, 4L, 1L, 3L, 1L, 1L, 2L, 0L, 2L, 
0L, 0L, 2L, 2L, 2L, 2L, 1L, 0L, 0L), Polytrichum.commune = c(1L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L), Calluna.vulgaris = c(43L, 38L, 56L, 68L, 56L, 68L, 38L, 
88L, 68L, 38L, 68L, 88L, 38L, 38L, 18L, 8L, 18L, 38L), Danthonia.decumbens = c(1L, 
0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 
0L), Genista.pilosa = c(0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Polytrichum.juniperinum = c(0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 3L, 3L, 68L, 0L, 3L, 3L, 2L, 2L, 
0L, 0L), Micarea.erratica = c(1L, 3L, 4L, 4L, 1L, 1L, 4L, 8L, 
18L, 18L, 4L, 3L, 0L, 2L, 3L, 2L, 0L, 4L), Trapelia.coarctata = c(0L, 
0L, 4L, 0L, 0L, 0L, 4L, 4L, 8L, 8L, 8L, 2L, 2L, 8L, 0L, 0L, 2L, 
4L), Baeomyces.rufus = c(2L, 2L, 1L, 2L, 4L, 1L, 6L, 2L, 3L, 
0L, 3L, 8L, 68L, 0L, 0L, 0L, 0L, 0L), Porpidia.crustulata = c(0L, 
0L, 0L, 0L, 0L, 0L, 0L, 2L, 0L, 2L, 2L, 0L, 0L, 4L, 1L, 0L, 0L, 
0L), Catillaria.chalybeia = c(0L, 0L, 0L, 0L, 0L, 0L, 2L, 2L, 
0L, 3L, 3L, 0L, 0L, 2L, 0L, 1L, 0L, 0L), Rhizocarpon.reductum = c(0L, 
0L, 0L, 0L, 0L, 0L, 2L, 3L, 0L, 2L, 4L, 1L, 0L, 0L, 2L, 0L, 0L, 
0L), Porina.chlorotica = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
2L, 2L, 2L, 0L, 0L, 0L, 0L, 0L, 0L), Acarospora.fuscata = c(0L, 
0L, 0L, 0L, 0L, 0L, 2L, 0L, 0L, 3L, 1L, 2L, 0L, 0L, 0L, 0L, 0L, 
0L), Dibaeis.baeomyces = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 3L, 
0L, 0L, 3L, 0L, 0L, 2L, 0L, 0L, 0L), Erica.tetralix = c(18L, 
2L, 1L, 0L, 31L, 1L, 2L, 3L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 
0L, 68L), Campylopus.introflexus = c(1L, 38L, 0L, 0L, 1L, 0L, 
38L, 18L, 8L, 8L, 0L, 18L, 0L, 38L, 0L, 2L, 0L, 2L), Cladonia.coccifera = c(0L, 
4L, 4L, 0L, 1L, 1L, 0L, 0L, 2L, 0L, 0L, 0L, 3L, 0L, 1L, 0L, 0L, 
4L), Cladonia.grayi = c(0L, 2L, 1L, 0L, 1L, 1L, 0L, 2L, 2L, 0L, 
0L, 2L, 3L, 0L, 0L, 0L, 0L, 0L), Agrostis.vinealis = c(1L, 0L, 
1L, 0L, 0L, 1L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
), Cladonia.macilenta = c(0L, 0L, 1L, 0L, 1L, 0L, 0L, 2L, 0L, 
0L, 0L, 2L, 2L, 0L, 0L, 0L, 0L, 2L), Polytrichum.piliferum = c(0L, 
0L, 18L, 38L, 0L, 1L, 2L, 0L, 0L, 0L, 0L, 2L, 0L, 0L, 0L, 0L, 
3L, 0L), Rumex.acetosella = c(0L, 0L, 0L, 2L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 2L, 0L, 0L, 0L, 0L, 0L), Cladonia.ramulosa = c(0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 
0L), Hypochaeris.radicata = c(0L, 0L, 0L, 2L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Molinia.caerulea = c(1L, 
2L, 1L, 4L, 8L, 1L, 3L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 2L, 2L, 3L, 
0L), Carex.arenaria = c(0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 2L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Cladonia.fimbriata = c(0L, 0L, 
0L, 0L, 0L, 0L, 1L, 2L, 2L, 0L, 0L, 2L, 0L, 0L, 0L, 0L, 0L, 0L
), Pinus.sylvestris = c(0L, 2L, 1L, 2L, 1L, 0L, 2L, 2L, 2L, 2L, 
1L, 2L, 2L, 8L, 2L, 2L, 18L, 0L), Quercus.robur = c(0L, 0L, 0L, 
1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), 
    Betula.pendula = c(0L, 0L, 0L, 2L, 0L, 0L, 0L, 2L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Cephaloziella.divaricata = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L), Pseudoscleropodium.purum = c(0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 2L, 0L, 0L, 0L, 0L, 0L), Cladonia.humilis = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 2L), Festuca.species = c(0L, 0L, 1L, 0L, 0L, 1L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Brachythecium.rutabulum = c(0L, 
    0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L), Carex.panicea = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 2L), Sorbus.aucuparia.1 = c(1L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L), Ceratodon.purpureus = c(0L, 0L, 0L, 0L, 0L, 0L, 
    2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 2L, 0L, 0L), Polytrichum.formosum = c(0L, 
    0L, 0L, 0L, 0L, 0L, 2L, 2L, 0L, 0L, 0L, 3L, 0L, 0L, 0L, 0L, 
    0L, 0L), Bryum.capillare = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L), Lecidea.lithophila = c(0L, 
    0L, 0L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L), Amelanchier.lamarckii = c(0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L), Salix.aurita.1 = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 1L), Cephaloziella.species = c(0L, 0L, 0L, 0L, 0L, 1L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Betula.pubescens = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 2L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L), Lecidella.scabra = c(0L, 0L, 0L, 0L, 1L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Calamagrostis.epigejos = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L), Placynthiella.icmalea = c(0L, 4L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Geranium.robertianum = c(0L, 
    0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L), Micarea.lynceola = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Porpidia.soredizodes = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L), Scoliciosporum.umbrinum = c(0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Trapelia.obtegens = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L), Deschampsia.flexuosa.2 = c(0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 2L, 0L, 0L, 0L, 0L, 0L, 0L), Rubus.plicatus.1 = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 2L, 0L, 0L, 
    0L, 0L), Bryum.spec. = c(0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Cladonia.monomorpha.1 = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L), Geisleria.sychnogonoides = c(0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 8L, 0L, 0L, 4L), Micarea.lignaria = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 4L)), class = "data.frame", row.names = c(274784L, 274553L, 
272517L, 275195L, 272514L, 274783L, 99L, 113L, 96L, 109L, 117L, 
102L, 114L, 97L, 138L, 133L, 95L, 237L))

我希望问题很明确,否则我会扩大。

1 个答案:

答案 0 :(得分:1)

!嗨,我想我理解您的问题。

对我而言,最简单的思考方法是,将网站变量设置为变量,然后获取广泛的数据并使其尽可能长:

int n;

for(int i=0; i < 5; ++i)
{
    // read first number eg 12345
    scanf("%d",&n);
    int d1 = n % 10;    // ones place = 5
    n /= 100;           // n becomes 123
    int d100 = n % 10;  // hundreds place = 3
    n /= 100;           // n becomes 1
    int d10000 = n % 10;    // ten thousands place = 1

    int smallSum = 100*d10000 + 10*d100 + d1;
    // prints 135 
    printf("sum version 1 = %d\n", smallSum); 

    int bigSum = 10000*d10000 + 100*d100 + d1;
    // prints 10305
    printf("sum version 2 = %d\n",bigSum);     
}

现在,我们可以过滤丰度= 0的网站并做一些总结:

df <- df %>%
   mutate(site_no = 1:nrow(.)) %>%
   gather(species, abundance, -site_no)

这里的计数是它们所在的站点数,总数是总数。在变量和子集上使用ranging将获得您感兴趣的答案(使用-count或-total)。