我有一个尺寸为1042x64的Tibble_data.Frame.列是两栖动物科,行是该科中所有物种的名称.前5行2列如下所示:

> amphilist[1:5,1:2]
A tibble: 5 × 2
  `Allophrynidae_(1_genus;_3_species)` `Alsodidae_(3_genera;_26_species)`
  <chr>                                <chr>                             
1 Allophryne_relicta                   Alsodes_australis                 
2 Allophryne_resplendens               Alsodes_barrioi                   
3 Allophryne_ruthveni                  Alsodes_cantillanensis            
4 NA                                   Alsodes_coppingeri                
5 NA                                   Alsodes_gargola 

各科的种类不同,最大的有1,042种,最小的只有1种.除了唯一的一科有1,042种外,所有的列都充满了Nas,组成了1,042行.为了下一步的分析,我需要从每个家族中随机排序一定数量的物种,但是我一直在为我的所有列获取Nas,即使其中没有Nas的列也是如此.以下是我到目前为止所做的:

我创建了一个循环来获取物种丰富度(spcR),并将其保存在df"species_no"中.然后用一个"ifelse"子句输入我需要的物种数量并将其保存到df #中.

amphilist <- read_xlsx("amphilist.xlsx", col_names = TRUE)

families <- colnames(amphilist)
family_n <- ncol(amphilist)
spcR <- vector(length = family_n)

for(i in 1:length(families)) {
  families.i <- families[i]
  spcR[i] <- colSums(amphilist[,families.i] > 0, na.rm = TRUE)
}

species_no <- data.frame(families, spcR)
species_no$choose <- ifelse(species_no$spcR > 50, ceiling(species_no$spcR/10), 
                            ifelse(species_no$spcR >= 5 & species_no$spcR <= 50,
                                   5, species_no$spcR))

> species_no[1:3,]
                                        families spcR choose
1             Allophrynidae_(1_genus;_3_species)    3      3
2               Alsodidae_(3_genera;_26_species)   26      5
3 Alytidae_(2_subfamilies;_3_genera;_12_species)   12      5

从这里开始,我就被卡住了,并得到了错误Nas.我创建了一个具有我需要的元素数量的向量,但是我不能做出随机 Select 来工作.我想从每一列中获得由Choose_no向量定义的物种数量,而不考虑Nas#

choose_no <- species_no$choose
set.seed(43)
for(i in 1:length(families)) {
  families.i <- families[i]
  choose_no.i <- choose_no[i]
  rand_amphilist <- amphilist[sample(amphilist[,i], 
                                     size = choose_no.i), ]
}

有没有人能帮帮我?非常感谢!

推荐答案

# SETUP 
# load lib
library(tidyverse)

# example data
amphilist <- tribble(
  ~"Allophrynidae_(1_genus;_3_species)", ~"Alsodidae_(3_genera;_26_species)"
  , "Allophryne_relicta"                ,  "Alsodes_australis"                
  , "Allophryne_resplendens"            ,  "Alsodes_barrioi"                  
  , "Allophryne_ruthveni"               ,  "Alsodes_cantillanensis"          
  , NA                                ,  "Alsodes_coppingeri"               
  , NA                                ,  "Alsodes_gargola" )

# make it long; an 8 row frame( not 5x2 = 10)
amphilist_long <- amphilist |> pivot_longer(cols=everything(),
                                            names_to = "category",
                                            values_to = "entry") |> filter(!is.na(entry))


# a vector with the number of values to extract per category
(choose_no <- data.frame(
  category = c("Allophrynidae_(1_genus;_3_species)", "Alsodidae_(3_genera;_26_species)"),
  sampsize = 1:2
) |> deframe())

set.seed(42) # for reproducibility

# the main event
# for each sample size choice for category (choose_no)
# filter out irrelevant records then sample the desired amount
# `_dfr` variant to collate results rowwise to a dataframe
(samp_list <- imap_dfr(choose_no,
    \(x,y){
      slice_sample(filter(amphilist_long,
                          category==y),
                   n=x)
    })
)

R相关问答推荐

基于R中的GPS点用方向箭头替换点

根据列表中项目的名称多次合并数据框和列表

R等效于LABpascal(n,1)不同的列符号

带有叠加饼图系列的Highmap

获取列中值更改的行号

如何使下一个按钮只出现在Rshiny 的一段时间后?""

R-更新面内部的栅格值

我如何才能找到FAMILY=POISSON(LINK=&Q;LOG&Q;)中的模型预测指定值的日期?

打印XTS对象

使用带有OR条件的grepl过滤字符串

如何在PackageStatus()中列出&q;不可用的包&q;?

Geom_Hline将不会出现,而它以前出现了

创建列并对大型数据集中的特定条件进行成对比较的更高效程序

解析嵌套程度极高的地理数据

如何为混合模型输出绘制不同的线型?

ggplot斜体轴刻度标签中的单个字符-以前的帖子建议不工作

按镜像列值自定义行顺序

R中的Desolve:返回的导数数错误

将每晚的平均值与每晚的值进行比较,统计是否有效?

使用另一列中的增长率外推R(使用dplyr)