r - XML标记内的变量到R数据框中的列
问题描述
尝试将 XML 文件转换为 R 中的数据框。但是,对于一个客户,XML 标记(节点)具有相同的名称两次。我希望数据框中的每个节点都位于单独的列中。
<Consumer ConsumerOID="123">
<BillingContact>
<PhoneInformation Type="Main">
<PhoneNumber>4758796523</PhoneNumber>
</PhoneInformation>
<PhoneInformation Type="Home">
<PhoneNumber>1234567890</PhoneNumber>
</PhoneInformation>
</BillingContact>
</Consumer>
<Consumer ConsumerOID="256">
<BillingContact>
<PhoneInformation Type="Main">
<PhoneNumber>9856321475</PhoneNumber>
</PhoneInformation>
</BillingContact>
</Consumer>
library(XML)
doc<-xmlParse("test.xml")
step1 <- xmlToDataFrame(nodes=getNodeSet(doc,"//OCADocument/Consumer/BillingContact/PhoneInformation"))
上面的代码只给了我电话号码,并且在数据框中的单个列中。这使得很难识别哪个电话号码属于哪个 consumerOID。
我想以如下所示的数据帧格式输出
"ConsumerID Phonenumber1 Phonenumber2"
" 123 4758796523 1234567890"
" 256 9856321475 NA"
解决方案
推荐阅读
- python - 合并两个数据框的更有效方法
- r - 使用 3 个不同的数据输出第 4 个数据帧
- python - 获取 matplotlib 图形轴类型
- ios - 适用于 iOS 的 Google Analytics(分析)SDK 是否具有 SSL 固定?
- python - 如何使用用户定义的函数执行 groupy by?
- python - 如何将预训练的 tensorflow pb 冻结图转换为可修改的 h5 keras 模型?
- python - 用于批量转换的 AWS SageMaker 部署
- r - 避免 raster::extract(rst,shp) 中的 for 循环
- python - OpenCV中LUT函数的解释?
- android - 未解决的参考:viewModels