Re: [R] replace matrix values with names from a dataframe
Does the following help: set.seed(1) Lvls - factor(letters[1:4]) A - array(sample(4, 6, replace=TRUE), dim=c(2,3)) A[] - levels(Lvls)[A] A [,1] [,2] [,3] [1,] b c a [2,] b d d If not, PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html; and submit another question (if the process of working the posting guide does not itself provide enlightenment). spencer graves [EMAIL PROTECTED] wrote: Hi, 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: data city.name 1munich 2 paris 3 tokio 4london 5boston 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,]24 [2,]51 [3,]53 [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,] bostontokio [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. Regards, Jens __ 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 -- Spencer Graves, PhD Senior Development Engineer PDF Solutions, Inc. 333 West San Carlos Street Suite 700 San Jose, CA 95110, USA [EMAIL PROTECTED] www.pdf.com http://www.pdf.com Tel: 408-938-4420 Fax: 408-280-7915 __ 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
[R] replace matrix values with names from a dataframe
Hi, 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: data city.name 1munich 2 paris 3 tokio 4london 5boston 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,]24 [2,]51 [3,]53 [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,] bostontokio [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. Regards, Jens __ 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
Re: [R] replace matrix values with names from a dataframe
maybe something like this could be helpful city.name - c(munich, paris, tokio, london, boston) X - cbind(c(2, 5, 5), c(4, 1, 3)) matrix(city.name[X], ncol = 2) I hope it helps. Best, Dimitris Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/16/336899 Fax: +32/16/337015 Web: http://www.med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm - Original Message - From: [EMAIL PROTECTED] To: r-help@stat.math.ethz.ch Sent: Thursday, July 28, 2005 3:52 PM Subject: [R] replace matrix values with names from a dataframe Hi, 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: data city.name 1munich 2 paris 3 tokio 4london 5boston 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,]24 [2,]51 [3,]53 [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,] bostontokio [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. Regards, Jens __ 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 __ 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