Here is reproducible example, #1 fails because what I suspect is a bug in merge, but #2 works. Robert
library(maptools) data(wrld_simpl) # create some data to merge with m = sort(unique(wrld_si...@data$region)) m = cbind(m, 1:length(m)) colnames(m) = c('REGION', 'v') # merge wrld_si...@data = merge(wrld_si...@data, m, by='REGION', all.x=TRUE, sort=FALSE) # but this does not work: col = terrain.colors(nrow(m)) plot(wrld_simpl, col=col[wrld_simpl$v]) # he data.frame is rearanged; despite "sort=FALSE" # this looks like a bug in merge to me. Anyone, am I wrong? Therefore, I would do library(maptools) data(wrld_simpl) # get the data.frame out of the SPGDF; to simplify code d = wrld_si...@data # add a sort key d$sort = 1:nrow(d) # create some data to merge with m = sort(unique(d$REGION)) m = cbind(m, 1:length(m)) colnames(m) = c('REGION', 'v') # merge x = merge(d, m, by='REGION', all.x=TRUE) # sort x = x[order(x$sort),] # put the data back wrld_si...@data = x # now it works col = terrain.colors(nrow(m)) plot(wrld_simpl, col=col[wrld_simpl$v]) On Wed, Jan 27, 2010 at 6:59 PM, Zia Ahmed <z...@cornell.edu> wrote: > Hi, Roberts, > In my last mail, I mentioned it works fine, but when I try write shape file > after merging it shows error. I think I need to define spatial object or > data frame after merging. I do not know how to do this. Thanks again. > Zia > >> poly.data1 = merge(poly.data, data.table, by="MUKEY" ,all.x = TRUE) >> names(poly.data1) # polygoan > [1] "MUKEY" "AREASYMBOL" "SPATIALVER" "MUSYM" "Sand" [6] > "Silt" "Clay" "pH" "CEC" "EC" [11] "SAR" > "CaCO3" "SOM" "BD" "AWC" [16] "KSAT" > "Kf" "Kw" "LL" "LEP" >>polyShp <- "D:/test/mapunit_poly.shp" > writeOGR(poly.data1, dsn=polyShp, >> layer="mapunit_poly", driver="ESRI Shapefile") > Error in writeOGR(poly.data1, dsn = polyShp, layer = "mapunit_poly", driver > = "ESRI Shapefile") : > obj of wrong class > > Zia Ahmed wrote: >> >> Thank you so much Roberts! it works. >> >> Zia >> >> Robert J. Hijmans wrote: >>> >>> Zia, something like this might work as long as MUKEY has no NA values: >>> >>> poly.data = merge(poly.data, data.table, by="MUKEY" ,all.x = TRUE) >>> >>> Robert >>> >>> On Wed, Jan 27, 2010 at 1:58 PM, Zia Ahmed <z...@cornell.edu> wrote: >>> >>>> >>>> I have created one data table and one polygon in R. Both have a similar >>>> type >>>> of column. I want to combine or join them with this column or field >>>> ("MUKEY"). Then I will convert this polygon as a ESRI shape file. >>>> But when I used spCbind, I got error. I think i miss something >>>> here. >>>> Is there any way join attribute table with polygon? Thanks >>>> Zia >>>> >>>> poly.data1 <- spCbind(poly.data, data.table) >>>> Error in spCbind(poly.data, data.table) : different numbers of rows >>>> >>>> >>>> names(poly.data) # polygoan >>>> [1] "AREASYMBOL" "SPATIALVER" "MUSYM" "MUKEY" > >>>> names(data.table) # >>>> data >>>> [1] "MUKEY" "Sand" "Silt" "Clay" "pH" "CEC" "EC" "SAR" >>>> "CaCO3" >>>> [10] "SOM" "BD" "AWC" "KSAT" "Kf" "Kw" "LL" "LEP" >>>> >>>>> >>>>> length(poly.data$MUKEY) >>>>> >>>> >>>> [1] 76969 >>>> >>>>> >>>>> length(data.table$MUKEY) >>>>> >>>> >>>> [1] 184 >>>> poly.data1 <- spCbind(poly.data, data.table) >>>> >>>> combinedShp1 <- "D:/test/combined1.shp" writeOGR(poly.data1, >>>> dsn=combinedShp1, layer="combined1", driver="ESRI Shapefile") >>>> >>>> _______________________________________________ >>>> R-sig-Geo mailing list >>>> R-sig-Geo@stat.math.ethz.ch >>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo >>>> >>>> >> >> _______________________________________________ >> R-sig-Geo mailing list >> R-sig-Geo@stat.math.ethz.ch >> https://stat.ethz.ch/mailman/listinfo/r-sig-geo > > _______________________________________________ R-sig-Geo mailing list R-sig-Geo@stat.math.ethz.ch https://stat.ethz.ch/mailman/listinfo/r-sig-geo