R's XML example not working - xmlToDataFrame
With the next code:
library(XML)
f = system.file("exampleData", "size.xml", packag开发者_高级运维e = "XML")
doc = xmlParse(f)
z = xmlToDataFrame(f, colClasses = list("integer", "integer", "numeric"))
y = xmlToDataFrame(nodes = xmlChildren(xmlRoot(doc)[["size"]]))
browser()
I get these results :
Browse[1]> z
age sex number
1 0 0 500
2 0 1 300
3 1 0 200
4 1 1 400
5 10 0 NA
Browse[1]> y
text
1 0
2 0
3 500
This is from package help and following the manual, what can be wrong? The results for y are not the same for z, why?
Thanks
You didn't copy the entire example. I get the same error when I just copy those two lines from the example, but no error when I copy the entire example from ?xmlToDataFrame .
(I have done this twice and I have not figured out which of the arguments is not being passed properly, and so my working hypothesis is that some connections is not established properly.)
EDIT; (now an entirely different question) Because the root is different than the whole file:
> xmlRoot(doc)[["size"]]
<size>
<age>0</age>
<sex>0</sex>
<number>500</number>
</size>
EDIT2: You do not need to specify the colClasses:
> xmlToDataFrame(f)
age sex number
1 0 0 500
2 0 1 300
3 1 0 200
4 1 1 400
5 10 0 <NA>
And if you want the read.table behavior of guessing at the class from the first five lines of data then you can do a write/read.table operation:
> write.table(xmlToDataFrame(f), file="test.txt", quote=FALSE)
> read.table(file="test.txt")
> str(read.table(file="test.txt"))
'data.frame': 5 obs. of 3 variables:
$ age : int 0 0 1 1 10
$ sex : int 0 1 0 1 0
$ number: int 500 300 200 400 NA
精彩评论