[R-sig-Geo] Error in SpatialPointsDataFrame(..., match.ID=TRUE)?

Roger Bivand Roger.Bivand at nhh.no
Thu Oct 10 10:19:10 CEST 2013


On Thu, 10 Oct 2013, Jonathan Greenberg wrote:

> 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)) {

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

>
> --j
>
> On Wed, Oct 9, 2013 at 5:28 PM, Jonathan Greenberg <jgrn at illinois.edu> wrote:
>> 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!
>>
>> --
>> Jonathan A. Greenberg, PhD
>> Assistant Professor
>> Global Environmental Analysis and Remote Sensing (GEARS) Laboratory
>> Department of Geography and Geographic Information Science
>> University of Illinois at Urbana-Champaign
>> 259 Computing Applications Building, MC-150
>> 605 East Springfield Avenue
>> Champaign, IL  61820-6371
>> Phone: 217-300-1924
>> http://www.geog.illinois.edu/~jgrn/
>> AIM: jgrn307, MSN: jgrn307 at hotmail.com, Gchat: jgrn307, Skype: jgrn3007
>
>
>
>

-- 
Roger Bivand
Department of Economics, NHH Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no



More information about the R-sig-Geo mailing list