Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将tibble与各个站点的物种数据转换为vegan::diversity()的数字矩阵_R_Matrix_Tibble_Vegan - Fatal编程技术网

将tibble与各个站点的物种数据转换为vegan::diversity()的数字矩阵

将tibble与各个站点的物种数据转换为vegan::diversity()的数字矩阵,r,matrix,tibble,vegan,R,Matrix,Tibble,Vegan,我有TIBLE格式的数据,看起来像这样,多了22行和7列: reprex[1:10,1:7] # A tibble: 10 x 7 # Groups: Point, Layer [10] Point Layer Lari_deci Quer_rope Pinu_sylv Betu_pend Sorb_aucu <chr> <chr> <chr> <chr> <chr> <chr>

我有TIBLE格式的数据,看起来像这样,多了22行和7列:

reprex[1:10,1:7]
# A tibble: 10 x 7
# Groups:   Point, Layer [10]
   Point Layer Lari_deci Quer_rope Pinu_sylv Betu_pend Sorb_aucu
   <chr> <chr> <chr>     <chr>     <chr>     <chr>     <chr>    
 1 P03   C     21        17        5         1         0        
 2 P03   U     0         0         0         0         3        
 3 P06   C     3         28        28        0         0        
 4 P07   C     0         3         20        1         1        
 5 P07   U     0         0         0         0         0        
 6 P08   C     0         16        21        0         0        
 7 P08   U     0         0         0         0         0        
 8 P10   C     0         17        44        1         0        
 9 P10   U     0         50        0         0         0        
10 P11   C     0         36        1         0         0  
我想计算每个点的辛普森多样性指数,分别考虑两个层次。由于我最初的尝试失败了,我决定将上述数据分成两部分,即C和U两个级别,然后删除图层列并将点转换为行名

结果,我得到了理论上只有数字的数据,所有剩余的列都有相应物种的计数。但实际上,情况似乎并非如此,这就是我的问题所在。然后,我使用as.matrix转换了data.frame,但仍然出现以下错误: 多样性错误,索引=simpson:输入数据必须是数字

但这会丢失行号,从而丢失点标识。虽然多样性现在起作用了,但它将所有的值都视为一个,并且只为整个数据计算一个多样性指数,而不是在我的原始数据格式中为每行计算一个值

为什么多样性不能处理这些数据?我能做些什么来解决这个问题? 是否有任何方法可以执行多样性,而不必将具有两个层级别的原始数据拆分为两个单独的矩阵?
在我看来,您的原始数据帧将数字列存储为chr。如果在执行拆分之前将其强制为数字,则应可以正常工作:

reprex_C <- reprex %>% 
  mutate(across(Lari_deci:Sorb_aucu,.fns = as.numeric)) %>%
  filter(Layer == "C") %>% ungroup %>% select(-2) %>% 
  column_to_rownames(var="Point") %>% as.matrix %>%
  vegan::diversity(index = "simpson")

恐怕我对多样性不够熟悉,无法回答你的第二个问题。

我尝试了你的建议,但我得到了与以前相同的错误:多样性错误,index=simpson:输入数据必须是数字这很奇怪,因为它对我有效。我看到你提到有7个额外的栏目。你是否调整了第2行的代码,我在其中加入了Lari_deci:Sorb_aucu,以包含你正在处理的第一个和最后一个数字列?哦,基本!很抱歉,我错过了那一步,但现在它可以工作了。非常感谢。素食主义者应该是tibble盲,tibble应该像真正的蓝色数据帧一样工作。如果失败,请提交错误报告。这里真正的问题似乎是数据中没有数字,只有文本。tibbles的一个优点是,通过tibbles,您可以获得有关变量类型的信息,如果您看到了这些信息,您就不应该期望任何数值方法能够工作。
reprex_C <- reprex %>% filter(Layer == "C") %>% ungroup %>% select(-2) %>% 
  column_to_rownames(var="Point") %>% as.matrix %>% 
  diversity(index = "simpson")
# I would have a similar 'reprex_U' object for Layer == "U".
as.numeric(reprex_C[,1:14])
reprex_C <- reprex %>% 
  mutate(across(Lari_deci:Sorb_aucu,.fns = as.numeric)) %>%
  filter(Layer == "C") %>% ungroup %>% select(-2) %>% 
  column_to_rownames(var="Point") %>% as.matrix %>%
  vegan::diversity(index = "simpson")