O'Reilly logo

R Data Analysis Cookbook - Second Edition by Kuntal Ganguly

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

To import ESRI shape files to R, follow these steps:

  1. Load the sp and rgdal packages:
> library(sp) 
> library(rgdal) 
  1. Read the ESRI file of countries:
> countries_sp <- readOGR(".", "ne_50m_admin_0_countries") 
 
OGR data source with driver: ESRI Shapefile  
Source: ".", layer: "ne_50m_admin_0_countries" 
with 241 features and 63 fields 
Feature type: wkbPolygon with 2 dimensions 
 
> class(countries_sp) 
 
[1] "SpatialPolygonsDataFrame" 
attr(,"package") 
[1] "sp"
  1. Read the ESRI file of airports:
> airports_sp <- readOGR(".", "ne_50m_airports") OGR data source with driver: ESRI Shapefile Source: ".", layer: "ne_50m_airports" with 281 features and 10 fields Feature type: wkbPoint with 2 dimensions > class(airports_sp) [1] "SpatialPointsDataFrame" ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required