在R中具有嵌套数据帧的无效列表(unest list with nested data frames in R)

编程入门 行业动态 更新时间:2024-10-27 08:33:40
在R中具有嵌套数据帧的无效列表(unest list with nested data frames in R)

我正在解析foursquare 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

等等

谢谢!

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!

最满意答案

假设您的大型列表称为mylist 。 然后你可以迭代mylist[[1]]并提取相关的列:

do.call(rbind, lapply(mylist[[1]], `[`, c("id", "name")))

或者使用rbind.pages函数:

jsonlite::rbind.pages(mylist[[1]])[c("id", "name")]

两者都会给你

# id name # 1 4bf58dd8d48988d129951735 Train Station # 2 4bf58dd8d48988d1fe931735 Bus Station

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

更多推荐

本文发布于:2023-07-20 11:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1198673.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   数据   列表   unest   frames

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!