Pergunta

I have successfully added information to shapefiles before (see my post on http://rusergroup.swansea.ac.uk/Healthmap.ashx?HL=map ).

However, I just tried to do it again with a slightly different shapefile (new local health boards for Wales) and the code fails at spCbind with a "row names not identical error"

o <- match(wales.lonlat$NEW_LABEL, wds$HB_CD)
wds.xtra <- wds[o,]
wales.ncchd <- spCbind(wales.lonlat, wds.xtra)

My rows did have different names before and that didn't cause any problems. I relabeled the column in wds.xtra to match "NEW_LABEL" and that doesn't help.

The labels and order of labels do match exactly between wales.lonlat and wds.xtra.

(I'm using Revolution R 5.0, which is built on R 2.13.2)

Foi útil?

Solução

I use match to merge data to the sp data slot based on rownames (or any other common ID). This avoids the necessity of maptools for the spCbind function.

# Based on rownames
sdata@data=data.frame(sdata@data, new.df[match(rownames(sdata@data), rownames(new.df)),])

# Based on common ID
sdata@data=data.frame(sdata@data, new.df[match(sdata@data$ID, new.df$ID),])

# where; sdata is your sp object and new.df is a data.frame object that you want to merge to sdata.

Outras dicas

I had the same error and could resolve it by deleting all other data, which were not actually to be added. I suppose, they confused spCbind because the matching wanted to match all row-elements, not only the one given. In my example, I used

xtra2 <- data.frame(xtra$ID_3, xtra$COMPANY)

to extract the relevant fields and fed them to spCbind afterwards

gadm <- spCbind(gadm, xtra2)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top