Skip to content

Error in SpatialPointsDataFrame(...,match.ID=TRUE)?

3 messages · Jonathan Greenberg, Roger Bivand

#
I am trying to figure out if I'm missing something very obvious, or if
I came across a potentially serious bug.  I'm using examples from
"Applied Spatial Data Analysis with R" to create a
SpatialPointsDataFrame from the (attached) text file which is from
http://www.asdar-book.org/data2ed.php?chapter=1

The issue I'm having is in the creation of
"CRAN_SpatialPointsDataFrame2" which uses a re-ordered dataframe, but
continues to have the same rownames.  You'll see from the final step
that the coordinates are no longer "in sync" (joined properly) to the
data frame.

Ideas?

###

input_file <- "CRAN051001a.txt" # Attached to this message or from
http://www.asdar-book.org/data2ed.php?chapter=1

# import the file
CRAN_df <- read.table(cran_file,header=TRUE)

# Create a coordinate matrix:
CRAN_mat <- cbind(CRAN_df$long, CRAN_df$lat)

# Add rownames:
row.names(CRAN_mat) <- 1:nrow(CRAN_mat)

# Define a CRS:
CRAN_CRS <- CRS("+proj=longlat +ellps=WGS84")

# Create SpatialPointsDataFrame:
CRAN_SpatialPointsDataFrame1 <-
SpatialPointsDataFrame(coords=CRAN_mat,data=CRAN_df,
proj4string=CRAN_CRS,match.ID=TRUE)

# SpatialPoints coords match the dataframe long and lat:
CRAN_SpatialPointsDataFrame1[1,]

# Reorder the dataframe:
s <- sample(nrow(CRAN_df))
CRAN_df_reordered <- CRAN_df[s,]
rownames(CRAN_df_reordered)
rownames(CRAN_df_reordered["1",])

# Now join using match.ID=TRUE:
CRAN_SpatialPointsDataFrame2 <-
SpatialPointsDataFrame(coords=CRAN_mat,data=CRAN_df_reordered,
proj4string=CRAN_CRS,match.ID=TRUE)
CRAN_SpatialPointsDataFrame2["1",]
# Not matching!
#
As a follow-up, it appears that the requirement in
SpatialPointsDataFrame is not that the rownames not be NULL, but that
it be a character.  The line that appears to be causing the issue is:

if (match.ID && is.character(attr(data, "row.names"))) {


Note that in the above example:

is.character(attr(CRAN_df_reordered, "row.names"))
# is FALSE, but:
is.character(rownames(CRAN_df_reordered))
# is TRUE

It appears that data.frames have numeric row names that are allowed to
be re-ordered just like a character row name, so it seems like this
error could be fixed by modding SpatialPointsDataFrame to be:

if (match.ID && is.character(rownames(data)) {

--j
On Wed, Oct 9, 2013 at 5:28 PM, Jonathan Greenberg <jgrn at illinois.edu> wrote:

  
    
#
On Thu, 10 Oct 2013, Jonathan Greenberg wrote:

            
OK. Only use row.names() unless the object is a matrix, rownames() are by 
intention for matrices only.

This is handled in R-forge SVN revision 1470 of September 18. Always state 
your sessionInfo() when reporting a problem, until sp 1.0-13, the code on 
p. 34 of the book worked correctly, and should work correctly if you 
install the development version:

install.packages("sp", repos="http://R-Forge.R-project.org")

For details see:

https://r-forge.r-project.org/scm/viewvc.php/pkg/sp/R/SpatialPointsDataFrame-methods.R?root=rspatial&r1=1461&r2=1470

I've added an erratum on the book website to explain.

Roger