Creating a Data Frame from an XML
On Tue, Jan 22, 2013 at 3:11 PM, Adam Gabbert <adamjgabbert at gmail.com> wrote:
Hello,
I'm attempting to read information from an XML into a data frame in R using
the "XML" package. I am unable to get the data into a data frame as I would
like. I have some sample code below.
*XML Code:*
Header...
Data I want in a data frame:
<data>
<row BRAND="GMC" NUM="1" YEAR="1999" VALUE="10000" />
<row BRAND="FORD" NUM="1" YEAR="2000" VALUE="12000" />
<row BRAND="GMC" NUM="1" YEAR="2001" VALUE="12500" />
<row BRAND="FORD" NUM="1" YEAR="2002" VALUE="13000" />
<row BRAND="GMC" NUM="1" YEAR="2003" VALUE="14000" />
<row BRAND="FORD" NUM="1" YEAR="2004" VALUE="17000" />
<row BRAND="GMC" NUM="1" YEAR="2005" VALUE="15000" />
<row BRAND="GMC" NUM="1" YEAR="1967" VALUE="PRICLESS" />
<row BRAND="FORD" NUM="1" YEAR="2007" VALUE="17500" />
<row BRAND="GMC" NUM="1" YEAR="2008" VALUE="22000" />
</data>
*R Code:*
doc< -xmlInternalTreeParse ("Sample2.xml")
top <- xmlRoot (doc)
xmlName (top)
names (top)
art <- top [["row"]]
art
**
This will get a data frame of character columns
as.data.frame(t(xpathSApply(doc, "//row", xmlAttrs)), stringsAsFactors = FALSE)
BRAND NUM YEAR VALUE 1 GMC 1 1999 10000 2 FORD 1 2000 12000 3 GMC 1 2001 12500 4 FORD 1 2002 13000 5 GMC 1 2003 14000 6 FORD 1 2004 17000 7 GMC 1 2005 15000 8 GMC 1 1967 PRICLESS 9 FORD 1 2007 17500 10 GMC 1 2008 22000 -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com