[R-sig-Geo] Error in SpatialPointsDataFrame(..., match.ID=TRUE)?
Jonathan Greenberg
jgrn at illinois.edu
Thu Oct 10 00:52:13 CEST 2013
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:
> 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
--
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
More information about the R-sig-Geo
mailing list