【问题标题】:How to create several data sets, one for each value iterated in a for loop?如何创建多个数据集,一个用于在 for 循环中迭代的每个值?
【发布时间】:2020-10-02 14:09:38
【问题描述】:

所以我正在使用“rgbif”包挖掘属于“actinopterygii”组的巴西鱼类出现的数据,但由于该组的出现次数如此之多,我无法在一次。 通过这两行代码,我们可以看到出现了 323200 次:

#install.packages("rgbif")
library(rgbif)
actinopterygii<-name_backbone(name="Actinopterygii")
occ_count(taxonKey = actinopterygii$classKey,country="BR")

问题是检索出现次数的函数每次检索最多出现 2000 次:

actinopterygii_oc<-occ_search(taxonKey = actinopterygii$classKey,country="BR",limit=2000,start=0)
#the start argument refers to the index of the record we are starting at so we can page through all the results

我基本上是在尝试避免重复此行 60 次并将起始值每次更改 2000,因此我尝试使用 for 循环,但它不起作用。我为出现次数创建了一个间隔,一次执行 2000 到 2000 次检索:

interval<-seq(from = 0, to = 323200, by = 2000)
for (value in interval){
  actinopterygii_oc<-occ_search(taxonKey = actinopterygii$classKey,country="BR",limit=2000,start=value)
}

问题是这段代码每次只修改一组数据。那么,有什么方法可以创建多组数据,一个用于区间中的每个值,同时循环遍历区间中的值?

对于这可能造成的混乱,我深表歉意,但我无法更好地表达它,提前感谢您的任何回答

【问题讨论】:

    标签: r


    【解决方案1】:

    您可以像这样在列表中聚合数据集:

    interval<-seq(from = 0, to = 323200, by = 2000)
    actinopterygii_oc <- list()
    
    for (i in 1:length(interval)){
      value <- interval[i]
      actinopterygii_oc[[i]] <- occ_search(taxonKey = actinopterygii$classKey,country="BR",limit=2000,start=value)
    }
    

    然后使用例如dplyr::bind_rows(actinopterygii_oc) 组合它们。

    【讨论】:

      【解决方案2】:

      尝试使用 purrr::map 一次取回包含 2,000 行的小标题列表,而不是使用 for 循环。我可能不必告诉你这将花费很长时间

      interval <- seq(from = 1, to = 323200, by = 2000)
      
      list_of_tibbles <- 
         purrr::map(interval, 
                    ~ occ_search(taxonKey = actinopterygii$classKey,
                                 country="BR",
                                 limit=2000, 
                                 start= .x)
                    )
      

      我不打算获取你所有的数据,但你会得到类似的输出

      [[1]]
      Records found [323200] 
      Records returned [2000] 
      No. unique hierarchies [661] 
      No. media records [2000] 
      No. facets [0] 
      Args [limit=2000, offset=1, taxonKey=204, country=BR, fields=all] 
      # A tibble: 2,000 x 145
         key   scientificName decimalLatitude decimalLongitude issues datasetKey publishingOrgKey
         <chr> <chr>                    <dbl>            <dbl> <chr>  <chr>      <chr>           
       1 2550… Chaetodipteru…           -7.91            -34.8 cdrou… 50c9509d-… 28eb1a3f-1c15-4…
       2 2550… Myrichthys oc…           -7.90            -34.8 cdrou… 50c9509d-… 28eb1a3f-1c15-4…
       3 2550… Mugil curema …           -7.91            -34.8 cdrou… 50c9509d-… 28eb1a3f-1c15-4…
       4 2550… Centropomus u…           -7.91            -34.8 cdrou… 50c9509d-… 28eb1a3f-1c15-4…
       5 2550… Trachinotus c…           -7.91            -34.8 cdrou… 50c9509d-… 28eb1a3f-1c15-4…
       6 2550… Phractocephal…           -3.18            -59.9 cdrou… 50c9509d-… 28eb1a3f-1c15-4…
       7 2550… Diapterus aur…           -7.91            -34.8 cdrou… 50c9509d-… 28eb1a3f-1c15-4…
       8 2550… Chaetodipteru…           -7.91            -34.8 cdrou… 50c9509d-… 28eb1a3f-1c15-4…
       9 2550… Centropomus u…           -7.91            -34.8 cdrou… 50c9509d-… 28eb1a3f-1c15-4…
      10 2550… Calophysus ma…           -3.18            -59.9 cdrou… 50c9509d-… 28eb1a3f-1c15-4…
      # … with 1,990 more rows, and 138 more variables: installationKey <chr>,
      #   publishingCountry <chr>, protocol <chr>, lastCrawled <chr>, lastParsed <chr>,
      #   crawlId <int>, extensions <chr>, basisOfRecord <chr>, occurrenceStatus <chr>,
      #   taxonKey <int>, kingdomKey <int>, phylumKey <int>, classKey <int>, orderKey <int>,
      #   familyKey <int>, genusKey <int>, speciesKey <int>, acceptedTaxonKey <int>,
      #   acceptedScientificName <chr>, kingdom <chr>, phylum <chr>, order <chr>, family <chr>,
      #   genus <chr>, species <chr>, genericName <chr>, specificEpithet <chr>, taxonRank <chr>,
      #   taxonomicStatus <chr>, dateIdentified <chr>, coordinateUncertaintyInMeters <dbl>,
      #   stateProvince <chr>, year <int>, month <int>, day <int>, eventDate <chr>,
      #   modified <chr>, lastInterpreted <chr>, references <chr>, license <chr>,
      #   identifiers <chr>, facts <chr>, relations <chr>, gadm.level0.gid <chr>,
      #   gadm.level0.name <chr>, gadm.level1.gid <chr>, gadm.level1.name <chr>,
      #   gadm.level2.gid <chr>, gadm.level2.name <chr>, gadm.level3.gid <chr>,
      #   gadm.level3.name <chr>, geodeticDatum <chr>, class <chr>, countryCode <chr>,
      #   recordedByIDs <chr>, identifiedByIDs <chr>, country <chr>, rightsHolder <chr>,
      #   identifier <chr>, http...unknown.org.nick <chr>, verbatimEventDate <chr>,
      #   datasetName <chr>, collectionCode <chr>, gbifID <chr>, verbatimLocality <chr>,
      #   occurrenceID <chr>, taxonID <chr>, catalogNumber <chr>, recordedBy <chr>,
      #   http...unknown.org.occurrenceDetails <chr>, institutionCode <chr>, rights <chr>,
      #   eventTime <chr>, identifiedBy <chr>, identificationID <chr>, name <chr>,
      #   occurrenceRemarks <chr>, gadm <chr>, informationWithheld <chr>,
      #   recordedByIDs.type <chr>, recordedByIDs.value <chr>, individualCount <int>,
      #   establishmentMeans <chr>, continent <chr>, organismQuantityType <chr>, habitat <chr>,
      #   http...rs.tdwg.org.dwc.terms.organismQuantity <chr>,
      #   georeferenceVerificationStatus <chr>, verbatimSRS <chr>, verbatimCoordinateSystem <chr>,
      #   county <chr>, locality <chr>, taxonRemarks <chr>, preparations <chr>, disposition <chr>,
      #   vernacularName <chr>, organismName <chr>, fieldNotes <chr>, originalNameUsage <chr>,
      #   http...rs.tdwg.org.dwc.terms.organismQuantityType <chr>, …
      

      您会注意到,您返回的不仅有数据,还有其他元数据。将所有的 data 粘合到一个大数据框另一个 map

      glued_data <- 
         purrr::map(list_of_tibbles, "data") %>% 
         bind_rows()
      
      dim(glued_data)
      [1] 10000   162
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-08-31
        • 2019-03-01
        • 2023-04-08
        • 1970-01-01
        • 2019-12-15
        • 1970-01-01
        • 1970-01-01
        • 2022-01-18
        相关资源
        最近更新 更多