r-xml2在转换为数据帧时处理不清楚的错误



我正在尝试将xml文件转换为数据帧,而对于某些元素,它运行良好,而对于其他元素则不然。我不知道为什么。

下面是XML的一个简单版本:

<?xml version="1.0" encoding="UTF-8"?>
<clinical_study rank="6838">
<arm_group>
<arm_group_label>Arm I (Lozenge placebo)</arm_group_label>
<arm_group_type>Placebo Comparator</arm_group_type>
<description>Patients receive lozenge placebo PO QID.</description>
</arm_group>
<arm_group>
<arm_group_label>Arm II (LBR lozenge)</arm_group_label>
<arm_group_type>Experimental</arm_group_type>
<description>Patients receive lyophilized black raspberries lozenge PO (8gms/day)</description>
</arm_group>
<arm_group>
<arm_group_label>Arm III (Saliva Substitute placebo)</arm_group_label>
<arm_group_type>Placebo Comparator</arm_group_type>
<description>Patients receive Saliva Substitute placebo PO QID.</description>
</arm_group>
<arm_group>
<arm_group_label>Arm IV (LBR Saliva Substitute)</arm_group_label>
<arm_group_type>Experimental</arm_group_type>
<description>Patients receive lyophilized black raspberries Saliva Substitute PO (8gms/day).</description>
</arm_group>
<condition_browse>
<!-- CAUTION:  The following MeSH terms are assigned with an imperfect algorithm            -->
<mesh_term>Carcinoma</mesh_term>
<mesh_term>Carcinoma, Squamous Cell</mesh_term>
<mesh_term>Laryngeal Diseases</mesh_term>
<mesh_term>Laryngeal Neoplasms</mesh_term>
<mesh_term>Oropharyngeal Neoplasms</mesh_term>
<mesh_term>Carcinoma, Verrucous</mesh_term>
<mesh_term>Nasopharyngeal Neoplasms</mesh_term>
<mesh_term>Salivary Gland Neoplasms</mesh_term>
<mesh_term>Paranasal Sinus Neoplasms</mesh_term>
<mesh_term>Head and Neck Neoplasms</mesh_term>
<mesh_term>Neoplasms, Unknown Primary</mesh_term>
<mesh_term>Mouth Neoplasms</mesh_term>
<mesh_term>Hypopharyngeal Neoplasms</mesh_term>
<mesh_term>Tongue Neoplasms</mesh_term>
<mesh_term>Lip Neoplasms</mesh_term>
<mesh_term>Carcinoma in Situ</mesh_term>
</condition_browse>
<!-- Results have not yet been posted for this study                                          -->
</clinical_study>

我正在使用的代码(工作代码(:

library(XML)
library(dplyr)
library(xml2)
# read group
outc <- xml_find_all(xml, "//arm_group") %>% as_list() %>% dplyr::bind_rows() %>% as.data.frame()

还有一段不起作用的代码:

test1 <- xml_find_all(xml, "//condition_browse") %>% as_list() %>% dplyr::bind_rows() %>% as.data.frame()

第二段代码生成了一个包含1行的数据集,而不是预期的多行数据帧。

我无法确定错误是来自我的xml2语法、xpath语法还是仅仅来自xml数据。

你能支持吗?

condition_browse下的所有节点都标记为:"mesh_term"。bind_rows正在合并命名相似的行,从而导致只保存最后一个行
尝试使用

temp <-  xml_find_all(xml, "//condition_browse") %>% as_list() %>% unlist() 
#convert into data frame
test1 <-data.frame(names=names(temp), value=temp)

这将提供稍微不同的格式,但应该为您的其他分析提供一个良好的开端。

最新更新