[R-sig-Geo] Spatialpolygonsdataframe

Roger Bivand Roger.Bivand at nhh.no
Wed Aug 19 10:55:04 CEST 2015


On Wed, 19 Aug 2015, Metastate Metastate wrote:

> Hi
>
> I tried to use sp merge to join the spatialpolygonddataframe (xx) and an
> dataframe (yy4), which have 2 observations for each spatial ID. But it did
> not allow with an error message as below:
> Error in .local(x, y, ...) :  'y' has multiple records for one or more
> 'by.y' key(s)
>
> I also tried the mapmerge in eeptools but failed.
>
> Any suggestion? Thank you very much.
>
> Meta
>

Thanks for a reproducible example. Don't use merge() unless you know 
already that the objects are conformable:

>
> library("eeptools")
> library("sp")
> xx <- maptools::readShapePoly(system.file("shapes/sids.shp",
> package="maptools")[1], IDvar="FIPSNO")
> yy<-as(xx,"data.frame")
> yy$newvar<-sample(0:100,nrow(yy),replace=TRUE)
> yy<-subset(yy,select=c("FIPS","newvar"))
> yy3<-yy
> yy3$newvar<-10*yy3$newvar
> yy4<-data.frame(rbind(yy,yy3))
> newpoly<-merge(xx,yy4,sort=FALSE, by.x="FIPS",
>                  by.y="FIPS" ,all.y=TRUE)

Look at str(yy4) and dim(xx) - they are not conformable. Add a "time" 
variable and use reshape() to get to a conformable object:

yy4$time <- c(rep(1, 100), rep(2, 100))
yy4a <- reshape(yy4, direction="wide", idvar="FIPS", timevar="time")

str(yy4a)

and combine:

library(maptools)
xxx <- spCbind(xx, yy4a)
summary(xxx)
spplot(xxx, c("newvar.1", "newvar.2"))

Note that the SIDS dataset is already in thw "wide" format with data from 
1974 and 1979 as columns. Read up on the long and wide formats, and maybe 
look at the documentation of the spacetime package.

Roger

>
>
> On Tue, Aug 18, 2015 at 7:31 PM, Metastate Metastate <metastate at gmail.com>
> wrote:
>
>> Cool. Let me take a look. Thank you very much, Loïc.
>>
>> Meta
>>
>>
>>
>> On Tue, Aug 18, 2015 at 3:31 AM, Loïc Dutrieux <loic.dutrieux at wur.nl>
>> wrote:
>>
>>> Hi,
>>>
>>> Have a look at merge in the sp package.
>>> ?sp::merge
>>>
>>> Cheers,
>>> Loïc
>>>
>>> On 08/18/2015 04:12 AM, Metastate Metastate wrote:
>>>
>>>> Hi all,
>>>>
>>>> I am starting to learn spatial analysis using R. Do anyone know if the
>>>> attribute data in a spatialpolygonsdataframe can contain multiple
>>>> observations within the same polygon? To be specific, I have a
>>>> spatialpolygonsdataframe that contain USA county polygons and the
>>>> relevant
>>>> attribute data contains the relevant state,county and FIPs.  And I also
>>>> have another data-frame that contains weather features by year by county
>>>> (For example there are yearly average temperature for each county) . How
>>>> can I join the spatialpolygondataframe with the weather data so that the
>>>> resulted attribute table in the joint spatialpolygonsdataframe can
>>>> contain
>>>> *multiple-year* weather features within each county?
>>>>
>>>> Thank you very much for any kindly help.
>>>>
>>>> Meta
>>>>
>>>>         [[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
>>>>
>>>>
>>> _______________________________________________
>>> R-sig-Geo mailing list
>>> R-sig-Geo at r-project.org
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>>
>>
>>
>
> 	[[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

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


More information about the R-sig-Geo mailing list