Roger.Bivand at nhh.no
Wed Aug 19 10:55:04 CEST 2015
On Wed, 19 Aug 2015, Metastate Metastate wrote:
> 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.
Thanks for a reproducible example. Don't use merge() unless you know
already that the objects are conformable:
> xx <- maptools::readShapePoly(system.file("shapes/sids.shp",
> package="maptools"), IDvar="FIPSNO")
> 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")
xxx <- spCbind(xx, yy4a)
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.
> On Tue, Aug 18, 2015 at 7:31 PM, Metastate Metastate <metastate at gmail.com>
>> Cool. Let me take a look. Thank you very much, Loïc.
>> On Tue, Aug 18, 2015 at 3:31 AM, Loïc Dutrieux <loic.dutrieux at wur.nl>
>>> Have a look at merge in the sp package.
>>> 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
>>>> 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
>>>> *multiple-year* weather features within each county?
>>>> Thank you very much for any kindly help.
>>>> [[alternative HTML version deleted]]
>>>> R-sig-Geo mailing list
>>>> R-sig-Geo at r-project.org
>>> R-sig-Geo mailing list
>>> R-sig-Geo at r-project.org
> [[alternative HTML version deleted]]
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
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