I wanted to join a CSV file to a shapefile.

When I identified a field to facilitate the join, e.g.,

map <- spChFIDs(map, as.character(map$ID))

it returned

Error in spChFIDs(SP, x) : lengths differ

Can anyone advise?

  • Thanks. But the column ID does have unique values that can be used as the join field. Must the inputs of the field be of same length? – user3915459 Aug 12 '14 at 15:37
  • Could you post a sample of your map and csv? – cengel Aug 12 '14 at 18:36
  • @cengel It's a huge datasets and a screenshot won't be enought to show the variations. The field "ID" in the shapefile "map" is a string of numbers (with various number of digits). – user3915459 Aug 13 '14 at 18:28
  • Perhaps save the CSV to a table, so you know the join fields are the same data type. – klewis Aug 20 '14 at 20:41

Here is how you can do it in R using sp::merge

# read data    
p <- shapefile("path/file.shp")
d <- read.csv("path/file.csv")

# merge on common variable, here called 'key'
m <- merge(p, d, by='key')

# perhaps save as shapefile again
shapefile(m, "path/merged.shp")
  • Hi @RobertH, I was sent here from the question I posted yesterday. When I merge by the common variable, do I pull that variable from the shapefile or the CSV? The two variables are matching in content, but not by name. I've tried playing around with it a bit and keep getting errors (mainly: Error in [.data.frame(x@data, , by.x, drop = FALSE) : undefined columns selected) – Lauren Nov 17 '15 at 15:09
  • 1
    Figured it out. Because I needed to join the two layers that had different names, I had to alter the format a bit. It looked more like merge(p, d, by.x = "key", by.y = "matching.key") – Lauren Nov 17 '15 at 15:23

I don't know how to do with R to join a shapefile with a csv. However, if this can help you, you can also do this on QGIS using the pluggin MMQGIS.

enter image description here

  • Thanks. I've tried with ArcGIS before but the data set is huge and I prefer to do this in R. – user3915459 Aug 12 '14 at 15:38

If you're working in R then your shapefile and .csv need to be loaded as a data.frame or a data.table. You can use join() to merge them.

Do you have any more information about what kind of shapefile you have?

I've had luck doing the following when I merge shapefiles and data. Note that you need to leave off the .shp extension in the "layer" argument. I used "GEOID" where you'd put whatever the name of the "shape" in interest is.

shapefile@data$id = rownames(shapefile@data)
shapefile.points = fortify(shapefile, region = "id")
shapefile.df = join(shapefile.points, shapefile@data, by = "id")
shapefile.df = subset(shapefile.df, select = c(long, lat, group, GEOID))
names(shapefile.df) = c("long", "lat", "group", "GEOID")

Then you can merge your data using something like

full.data = join(mydata, shapefile.df, by = "GEOID", type = "full")

If you are working in R then merging is actually not required. You can use ggplot to display your data on a map. See, my example. This one liner works wonders with shape file bangladesh = fortify(p1, region = "ADM1_EN"). And, then you can use

ggplot() + geom_map(data = br2016a, aes(map_id = ADM1_EN, fill = HectareLocal), 
                    map =bangladesh) +
  expand_limits(x = bangladesh$long, y = bangladesh$lat) + 
  coord_fixed(.96) +
  scale_fill_gradient(low="thistle2", high="darkred", 
                      guide="colorbar", na.value="white") +
  labs(title = "Bangladesh Boro Rice (Local variety), 2016", x = element_blank(), y = element_blank(), 
       fill='Hectares') +   theme(legend.position = "bottom") 

And, the map I get is here.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.