r - 提取列表中的列表以在 R 中构建数据框
问题描述
我正在尝试使用我的个人数据从 LibraryThing api 构建一个包含书籍 ID、标题、作者、评级、收藏、开始和结束日期的数据框。我能够相当容易地获得一个嵌套列表,并且我已经弄清楚如何使用除日期之外的所有内容构建一个数据框(也许不是最好的方式,但它确实有效)。我的问题是日期。我正在使用的列表通常有 20 个元素,但只有当我在我的帐户中向书中添加日期时,它才会添加 startfinishdates 元素。这导致了两个问题:
- 如果它总是在那里,我可以像其他所有东西一样提取它,并且它大部分时间都会有 NA,我可以使用 cbind 让它与其他信息正确对齐
- 当我使用名称提取它并获得一个元素较少的对象时,我没有办法将它加入到其他所有内容中(它没有书籍 ID)
最终,我想构建这个数据框和一个答案,告诉我如何提取书籍 ID 并将其与每个 startfinishdate 相关联,这样我就可以加入书籍 ID 是可以接受的。我只是将它添加到我拥有的代码中。
我也乐于从跳跃中学习更好的方法并重新设计整个事物,因为我在 R 中没有过多地使用列表,而且我经过多次试验和错误后整理出来的东西。不过我确实想使用 R,因为最终我将使用它为我的网站创建一个 R Markdown 页面(例如,显示书籍完成日期的图)。
您可以运行下面的代码并获取数据(无需 api 密钥)。
library(jsonlite)
library(tidyverse)
library(assertr)
data<-fromJSON("http://www.librarything.com/api_getdata.php?userid=cau83&key=392812157&max=450&showCollections=1&responseType=json&showDates=1")
books.lst<-data$books
#create df from json
create.df<-function(item){
df<-map_df(.x=books.lst,~.x[[item]])
df2 <- t(df)
return(df2)
}
ids<-create.df(1)
titles<-create.df(2)
ratings<-create.df(12)
authors<-create.df(4)
#need to get the book id when i build the date df's
startdates.df<-map_df(.x=books.lst,~.x$startfinishdates) %>% select(started_stamp,started_date)
finishdates.df<-map_df(.x=books.lst,~.x$startfinishdates) %>% select(finished_stamp,finished_date)
collections.df<-map_df(.x=books.lst,~.x$collections)
#from assertr: will create a vector of same length as df with all values concatenated
collections.v<-col_concat(collections.df, sep = ", ")
#assemble df
books.df<-as.data.frame(cbind(ids,titles,authors,ratings,collections.v))
names(books.df)<-c("ID","Title","Author","Rating","Collections")
books.df<-books.df %>% mutate(ID=as.character(ID),Title=as.character(Title),Author=as.character(Author),
Rating=as.character(Rating),Collections=as.character(Collections))
解决方案
这种方法在tidyverse
元包之外。使用 base-R,您可以使用以下代码使其工作。
Map
将用户定义的函数应用于data$books
参数中提供的每个元素,并为您的data.frame
. Reduce
将获取所有单独的数据帧并将它们合并(或减少)到单个 data.frame booksdf
。
library(jsonlite)
data<-fromJSON("http://www.librarything.com/api_getdata.php?userid=cau83&key=392812157&max=450&showCollections=1&responseType=json&showDates=1")
booksdf=Reduce(function(x,y){rbind(x,y)},
Map(function(x){
lenofelements = length(x)
if(lenofelements>20){
if(!is.null(x$startfinishdates$started_date)){
started_date = x$startfinishdates$started_date
}else{
started_date=NA
}
if(!is.null(x$startfinishdates$started_stamp)){
started_stamp = x$startfinishdates$started_date
}else{
started_stamp=NA
}
if(!is.null(x$startfinishdates$finished_date)){
finished_date = x$startfinishdates$finished_date
}else{
finished_date=NA
}
if(!is.null(x$startfinishdates$finished_stamp)){
finished_stamp = x$startfinishdates$finished_stamp
}else{
finished_stamp=NA
}
}else{
started_stamp = NA
started_date = NA
finished_stamp = NA
finished_date = NA
}
book_id = x$book_id
title = x$title
author = x$author_fl
rating = x$rating
collections = paste(unlist(x$collections),collapse = ",")
return(data.frame(ID=book_id,Title=title,Author=author,Rating=rating,
Collections=collections,Started_date=started_date,Started_stamp=started_stamp,
Finished_date=finished_date,Finished_stamp=finished_stamp))
},data$books))
推荐阅读
- reactjs - 导航器只能包含 'Screen'、'Group' 或 'React.Fragment' 作为其直接子级
- ajax - Google translate API - 使用 ajax 的多个查询
- android - Android 播放 url 重定向/要求他们登录,例如内部测试邀请
- python-3.x - Python - 加载多个带有特定列的多个工作表的excel文件
- java - 如何将毫秒转换为“mm:ss:ms”格式?
- apache - 我可以在 Drupal Apache 服务器上运行 Dash 应用程序(Flask 应用程序)吗?
- javascript - 如何在 JavaScript 的构造函数中使用类变量?
- python - 检测到 Dataframe 中的特定字符后删除特定行(噪声)
- linux - DB2 创建数据库需要很长时间
- visual-studio-code - Microsoft VS Code jupyter 笔记本