Like lapply xmlapply returns a list like sapply

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: > It contains the content. •  We can drill down further into the list: > oneplant[['COMMON']]! <COMMON>Bloodroot</COMMON> ! •  Note that this doesn’t remove the markup. To do this, use the func8on xmlValue! > xmlValue(oneplant[['COMMON']])! [1] "Bloodroot"! > xmlValue(oneplant[['BOTANICAL']])! [1] "Sanguinaria canadensis"! •  There are special XML versions of lapply and sapply, named xmlApply xmlSApply. Each takes an XMLNode object as its primary argument. They iterate over the node’s children nodes, invoking the given func8on. •  Like lapply, xmlApply returns a list. Like sapply, xmlSApply returns a simpler data structure if possible. To create the data frame: 1)  What do you want to operate on (iterate over)? 2) What do you want to produce? In our plants example, we can use xmlSApply to extract the common names of all the plants. > common = xmlSApply(catalog, function(x){ + xmlValue(x[['COMMON']])}) > head(common) PLANT PLANT PLANT "Bloodroot" "Columbine" "Marsh Marigold" PLANT PLANT PLANT "Cowslip" "Dutchman's-Breeches" "Ginger, Wild" The elements of the root node are all plant nodes, like oneplant. •  Using the same strategy, we can create a full data frame with all variables. > > > + > getvar = function(x, var) xmlValue(x[[var]]) varNames = names(catalog[[1]]) res = lapply(varNames, function(var){ xmlSApply(catalog,getvar,var)}) plants = data.frame(res) What is this command doing? XPath What is XPath •  Extrac8on tool designed for loca8ng conten...
View Full Document

This document was uploaded on 02/16/2014 for the course STATISTICS 3026 at Columbia.

Ask a homework question - tutors are online