You can merge attribute tables / dataframes using merge
and a shared id/name.
Load a spatial dataset using sf
:
library(sf)
parks <- read_sf("~/workspace/_temp/ZM_parks.shp")
Resulting data:
Simple feature collection with 4 features and 1 field
geometry type: POLYGON
dimension: XY
bbox: xmin: 22.29239 ymin: -16.69977 xmax: 30.3777 ymax: -12.3986
CRS: 4326
# A tibble: 4 x 2
name geometry
<chr> <POLYGON [°]>
1 Kasanka ((30.08454 -12.4032, 30.08514 -12.40292, 30.08713 -12.40218,…
2 Kafue ((26.54187 -14.48086, 26.54334 -14.48032, 26.5448 -14.47933,…
3 Liuwa Pla… ((22.37276 -14.21263, 22.37328 -14.21284, 22.37382 -14.21283…
4 Lusaka ((28.34302 -15.49064, 28.43507 -15.49114, 28.43567 -15.49218…
Create a matching set of data on name
:
prefs <- data.frame(name = c("Kasanka", "Kafue", "Liuwa Plains", "Lusaka"),
preference = c(4, 1, 3, 2))
Merge:
merge(parks, prefs, by = "name")
Result is the spatial object with a new column for 'preferences':
Simple feature collection with 3 features and 2 fields
geometry type: POLYGON
dimension: XY
bbox: xmin: 25.21394 ymin: -16.69977 xmax: 30.3777 ymax: -12.3986
CRS: 4326
name preference geometry
1 Kafue 1 POLYGON ((26.54187 -14.4808...
2 Kasanka 4 POLYGON ((30.08454 -12.4032...
3 Lusaka 2 POLYGON ((28.34302 -15.4906...
cbind
is not correct, look at?merge
or?dplyr::left_join
. Since you do not provide a code example we cannot say anything regarding "Why might this be happening?". Have you looked at the arguments for merge? There are several that control how the merge returns results (eg.,all.x
,all
,all.y
).