let's say, I have this xml file:
<?xml version="1.0" encoding="UTF-8" ?>
<TimeSeries>
<timeZone>1.0</timeZone>
<series>
<header/>
<event date="2009-09-30" time="10:00:00" value="0.0" flag="2"></event>
<event date="2009-09-30" time="10:15:00" value="0.0" flag="2"></event>
<event date="2009-09-30" time="10:30:00" value="0.0" flag="2"></event>
<event date="2009-09-30" time="10:45:00" value="0.0" flag="2"></event>
<event date="2009-09-30" time="11:00:00" value="0.0" flag="2"></event>
<event date="2009-09-30" time="11:15:00" value="0.0" flag="2"></event>
</series>
<series>
<header/>
<event date="2009-09-30" time="08:00:00" value="1.0" flag="2"></event>
<event date="2009-09-30" time="08:15:00" value="2.6" flag="2"></event>
<event date="2009-09-30" time="09:00:00" value="6.3" flag="2"></event>
<event date="2009-09-30" time="09:15:00" value="4.4" flag="2"></event>
<event date="2009-09-30" time="09:30:00" value="3.9" flag="2"></event>
<event date="2009-09-30" time="09:45:00" value="2.0" flag="2"></event>
<event date="2009-09-30" time="10:00:00" value="1.7" flag="2"></event>
<event date="2009-09-30" time="10:15:00" value="2.3" flag="2"></event>
<event date="2009-09-30" time="10:30:00" value="2.0" flag="2"></event>
</series>
<series>
<header/>
<event date="2009-09-30" time="10:00:00" value="0.0" flag="2"></event>
<event date="2009-09-30" time="10:15:00" value="0.0" flag="2"></event>
<event date="2009-09-30" time="10:30:00" value="0.0" flag="2"></event>
<event date="2009-09-30" time="10:45:00" value="0.0" flag="2"></event>
<event date="2009-09-30" time="11:00:00" value="0.0" flag="2"></event>
</series>
</TimeSeries>
and let's say I want to do something with its series elements and that I would like to put in practice the advice 'vectorize the vectorizable'... I import the XML library and do the following:
R> library("XML")
R> doc <- xmlTreeParse('/home/mario/Desktop/sample.xml')
R> TimeSeriesNode <- xmlRoot(doc)
R> seriesNodes <- xmlElementsByTagName(TimeSeriesNode, "series")
R> length(seriesNodes)
[1] 3
R> (function(x){length(xmlElementsByTagName(x[['series']], 'event'))}
+ )(seriesNodes)
[1] 6
R>
and I don't understand why I should only get the result of applying the function to the first element: I had expected three values, just as the length of seriesNodes, something like this:
R> mapply(length, seriesNodes)
series series series
7 10 6
oops! I already came with the answer: "use mapply
":
R> mapply(function(x){length(xmlElementsByTagName(x, 'event'))}, seriesNodes)
series series series
6 9 5
but then I see the following problem: the R-inferno tells me that I'm "loop-hiding", not "vectorizing"! can I avoid looping at all? ...