0

starting to use R in my data analysis, but still a relative newbie. I have a data frame that looks like this:

locations

loc IDnum
100000001 1
100000009 7
100000021 3
100000004 2
100000017 3
100000007 7
100000067 5

and a matrix with a list of the ID numbers (from the second column) and the corresponding strings (like a translation table of sorts).

Looks similar to this:

names

IDnum   IDnames
1  NNW43
2  N3
3  SE21
4  SW54
5  W6
6  W12
7  NE10
...

So this matrix is shorter because each ID number has a corresponding string, but in the original data frame there are more than one loc that contain the same IDnum.

I'm sure there is an easy way to match up each ID number with a string and create a new data frame with the first column the same but the second containing the strings instead of ID numbers, but I'm not sure what it is. All I know is that I'm told if you use loops in R you're doing it wrong.

Result would be something like

loc name
100000001 NNW43
100000009 NE10
100000021 SE21
100000004 N3
100000017 SE21
100000007 NE10
100000067 W6

I had a hard time figuring out how to title this question so any input on that would be useful as well. Thanks in advance!

EDITED to provide reproducible example

  • 5
    You probably looking for `merge` or `match`, but we don't have your other matrix here. – David Arenburg Jul 23 '15 at 20:22
  • How do you know what IDnum 12 matches to "string1"? As requested above, if you made a more complete [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) it would be easier to provide you with a solution, – MrFlick Jul 23 '15 at 20:38
  • Can you spear us the `
    ` and format your code just like I already did? Anyway, just try something like `merge(df1, df2, by = "IDnum", all.x = TRUE)`
    – David Arenburg Jul 23 '15 at 20:51
  • See [here](http://stackoverflow.com/questions/1299871/how-to-join-merge-data-frames-inner-outer-left-right) for more details. – David Arenburg Jul 23 '15 at 20:56
  • Thanks for your help! – janmarander Jul 23 '15 at 21:05

2 Answers2

0

If the names matrix or dataframe has sequentially numbered names, then would just use the IDnum as an index into that object's IDnames vector:

 locations$name <- names$IDnames[locations$IDnum]

If they are not sequentially numbered, then you need to use match to get the proper row number:

 locations$name <- names$IDnames[match( locations$IDnum, names$IDnum) ]

(This is in all probability a duplicate question and answer.)

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • I agree, it's probably a duplicate. I couldn't find a similar question but that is likely because I wasn't sure what terms to search. Had I searched "merge" that might have worked. – janmarander Jul 23 '15 at 21:32
  • If you take words from your title and search SO with: `[r] translate lookup table` , you do find relevant strategies. – IRTFM Jul 23 '15 at 21:35
  • Yes but originally I didn't have the phrase "lookup table" in my title. It wasn't until later that I realized that was a good description and changed the title to make it more relevant, so future visitors could find this question more easily. – janmarander Jul 23 '15 at 21:57
0

I ended up using loc_names <- merge(locations, names, by = "IDnum", all.x=TRUE)