[R] XML: Reading transition matrices into R

Duncan Temple Lang duncan at wald.ucdavis.edu
Thu Nov 12 15:17:28 CET 2009



stefan.duke at gmail.com wrote:
> Hello,
> from a software I have the following output in xml (see below):
> It is a series of matrices, for each age one. I have 3 categories
> (might vary in the application), hence, 3x3 matrices where each
> element gives the probability of transition from i to j. I would like
> read this data into R (preferably in a list, where each list element
> is one of the age specific matrices) and - after altering the values
> in R - write it back into the file.  I know that there is an xml
> package in R with which I have already struggled, but I have to admit
> my understanding is too limited. Maybe somebody had a similar problem
> or know the code of the top of his or her head.

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.



> 
> Any help appreciated.
> 
> Thanks and best,
> Stefan
> 
> 
> 
> 
> 
> <?xml version="1.0" encoding="UTF-8" standalone="no"?><transitionmatrix>
> <transition><age>0</age><sex>0</sex><from>1</from><to>1</to><percent>99.99999</percent></transition><transition><age>0</age><sex>0</sex><from>1</from><to>2</to><percent>0.0</percent></transition><transition><age>0</age><sex>0</sex><from>1</from><to>3</to><percent>0.0</percent></transition><transition><age>0</age><sex>0</sex><from>2</from><to>1</to><percent>0.0</percent></transition><transition><age>0</age><sex>0</sex><from>2</from><to>2</to><percent>99.999985</percent></transition><transition><age>0</age><sex>0</sex><from>2</from><to>3</to><percent>0.0</percent></transition><transition><age>0</age><sex>0</sex><from>3</from><to>1</to><percent>0.0</percent></transition><transition><age>0</age><sex>0</sex><from>3</from><to>2</to><percent>0.0</percent></transition><transition><age>0</age><sex>0</sex><from>3</from><to>3</to><percent>99.999985</percent></transition><transition><age>0</age><sex>1</sex><from>1</from><to>1</to><percent>100.0</percent></transition><transition><age>0<
/!
>  age><sex>1</sex><from>1</from><to>2</to><percent>0.0</percent></transition><transition><age>0</age><sex>1</sex><from>1</from><to>3</to><percent>0.0</percent></transition><transition><age>0</age><sex>1</sex><from>2</from><to>1</to><percent>0.0</percent></transition><transition><age>0</age><sex>1</sex><from>2</from><to>2</to><percent>100.0</percent></transition><transition><age>0</age><sex>1</sex><from>2</from><to>3</to><percent>0.0</percent></transition><transition><age>0</age><sex>1</sex><from>3</from><to>1</to><percent>0.0</percent></transition><transition><age>0</age><sex>1</sex><from>3</from><to>2</to><percent>0.0</percent></transition><transition><age>0</age><sex>1</sex><from>3</from><to>3</to><percent>100.0</percent></transition><transition><age>1</age><sex>0</sex><from>1</from><to>1</to><percent>99.99999</percent></transition><transition><age>1</age><sex>0</sex><from>1</from><to>2</to><percent>0.0</percent></transition><transition><age>1</age><sex>0</sex><from>1</fro
m!
>  ><to>3</to><percent>0.0</percent></transition><transition><age>1</age>
> <sex>0</sex><from>2</from><to>1</to><percent>0.0</percent></transition><transition><age>1</age><sex>0</sex><from>2</from><to>2</to><percent>99.999985</percent></transition><transition><age>1</age><sex>0</sex><from>2</from><to>3</to><percent>0.0</percent></transition><transition><age>1</age><sex>0</sex><from>3</from><to>1</to><percent>0.0</percent></transition><transition><age>1</age><sex>0</sex><from>3</from><to>2</to><percent>0.0</percent></transition><transition><age>1</age><sex>0</sex><from>3</from><to>3</to><percent>99.999985</percent></transition><transition><age>1</age><sex>1</sex><from>1</from><to>1</to><percent>100.0</percent></transition><transition><age>1</age><sex>1</sex><from>1</from><to>2</to><percent>0.0</percent></transition><transition><age>1</age><sex>1</sex><from>1</from><to>3</to><percent>0.0</percent></transition><transition><age>1</age><sex>1</sex><from>2</from><to>1</to><percent>0.0</percent></transition><transition><age>1</age><sex>1</sex><from>2</fro
m!
>  ><to>2</to><percent>100.0</percent></transition><transition><age>1</age><sex>1</sex><from>2</from><to>3</to><percent>0.0</percent></transition><transition><age>1</age><sex>1</sex><from>3</from><to>1</to><percent>0.0</percent></transition><transition><age>1</age><sex>1</sex><from>3</from><to>2</to><percent>0.0</percent></transition><transition><age>1</age><sex>1</sex><from>3</from><to>3</to><percent>100.0</percent></transition><transition><age>2</age><sex>0</sex><from>1</from><to>1</to><percent>99.35205</percent></transition><transition><age>2</age><sex>0</sex><from>1</from><to>2</to><percent>0.6479474</percent></transition><transition><age>2</age><sex>0</sex><from>1</from><to>3</to><percent>0.0</percent></transition><transition><age>2</age><sex>0</sex><from>2</from><to>1</to><percent>0.0</percent></transition><transition><age>2</age><sex>0</sex><from>2</from><to>2</to><percent>97.101456</percent></transition><transition><age>2</age><sex>0</sex><from>2</from><to>3</to><perc
e!
>  nt>2.8985496</percent></transition><transition><age>2</age><sex>0</sex
>> <from>3</from><to>1</to><percent>0.0</percent></transition><transition><age>2</age><sex>0</sex><from>3</from><to>2</to><percent>0.0</percent></transition><transition><age>2</age><sex>0</sex><from>3</from><to>3</to><percent>100.0</percent></transition><transition><age>2</age><sex>1</sex><from>1</from><to>1</to><percent>97.82136</percent></transition><transition><age>2</age><sex>1</sex><from>1</from><to>2</to><percent>2.1786473</percent></transition><transition><age>2</age><sex>1</sex><from>1</from><to>3</to><percent>0.0</percent></transition><transition><age>2</age><sex>1</sex><from>2</from><to>1</to><percent>0.0</percent></transition><transition><age>2</age><sex>1</sex><from>2</from><to>2</to><percent>93.33336</percent></transition><transition><age>2</age><sex>1</sex><from>2</from><to>3</to><percent>6.6666408</percent></transition><transition><age>2</age><sex>1</sex><from>3</from><to>1</to><percent>0.0</percent></transition><transition><age>2</age><sex>1</sex><from>3</from
>!
>  <to>2</to><percent>0.0</percent></transition><transition><age>2</age><sex>1</sex><from>3</from><to>3</to><percent>100.0</percent></transition>
> </transitionmatrix>
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list