Skip to content
Prev 200023 / 398506 Next

XML: Reading transition matrices into R

stefan.duke at gmail.com wrote:
Hi Stefan

  There are many approaches for handling this. I assume that the primary
obstacle you are facing is extracting the values from the XML.  The following
will do that for you.
We start with the content in transition.xml (or in a string in R).
Since the XML is very shallow, i.e. not very hierarchical, and all
the information is in the transition nodes under the root, we can
use xmlToList().
This returns a list with an element for each <transition>
element, and such elements are character vectors containing the
values from age, sex, from, to, and percent.
So I've put these into a matrix and you are now back entirely
within R and can group the values by age and arrange them into
the individual transition matrices.


   doc = xmlParse("transition.xml")
   matrix(as.numeric(unlist(xmlToList(doc))), , 5, byrow = TRUE,
          dimnames = list(NULL, names(xmlRoot(doc)[[1]])))


 D.
/!
m!
m!
e!