【发布时间】:2016-07-28 20:39:37
【问题描述】:
XML 数据
<HealthData locale="en_US">
<ExportDate value="2016-06-02 14:05:23 -0400"/>
<Me HKCharacteristicTypeIdentifierDateOfBirth="" HKCharacteristicTypeIdentifierBiologicalSex="HKBiologicalSexNotSet" HKCharacteristicTypeIdentifierBloodType="HKBloodTypeNotSet" HKCharacteristicTypeIdentifierFitzpatrickSkinType="HKFitzpatrickSkinTypeNotSet"/>
<Record type="HKQuantityTypeIdentifierStepCount" sourceName="Ryan Praskievicz iPhone" unit="count" creationDate="2014-10-02 08:30:17 -0400" startDate="2014-09-24 15:07:06 -0400" endDate="2014-09-24 15:07:11 -0400" value="7"/>
<Record type="HKQuantityTypeIdentifierStepCount" sourceName="Ryan Praskievicz iPhone" unit="count" creationDate="2014-10-02 08:30:17 -0400" startDate="2014-09-24 15:12:13 -0400" endDate="2014-09-24 15:12:18 -0400" value="15"/>
<Record type="HKQuantityTypeIdentifierStepCount" sourceName="Ryan Praskievicz iPhone" unit="count" creationDate="2014-10-02 08:30:17 -0400" startDate="2014-09-24 15:17:16 -0400" endDate="2014-09-24 15:17:21 -0400" value="20"/>
</HealthData>
R 代码
> library(XML)
> doc="\\pathtoXMLfile"
> list <-xpathApply(doc, "//HealthData/Record", xmlAttrs)
> df <- do.call(rbind.data.frame, list)
> str(df)
我正在尝试获取上面显示的 XML 数据样本并将其加载到 R 中的数据框中,其中每个记录的名称即类型、源名称、单位、结束日期、值作为列标题和每个记录值,即计数, 2014-09-24 15:07:11 -0400, 7 作为数据框中每一行的值。
当df <- do.call(rbind.data.frame, list) 关闭时,它看起来也绑定了列标题的所有值。如果你 View(df) 或 str(df) 你会明白我的意思。如何使用记录变量名称作为列标题名称?
谢谢, 瑞恩
【问题讨论】:
标签: r xml xml-parsing rbind