[R-sig-Geo] R equivalent of join between two data frames

Juan Tomas Sayago juantomas.sayago at gmail.com
Tue Nov 1 14:47:18 CET 2011


Dear Erin I use this one that I found on raymond florax's website for
spatial econometrics.
It works great with points as well as with polygons.
PS: I will attach the R file if you need I can send you the files he used
to you on another email.
Hope it helps you
Juan Tomás
# import your shapefile using readShapePoly
shape <- readShapePoly("columbus.shp",IDvar="POLYID")

# read-in the table containing the data the you want to add to your
shapefile
  # make sure that the table contains a column with a unique identifier
corresponding to IDvar
tab <- read.table("join_example.txt",header=TRUE)

# creates a vector of all polygon IDs
  #note that it is the "character" version of POLYID (in this example)
shpID <- sapply(slot(shape,"polygons"),function(x) slot(x,"ID"))

# make the ID variable from the data table into class "character"
tabID <- as.character(tab$ID)

# match the two unique identifiers
  # match() needs both its arguments to be of the same class. Here:
"character"

  #### if you want to keep all polygons
    tab <- tab[match(shpID,tabID),]
      # matches both IDs and replaces nomatch values by NA
    rownames(tab) <- shpID
    shape.mod <- spCbind(shape,tab)

  #### if you want to delete polygons with no matching data in the table
    #tab <- tab[shpID%in%tabID,]
    #shape.mod <- shape.mod[shpID%in%tabID,]
      # matches and removes the non-matching locations from the table and
the original shapefile
    #shape.mod <- spCbind(shape.mod,tab)



On Tue, Nov 1, 2011 at 2:33 AM, Hodgess, Erin <HodgessE at uhd.edu> wrote:

> Dear R Sig Geo People:
>
> Is there an equivalent of "join" from ArcGIS 10 in R, please?
>
> I've been experimenting with "match" but the results between the two are
> not the same.
>
> thanks,
> Sincerely,
> Erin
>
>
> Erin M. Hodgess, PhD
> Associate Professor
> Department of Computer and Mathematical Sciences
> University of Houston - Downtown
> mailto: hodgesse at uhd.edu
>
>
>        [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>



-- 
Juan Tomás Sayago Gómez
Graduate Research Assistant
West Virginia University
886 Chestnut Ridge Road, Room 520
P.O. Box 6825
Morgantown, WV 26506-6825
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20111101/f87c005d/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: add_data.r
Type: application/octet-stream
Size: 2937 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20111101/f87c005d/attachment.obj>


More information about the R-sig-Geo mailing list