我正在尝试将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数据。
可以请你支持吗?
答案 0 :(得分:1)
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)
这将提供稍有不同的格式,但应该为其余的分析提供一个良好的开端。