0

I have two dataframes. The first one is a list of names:

head(record_g)
no   om_id     nm_id
1  1  barnes     hardy
2  2    fry3     long1
3  3    <NA> buckland1

The second one contains locations, one for each name:

     master                           ID                   geometry
 1   goulter               Acton Turville POINT (-2.277019 51.52794)
 2 sergeant1                     Alderton POINT (-2.231605 51.54452)
 3    baker2                    Allington POINT (-2.151944 51.47325)

For each person from the first dataframe I need to 'pull' his location from the second dataframe, so that I have a dataframe similar to the first one with location ID's rather than names. I've tried a function:

geo <- function(x){
  index <- match(x, places$master)
  geo <- places$geometry[index]
  geo
  }

but it does not work with character variables. Could anybody help? Thank you.

Mikhail
  • 37
  • 5
  • Sorry, I've put it the wrong way. My function ONLY works with character variables :( – Mikhail Sep 17 '20 at 20:19
  • 2
    `merge` should do this but what is `master` supposed to match? `om_id` or `nm_id`. Provide reproducible data (e.g. 15 lines) of each using `dput(head(record_g, 15)` and the same for `places`. Make sure there are at least a few matches. – dcarlson Sep 17 '20 at 20:32
  • Tentatively closing this as a dupe of the merge/join FAQ - it sounds like what you want is `merge(record_g, places[c("master", "geometry")], by.x = "<>", by.y = "master", all.x = TRUE)`. If that's not the case, please edit your question with enough details to understand what's different. – Gregor Thomas Sep 17 '20 at 20:50
  • master should match, first, om_id and second, nm_id, as I need both locations. But it's not a big problem, I can do this in two steps. I renamed the om_id column and called this dataframe rec; then I tried: rec15 <- dput(head(record_g, 15)) p <- dput(places) merge(r15, p) But all I've got was all lines of the second dataframe added to each line of the first one. – Mikhail Sep 17 '20 at 20:55
  • I am sorry but it's not just merging two dataframes by a column. There is in the second dataframe a line for the each name in the first one, but it's still to be found and pulled from there to the first dataframe. – Mikhail Sep 17 '20 at 21:06
  • Hi Gregor, you are absolutely right, this solved my problem. Thank you so much! – Mikhail Sep 17 '20 at 21:35

0 Answers0