[R] XML: Reading transition matrices into R

stefan.duke at gmail.com stefan.duke at gmail.com
Fri Nov 13 09:55:28 CET 2009


Thank you so much!!!!
You really helped me a lot!
Best from R'dam,
Stefan



On Fri, Nov 13, 2009 at 2:14 AM, Duncan Temple Lang
<duncan at wald.ucdavis.edu> wrote:
>
>
> stefan.duke at gmail.com wrote:
>> Hello,
>> thanks a lot. This is a form which I can work with in R.
>>
>> Another question, which I hope is not a bridge too far: Can I write
>> the R matrix which your code created back into the xml format (i.e.
>> with the same tags and structure) from which it came and hence feed it
>> back to the original software?
>
>
> trans = apply(xx, 1, function(x) {
>                       tr = newXMLNode("transition")
>                       mapply(newXMLNode, names(x), x, MoreArgs = list(parent = tr))
>                       tr
>                     })
> top = newXMLNode("transitionmatrix", .children = trans)
> saveXML(top, "newTransition.xml")
>
>
>
>
>>
>> Best,
>> Stefan
>>
>>
>> On Thu, Nov 12, 2009 at 3:17 PM, Duncan Temple Lang
>> <duncan at wald.ucdavis.edu> wrote:
>>>
>>> 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</f
> ro
>>> 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</f
> ro
>>> 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><pe
> rc
>>> 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</fr
> om
>>>> !
>>>>  <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.
>>
>> ______________________________________________
>> 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