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 process a a regular data frame with spatial attributes, follow these steps:

  1. Load the sp package:
> library(sp) 
  1. Read the data:
> nj <- read.csv("nj-wages.csv") 
> class(nj) 
[1] "data.frame" 
  1. Convert nj to a spatial object:
> coordinates(nj) <- c("Long", "Lat") 
> class(nj)  
 
[1] "SpatialPointsDataFrame" 
attr(,"package") 
[1] "sp" 
  1. Plot the points:
plot(nj)
  1. Convert the points to Lines and plot them:
nj.lines <- SpatialLines(list(Lines(list(Line(coordinates(nj))),     "linenj"))) 
> plot(nj.lines) 
 

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