[R] replace matrix values with names from a dataframe

From: <jhainm_at_fas.harvard.edu>
Date: Thu 28 Jul 2005 - 23:52:29 EST


I am looking for a way to replace matrix values with names from a dataframe.

Let me do this by example: I have a dataframe:

1 munich

2     paris
3     tokio

4 london
5 boston

each city name corresponds to only one index number (there is only one observation for each city). After doing some matching I end up with a matrix that looks something like this:

> X

       [,1] [,2]
[1,] 2 4
[2,] 5 1
[3,] 5 3
[4,] 12 217
[5,] 16 13

Here the numbers in the matrix are the index numbers from my original dataset, each row is a matched pair (so e.g. the first row tells me that obs. number 2 (i.e. Paris) was matched to obs number 4 (i.e. London)).

Now I am looking for a quick way to transform the index numbers back to city names, so that at the end I have a matrix that looks something like this:

> X.transformed

         [,1] [,2]
[1,] paris london
[2,] boston munich
[3,] boston tokio
[4,] 12 217
[5,] 16 13

etc. So instead of the index number, the matrix should contain the names that corresponds to it. In my real data, I have many many names and replacing each value by hand would take too long. Any help is highly appreciated.

Thank you.


R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Thu Jul 28 23:56:18 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:39:37 EST