Skip to Content
Data Mashups in R
book

Data Mashups in R

by Xiao-Yi Li, Jeremy Leipzig
June 2009
Beginner to intermediate
29 pages
42m
English
O'Reilly Media, Inc.
Content preview from Data Mashups in R

The Many Ways to Philly (Latitude)

Using Data Structures

Using the indexing list notation from R we can get to the nodes we need

  > lat<-xmlResult[['doc']][['ResultSet']][['Result']][['Latitude']][['text']]
  > long<-xmlResult[['doc']][['ResultSet']][['Result']][['Longitude']][['text']]
  > lat
  39.951405 

looks good, but if we examine this further

  > str(lat)
  list()
  - attr(*, "class")= chr [1:5] "XMLTextNode" "XMLNode" "RXMLAbstractNode" "XML...

Although it has a decent display value this variable still considers itself an XMLNode and contains no index to obtain raw leaf value we want—the descriptor just says list() instead of something we can use (like $lat). We’re not quite there yet.

Using Helper Methods

Fortunately, the XML package offers a method to access the leaf value: xmlValue

  > lat<-xmlValue(xmlResult[['doc']][['ResultSet']][['Result']][['Latitude']]) 
  > str(lat)
  chr "39.951405" 

Using Internal Class Methods

There are usually multiple ways to accomplish the same task in R. Another means to get to this our character lat/long data is to use the “value” method provided by the node itself

  > lat<-xmlResult[['doc']][['ResultSet']][['Result']][['Latitude']][['text']]$value

If we were really clever we would have understood that XML doc class provided us with useful methods all the way down! Try neurotically holding down the tab key after typing

  > lat<-xmlResult$ (now hold down the tab key)
  xmlResult$doc  xmlResult$dtd  
  (let's go with doc and start looking for more methods using $)
 > lat<-xmlResult$doc$ ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Data Mashups in R

Data Mashups in R

Jeremy Leipzig, Xiao-Yi Li
Graphing Data with R

Graphing Data with R

John Jay Hilfiger

Publisher Resources

ISBN: 9780596805302Errata