How to convert a spatial dataframe back to normal dataframe?

user2760 picture user2760 · Feb 17, 2012 · Viewed 21.2k times · Source

This is a basic question but unfortunately I could not find the relevant command elsewhere.

Is there a way i can convert a Spatial Points Dataframe to an ordinary dataframe in R.

e.g. if the ordinary dataframe is df with Lat, Lon as location coordinates I can obtain a spatial df as:

coordinates (df)= ~Lat + Lon

How is the reverse possible or is it even possible ?

Answer

Josh O'Brien picture Josh O'Brien · Feb 17, 2012

as.data.frame() does just what you are looking for:

library(sp)
# Construct a SpatialPointsDataFrame
data(meuse)
xy <- meuse[1:2]
df <- meuse[-1:-2]
SPDF <- SpatialPointsDataFrame(coords=xy, data=df)

# And then convert it (back) to a data.frame
DF <- as.data.frame(SPDF)