0

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). data

  • 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).

    enter image description here

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?

Andre Wildberg
  • 12,344
  • 3
  • 12
  • 29
June
  • 11
  • 2
  • That `merge()` looks correct. Are you sure that set of unique values in both `data$Location` and `clean_lonlat$clean_naam`are identical? I.e. there's no trailing whitespace in one or another? – margusl Jan 13 '23 at 13:51

1 Answers1

0

could you try the left join

data.coordinates <- data %>% left_join(clean_lonlat, by=c("Location"="clean_naam"))
jkatam
  • 2,691
  • 1
  • 4
  • 12