I'm trying to merge two dataframes:
df1 (
data
): has data for multiple individuals, one column specifies the location (Location
) where the individual (Bird
)was found. Therefore there exist multiple rows with the same location name (fe. Ijzermonding_slikken).df2 (
clean_lonlat
): has the same location names as df1, but also includes the coordinates (lonlat$WKT
) from this exact location (clean_naam
). There only exists one row per location with coordinates (see again Ijzermonding_slikken).
I want to merge the two datasets so that in df1 the coordinates corresponding to each location (taken out of df2) are included.
I've come up with this codeline:
data.coordinates <- merge(data, clean_lonlat, by.x="Location", by.y="clean_naam",all.x=TRUE)
However, if I try this I get as output a lot of NA's in the coordinates column because the number of rows in each dataframe are not equal, and the code only provides the first Location
with the corresponding coordinates. Is there a way to add coordinates to each location?