R 如何将xml2::xml_find_all()元素转换为TIBLE格式
我有以下代码:R 如何将xml2::xml_find_all()元素转换为TIBLE格式,r,xml,tibble,R,Xml,Tibble,我有以下代码: 库(tidyverse) 库(xml2) xmlfile[1]\n[2]\n[3]\n[4]\n[5]\n[6]\n[7]\n[8]\n[9]\n[10]\n[12]\n1。-选择包含所需信息的节点(引文): 2.-获取标题(这是一个值): 3.-获取另一个信息(其为属性): 长度4: df2 <- as.data.frame(do.call(rbind, ll[unlist(lapply(ll, length)) == 4])) str(df2) 'data.frame'
库(tidyverse)
库(xml2)
xmlfile[1]\n[2]\n[3]\n[4]\n[5]\n[6]\n[7]\n[8]\n[9]\n[10]\n[12]\n1。-选择包含所需信息的节点(引文
):
2.-获取标题(这是一个值):
3.-获取另一个信息(其为属性):
长度4:
df2 <- as.data.frame(do.call(rbind, ll[unlist(lapply(ll, length)) == 4]))
str(df2)
'data.frame': 3 obs. of 4 variables:
$ Title: Factor w/ 3 levels "A novel gene, located on human chromosome 21q11.",..: 1 3 2
$ type : Factor w/ 1 level "submission": 1 1 1
$ date : Factor w/ 3 levels "2000-01","2002-06",..: 1 2 3
$ db : Factor w/ 2 levels "EMBL/GenBank/DDBJ databases",..: 1 1 2
df21.-选择包含所需信息的节点(引文
):
2.-获取标题(这是一个值):
3.-获取另一个信息(其为属性):
长度4:
df2 <- as.data.frame(do.call(rbind, ll[unlist(lapply(ll, length)) == 4]))
str(df2)
'data.frame': 3 obs. of 4 variables:
$ Title: Factor w/ 3 levels "A novel gene, located on human chromosome 21q11.",..: 1 3 2
$ type : Factor w/ 1 level "submission": 1 1 1
$ date : Factor w/ 3 levels "2000-01","2002-06",..: 1 2 3
$ db : Factor w/ 2 levels "EMBL/GenBank/DDBJ databases",..: 1 1 2
df2尝试此解决方法purrr::map\u df(~as.list(.))
。就你而言
tmp%
xml2::xml\u find\u all(//d1:reference)
关键%
xml2::xml_attrs()%>%
purrr::map_df(~as.list()
参考百分比
xml2::xml_children()%>%
xml2::xml_attrs()%>%
purrr::map_df(~as.list()
然后将它们与dplyr::bind_cols(key,ref)
合并。希望这能有所帮助。试试这个解决方法purrr::map\u df(~as.list()
。就你而言
tmp%
xml2::xml\u find\u all(//d1:reference)
关键%
xml2::xml_attrs()%>%
purrr::map_df(~as.list()
参考百分比
xml2::xml_children()%>%
xml2::xml_attrs()%>%
purrr::map_df(~as.list()
然后将它们与dplyr::bind_cols(key,ref)
合并。希望这有帮助。谢谢。如何获取每个参考键的Pubmed id,例如键7的
您需要添加try(xml\u attr(xml\u child(xml\u child(x)[1],3),“id”)
,我已经编辑了帖子。谢谢。如何获取每个参考键的Pubmed id,例如键7的
您需要添加try(xml\u attr(xml\u child(x)[1],3),“id”)
,我已经编辑了这篇文章。
ll <- lapply(nodeset,
function(x) c(Title = xml_text(xml_children(x)[1]),
xml_attrs(xml_children(x)[1])[[1]]))
df1 <- as.data.frame(do.call(rbind, ll[unlist(lapply(ll, length)) == 7]))
str(df1)
'data.frame': 9 obs. of 7 variables:
$ Title : Factor w/ 9 levels "Complete sequencing and characterization of 21,243 full-length human cDNAs.",..: 2 1 6 8 3 7 4 5 9
$ type : Factor w/ 1 level "journal article": 1 1 1 1 1 1 1 1 1
$ date : Factor w/ 6 levels "2000","2001",..: 2 3 1 3 2 3 4 5 6
$ name : Factor w/ 9 levels "Biochem. Biophys. Res. Commun.",..: 9 7 8 4 1 5 2 3 6
$ volume: Factor w/ 9 levels "10","12","14",..: 4 7 8 3 6 5 1 9 2
$ first : Factor w/ 9 levels "137","17","2121",..: 8 6 5 3 1 9 7 2 4
$ last : Factor w/ 9 levels "141","17","2127",..: 8 6 5 3 1 9 7 2 4
df2 <- as.data.frame(do.call(rbind, ll[unlist(lapply(ll, length)) == 4]))
str(df2)
'data.frame': 3 obs. of 4 variables:
$ Title: Factor w/ 3 levels "A novel gene, located on human chromosome 21q11.",..: 1 3 2
$ type : Factor w/ 1 level "submission": 1 1 1
$ date : Factor w/ 3 levels "2000-01","2002-06",..: 1 2 3
$ db : Factor w/ 2 levels "EMBL/GenBank/DDBJ databases",..: 1 1 2
ll <- lapply(nodeset,
function(x) c(Title = xml_text(xml_children(x)[1]),
xml_attrs(xml_children(x)[1])[[1]],
id = try(xml_attr(xml_child(xml_children(x)[1], 3), "id"))))
df1 <- as.data.frame(do.call(rbind, ll[unlist(lapply(ll, length)) == 8]))
str(df1)
'data.frame': 9 obs. of 8 variables:
$ Title : Factor w/ 9 levels "Complete sequencing and characterization of 21,243 full-length human cDNAs.",..: 2 1 6 8 3 7 4 5 9
$ type : Factor w/ 1 level "journal article": 1 1 1 1 1 1 1 1 1
$ date : Factor w/ 6 levels "2000","2001",..: 2 3 1 3 2 3 4 5 6
$ name : Factor w/ 9 levels "Biochem. Biophys. Res. Commun.",..: 9 7 8 4 1 5 2 3 6
$ volume: Factor w/ 9 levels "10","12","14",..: 4 7 8 3 6 5 1 9 2
$ first : Factor w/ 9 levels "137","17","2121",..: 8 6 5 3 1 9 7 2 4
$ last : Factor w/ 9 levels "141","17","2127",..: 8 6 5 3 1 9 7 2 4
$ id : Factor w/ 9 levels "10830953","11536050",..: 2 4 1 6 3 5 7 8 9