简体   繁体   English

创建具有丰度(计数)的物种矩阵

[英]Create species matrix with abundance (counts)

I have a dataset (as.data.frame) like this one:我有一个这样的数据集(as.data.frame):

Site地点 Species物种 Count数数
a一种 Abies冷杉 14 14
b Alnus阿尔努斯 1 1
c C Pinus 1 1
c C Artem阿尔特姆 2 2
n n ... ... ... ...

, n of sites = 26000. I need to convert it into a matrix like this one in R: , n of sites = 26000. 我需要将其转换为 R 中这样的矩阵:

Abies冷杉 Alnus阿尔努斯 Pinus Artem阿尔特姆
a一种 14 14 0 0 0 0 0 0
b 0 0 1 1 0 0 0 0
c C 0 0 0 0 1 1 2 2
n n ... ... ... ... ... ... ... ...

I came across the 'fossil' package, with the create.matrix fuction.我遇到了带有 create.matrix 功能的 'fossil' 包。 This function creates the matrix I need but only with the presence (1) or absence (0) of each species for each site.这个函数创建了我需要的矩阵,但只有每个站点的每个物种存在 (1) 或不存在 (0)。 However, I need the abundance (count), not the presence-absence (1-0).但是,我需要丰度(计数),而不是存在-不存在(1-0)。

I hope I'm not too late to answer your question.我希望我回答你的问题不会太晚。

If you type ?create.matrix in the RStudio console you can get the documentation about the function.如果您在 RStudio 控制台中键入?create.matrix ,您可以获得有关该函数的文档。 There it's said that you can actually use your original raw data to make an abundance matrix, but you have to include a couple of extra arguments( tax.name to indicate the species names, locality to indicate the sites, abund.col to indicate the count of each species and abund = TRUE just to let the function know we're working with abundance data).据说您实际上可以使用原始原始数据来制作丰度矩阵,但是您必须包含一些额外的参数( tax.name表示物种名称, locality表示地点, abund.col表示每个物种的计数和abund = TRUE只是为了让函数知道我们正在处理丰度数据)。

In your case...在你的情况...

df <- create.matrix(x, tax.name = "Species",
   locality = "Site",
   abund.col = "Count",
   abund = TRUE)

Where x is the name of your data.frame containg those three columns (Site, Species and Count).其中x是包含这三列(站点、物种和计数)的 data.frame 的名称。 However, this will create a data.frame where the rows are the species and the columns are the sites.但是,这将创建一个 data.frame,其中行是物种,列是站点。 If you want to transpose it, just use the function t(df) to change the species to the columns and the sites to the rows!如果你想转置它,只需使用函数t(df)将物种更改为列,将站点更改为行!

Hope this was helpful, also you can check the rest of the documentation right here .希望这对您有所帮助,您也可以在此处查看其余文档。

Also it is important to know that the output of the function create.matrix is not a data.frame, so you might want to convert it to a data frame using as.data.frame while doing the transposition...同样重要的是要知道函数 create.matrix 的输出不是 data.frame,因此您可能希望在进行转置时使用as.data.frame将其转换为数据帧...

abundance.matrix <- as.data.frame(t(df))
import pandas as pd
import numpy as np

df1=pd.DataFrame([14],index=['A'],columns=['Abies'])
df2=pd.DataFrame([1],index=['B'],columns=['Alnus'])
df3=pd.DataFrame([1],index=['C'],columns=['Pinus'])
df4=pd.DataFrame([2],index=['C'],columns=['Artem'])

A_B=pd.merge(df1, df2, how='outer', left_index=True, right_index=True)
C_C=pd.merge(df3, df4, how='outer', left_index=True, right_index=True)
new=pd.merge(A_B, C_C, how='outer', left_index=True, right_index=True)

new=new.replace(np.nan,0)
new=new.astype(int)
new
  • Import two libraries: numpy and pandas导入两个库: numpypandas

  • Create data frame for each count and make index as 'Site' and column as 'Species'为每个计数创建数据框,并将索引设为“站点”,将列设为“物种”

  • Merge all those data frames合并所有这些数据框

  • Replace NaN values with 0用 0 替换 NaN 值

  • Convert 'float' to 'int'将“浮动”转换为“整数”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM