我需要将表格Person.Person(在XML中)中的属性Demographics从SQL Server数据库AdventureWorks2014导出到R中进行一些统计分析。我想使用XML包,但看起来R在导出时并不能将我的文件识别为XML。
有人知道如何使用某些软件包直接从SQL转到R吗?没有将数据导出到CSV之前?
我在SQL中的一个元组看起来像这样:
<IndividualSurvey xmlns="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/IndividualSurvey">
<TotalPurchaseYTD>-31</TotalPurchaseYTD>
<DateFirstPurchase>2003-11-01Z</DateFirstPurchase>
<BirthDate>1962-07-26Z</BirthDate>
<MaritalStatus>S</MaritalStatus>
<YearlyIncome>0-25000</YearlyIncome>
<Gender>M</Gender>
<TotalChildren>1</TotalChildren>
<NumberChildrenAtHome>0</NumberChildrenAtHome>
<Education>Graduate Degree</Education>
<Occupation>Manual</Occupation>
<HomeOwnerFlag>0</HomeOwnerFlag>
<NumberCarsOwned>0</NumberCarsOwned>
<CommuteDistance>0-1 Miles</CommuteDistance>
</IndividualSurvey>
我想在R中有不同的属性:
TotalPurchaseYTD DateFirstPurchase BirthDate MaritalStatus YearlyIncome...
-31 2003-11-01 1962-07-26 S 0-25000 ....
答案 0 :(得分:1)
我们可以使用:
library(xml2)
df <- read_xml(x) %>% as_list %>% sapply(rbind) %>% as.data.frame
df
# TotalPurchaseYTD DateFirstPurchase BirthDate MaritalStatus YearlyIncome Gender TotalChildren NumberChildrenAtHome Education Occupation HomeOwnerFlag NumberCarsOwned CommuteDistance
# 1 -31 2003-11-01Z 1962-07-26Z S 0-25000 M 1 0 1 Graduate Degree Manual 0 0 0-1 Miles