如何将选定的子列表合并到R中的一个数据框中?

时间:2017-04-21 13:05:52

标签: r

这是我正在使用的3种植物物种的子集

spp <- c("Shorea robusta Gaertn.", "Schima wallichii (DC.) Korth.", 
"Terminalia alata Roth")

我想从GBIF中检索这些物种的发生数据,我正在使用“rgbif :: occ_data”函数

library(rgbif)

test.rgbif <- occ_data(scientificName = spp, hasCoordinate=T, 
 geometry = "POLYGON ((78.0074716700000010 24.5425829399999991, 
  78.0074716700000010 32.2509633700000009, 
  90.2318328799999989 32.2509633700000009, 
  90.2318328799999989 24.5425829399999991, 
  78.0074716700000010 24.5425829399999991))")

test.rgbif # List of 3 each with List of 2


output <- bind_rows(test.rgbif$`Shorea robusta Gaertn.`$data,
 test.rgbif$`Schima wallichii (DC.) Korth.`$data,
 test.rgbif$`Terminalia alata Roth`$data); 

dim(output) 
#20      78

如上所述,我想将3个列表的第二个子列表合并为一个数据帧。但是,当我与几个物种合作时,以这种方式做这将是不切实际的。那么,有人可以建议我如何通过选择3个列表的第二个子列表而不是现在完成他们的全名来完成此操作?

2 个答案:

答案 0 :(得分:1)

您可以尝试do.call(rbind, ...)例程:

do.call(rbind, lapply(test.rgbif[spp], function(df) df$data))

如果data.frame具有不同的列或列顺序,则dplyr::bind_rows更适合,因为它将按列名匹配:

do.call(bind_rows, lapply(test.rgbif[spp], function(df) df$data))

答案 1 :(得分:0)

我无法尝试*但以下应该这样做

library(magrittr)
library(purrr)
library(data.table)

test.rgbif %>% 
  # turns list of length 3 with 2 elements each into a 
  # list of 2 with 3 elements each
  purrr::transpose %>%
  # take the data list-entry
  .$data %>% 
  # and rbind its 3 elements
  rbindlist(idcol = "Species")

*:我收到以下错误

> test.rgbif <- ...
Error in check_wkt(geometry) : 
  bad lexical cast: source type value could not be interpreted as target at '
' in 'polygon ((78.0074716700000010 24.5425829399999991, 
                       78.0074716700000010 32.25'