[R-sig-Geo] spgpc

Roger Bivand Roger.Bivand at nhh.no
Thu May 8 21:47:48 CEST 2008


On Thu, 8 May 2008, Mihai Nica wrote:

> Greetings:
>
> I used to merge a SpatialPolygonsDataFrame with another dataframe with the following code (thanks to Dr. Bivand):
> ############################################################################################
> library(maptools)
> library(spgpc)

The functionality from the former off-CRAN spgpc is now in maptools.

> county <- readShapePoly("contUScty2000.shp", ID="GISJOIN") # put shapefile in SpatialPolygonsDataFrame
> ok <- county[county$STATENAM =="Oklahoma",] # subset with "[" method
> dat=data.frame(read.delim(file="okdata.txt", header = TRUE, sep = "\t", quote="\"", dec=".",na.strings = "NA"))
> dat=dat[dat$Line.code=="10",]; str(dat)
> ok.df<- merge(as(ok, "data.frame"), dat, by.x="NHGISCTY",by.y="FIPS", all.x=TRUE, sort=FALSE)
> row.names(ok.df)=ok.df$SP_ID ######## - $SP_ID seems not to work either
> ok <- SpatialPolygonsDataFrame(as(ok.df, "SpatialPolygons"), data=ok.df)
                                     ^^^^
                           should be ok, I think 
>

ok is a SpatialPolygonsDataFrame, ok.df is a data frame.

Beyond that, look at ?"spChFIDs-methods" for ways of setting the 
SpatialPolygons IDs. Please just step though things line by line and be 
sure you understand what each line does. There is no silver bullet here, 
and certainly no magic, the above is a bit muddled and pretty illegible. 
Please do use spaces around operators and after commas. If you had, you 
would have seen yourself that the objects were not the ones you thought 
they were.

Hope this helps,

Roger

> Error in as(ok.df, "SpatialPolygons") :
>  no method or default for coercing "data.frame" to "SpatialPolygons" #this is the outcome...
>
>
> ###########################################################################################
>
> Unfortunatelly the last two lines are obsolete, (it seems that wrapping  "spgpc" with other packages might be the culprit) and I am lost again. I read the last discussions on the list, but was unable to solve the issue.
>
> Please be so kind as to offer a hint.
>
> Thanks,
>
> mihai
>
>
>      ____________________________________________________________________________________
>
> [[elided Yahoo spam]]
> 	[[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, 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