问题描述
我正在研究四quare API中的一些数据,我的结果中有一部分看起来如下,一个带有嵌套数据框的大列表:
List of 1 $ :List of 26 ..$ :'data.frame': 1 obs. of 6 variables: .. ..$ id : chr "4bf58dd8d48988d129951735" .. ..$ name : chr "Train Station" .. ..$ pluralName: chr "Train Stations" .. ..$ shortName : chr "Train Station" .. ..$ icon :'data.frame': 1 obs. of 2 variables: .. .. ..$ prefix: chr "https://ss3.4sqi.net/img/categories_v2/travel/trainstation_" .. .. ..$ suffix: chr ".png" .. ..$ primary : logi TRUE ..$ :'data.frame': 1 obs. of 6 variables: .. ..$ id : chr "4bf58dd8d48988d1fe931735" .. ..$ name : chr "Bus Station" .. ..$ pluralName: chr "Bus Stations" .. ..$ shortName : chr "Bus Station" .. ..$ icon :'data.frame': 1 obs. of 2 variables: .. .. ..$ prefix: chr "https://ss3.4sqi.net/img/categories_v2/travel/busstation_" .. .. ..$ suffix: chr ".png" .. ..$ primary : logi TRUE ..$ :'data.frame': 1 obs. of 6 variables:
我正在尝试针对某些元素的这些数据框,以便我可以将它们转移到我拥有的预先存在的文件中.最终,我希望最终结果看起来像以下内容:
$id $name 4bf58dd8d48988d129951735 train station 4bf58dd8d48988d1fe931735 bus station
等.
谢谢!
推荐答案
假设您的大列表称为mylist.然后,您可以通过mylist[[1]]迭代并提取相关列:
do.call(rbind, lapply(mylist[[1]], `[`, c("id", "name")))
或使用jsonlite的rbind.pages函数:
jsonlite::rbind.pages(mylist[[1]])[c("id", "name")]
这两个都会给你
# id name # 1 4bf58dd8d48988d129951735 Train Station # 2 4bf58dd8d48988d1fe931735 Bus Station
问题描述
I'm working on parsing out some data from the foursquare api and I have one portion of the results that look like the following, a large list with nested dataframes:
List of 1 $ :List of 26 ..$ :'data.frame': 1 obs. of 6 variables: .. ..$ id : chr "4bf58dd8d48988d129951735" .. ..$ name : chr "Train Station" .. ..$ pluralName: chr "Train Stations" .. ..$ shortName : chr "Train Station" .. ..$ icon :'data.frame': 1 obs. of 2 variables: .. .. ..$ prefix: chr "https://ss3.4sqi.net/img/categories_v2/travel/trainstation_" .. .. ..$ suffix: chr ".png" .. ..$ primary : logi TRUE ..$ :'data.frame': 1 obs. of 6 variables: .. ..$ id : chr "4bf58dd8d48988d1fe931735" .. ..$ name : chr "Bus Station" .. ..$ pluralName: chr "Bus Stations" .. ..$ shortName : chr "Bus Station" .. ..$ icon :'data.frame': 1 obs. of 2 variables: .. .. ..$ prefix: chr "https://ss3.4sqi.net/img/categories_v2/travel/busstation_" .. .. ..$ suffix: chr ".png" .. ..$ primary : logi TRUE ..$ :'data.frame': 1 obs. of 6 variables:
I'm trying yo unnest these dataframe for certain elements so that i can cbind them to a pre-existing file I have. Ultimately I would like the end result to look something like the following:
$id $name 4bf58dd8d48988d129951735 train station 4bf58dd8d48988d1fe931735 bus station
etc.
Thanks!
推荐答案
Suppose your large list is called mylist. Then you can either iterate through mylist[[1]] and extract the relevant columns:
do.call(rbind, lapply(mylist[[1]], `[`, c("id", "name")))
or use the rbind.pages function from jsonlite:
jsonlite::rbind.pages(mylist[[1]])[c("id", "name")]
both of which will give you
# id name # 1 4bf58dd8d48988d129951735 Train Station # 2 4bf58dd8d48988d1fe931735 Bus Station