Hi all - I am NEW to R and NEW to any type of programming. I am making heatmaps using the heatmap.2 function within gplots package. At present, when the heatmap is plotted it uses the row identifiers as 1,2,3,4...etc. However, I much rather use my own labels. I was told my another well-versed R programmer to use the follow script: x<-as.matrix(test1[,-1]) ## skip column 1 rownames(x)<- test[1,] heatmap(x, scale="none") This was for data set up in 4 columns such as when you read the data in R it looks like: Loci Cont NaCl Peg 1 0231 2.1 4.2 4.1 2 1253 4.1 2.3 2.3 3 8167 5.7 1.1 3.4 Using the script above and this dataset it worked well - the Loci was used as the row labels rather than the numbers assigned by R. Looking at the data after this script it looks like: Loci Cont NaCl Peg 0231 0231 2.1 4.2 4.1 1253 1253 4.1 2.3 2.3 8167 8167 5.7 1.1 3.4 Now, I want to do the same thing BUT my dataset looks like this: Loci NaCl Peg 1 0231 23.2 34.1 2 1253 25.3 21.3 3 8167 21.1 38.4 When I put in the script x<-as.matrix(test1[,-1]) ## skip column 1 rownames(x)<- test[1,] heatmap(x, scale="none") This is what follows (after looking at the dataset change) NaCl Peg [1,] 23.2 34.1 [2,] 25.3 21.3 [3,] 21.1 38.4 If I alter the -1 in the x<-as.matrix(test1[,-1]) line it will 'skip' other columns but not the first one. What do I need to enter to make the dataset look like ? Loci NaCl Peg 0231 0231 23.2 34.1 1253 1253 25.3 21.3 8167 8167 21.1 38.4 Please explain the answer and WHAT the #'s inside the [] mean! Thanks -- View this message in context: http://r.789695.n4.nabble.com/Row-names-and-matrixs-tp3516372p3516372.html Sent from the R help mailing list archive at Nabble.com.
Dear Anonymous/Lindsey Nielsen(?): While it would not be hard to answer your questions, given your admitted lack of programming or R experience, approaching your ignorance piecemeal in this fashion does not seem to be a sensible way to learn either. Instead, please start by reading the "Introduction to R" tutorial that comes with the software and working through its examples carefully. More than likely, this will get you well on your way and also allow you gain access to many more or R's capabilities. If you analyze data as part of your career, you won't regret this investment. You will also be able to use R's native Help system and search capabilities, which much of the time will provide you the information you need to solve your difficulties. After you have done this, you should also be able to seek the help of this list more effectively when you are stumped (as most of us are from time to time), thus wasting both less of both your time and ours. Cheers, Bert On Wed, May 11, 2011 at 7:23 PM, nielsen4897 <lindseynielsenpc at gmail.com> wrote:> Hi all - > I am NEW to R and NEW to any type of programming. ?I am making heatmaps > using the heatmap.2 function within gplots package. ?At present, when the > heatmap is plotted it uses the row identifiers as 1,2,3,4...etc. ?However, I > much rather use my own labels. ?I was told my another well-versed R > programmer to use the follow script: > x<-as.matrix(test1[,-1]) ?## skip column 1 > rownames(x)<- test[1,] > heatmap(x, scale="none") > > This was for data set up in 4 columns such as when you read the data in R it > looks like: > ? ? ? ?Loci ? ? ?Cont ? ? NaCl ? ? Peg > 1 ? ? ?0231 ? ? 2.1 ? ? ? 4.2 ? ? ? 4.1 > 2 ? ? ?1253 ? ? 4.1 ? ? ? 2.3 ? ? ?2.3 > 3 ? ? ?8167 ? ? 5.7 ? ? ? 1.1 ? ? ?3.4 > > Using the script above and this dataset it worked well - the Loci was used > as the row labels rather than the numbers assigned by R. > Looking at the data after this script it looks like: > ? ? ? ? ? ? Loci ? ? ?Cont ? ? NaCl ? ? Peg > 0231 ? ? 0231 ? ? 2.1 ? ? ? 4.2 ? ? ? 4.1 > 1253 ? ? ?1253 ? ? 4.1 ? ? ? 2.3 ? ? ?2.3 > 8167 ? ? ?8167 ? ? 5.7 ? ? ? 1.1 ? ? ?3.4 > > > Now, I want to do the same thing BUT my dataset looks like this: > ? ? ? Loci ? ? ? NaCl ? ? Peg > 1 ? ? ?0231 ? ? 23.2 ? ? ? 34.1 > 2 ? ? ?1253 ? ? 25.3 ? ? ?21.3 > 3 ? ? ?8167 ? ? 21.1 ? ? ?38.4 > > When I put in the script > x<-as.matrix(test1[,-1]) ?## skip column 1 > rownames(x)<- test[1,] > heatmap(x, scale="none") > > This is what follows (after looking at the dataset change) > ? ? ? ? ?NaCl ? ? ? Peg > [1,] ? ?23.2 ? ? ? 34.1 > [2,] ? ?25.3 ? ? ? 21.3 > [3,] ? ?21.1 ? ? ? 38.4 > > If I alter the -1 in the x<-as.matrix(test1[,-1]) ?line it will 'skip' other > columns but not the first one. > > What do I need to enter to make the dataset look like ? > ? ? ? ? ? ? ?Loci ? ? ? NaCl ? ? Peg > 0231 ? ? ?0231 ? ? 23.2 ? ? ? 34.1 > 1253 ? ? ?1253 ? ? 25.3 ? ? ?21.3 > 8167 ? ? ?8167 ? ? 21.1 ? ? ?38.4 > > Please explain the answer and WHAT the #'s inside the [] mean! > > Thanks > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Row-names-and-matrixs-tp3516372p3516372.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- "Men by nature long to get on to the ultimate truths, and will often be impatient with elementary studies or fight shy of them. If it were possible to reach the ultimate truths without the elementary studies usually prefixed to them, these would not be preparatory studies but superfluous diversions." -- Maimonides (1135-1204) Bert Gunter Genentech Nonclinical Biostatistics
Nielsen, The numbers in the brackets reference a component of a matrix/data frame/vector. So if you have:> x <- c(1:10) # a vector of integers in sequence from 1-10 > x[3] # the third component of x[1] 3 For 2-way matrices or data frames, the formatting is [row,column]. So, for a 10 x 10 matrix x:> x <- matrix(1:100, ncol = 10, byrow = T) > x[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 2 3 4 5 6 7 8 9 10 [2,] 11 12 13 14 15 16 17 18 19 20 [3,] 21 22 23 24 25 26 27 28 29 30 [4,] 31 32 33 34 35 36 37 38 39 40 [5,] 41 42 43 44 45 46 47 48 49 50 [6,] 51 52 53 54 55 56 57 58 59 60 [7,] 61 62 63 64 65 66 67 68 69 70 [8,] 71 72 73 74 75 76 77 78 79 80 [9,] 81 82 83 84 85 86 87 88 89 90 [10,] 91 92 93 94 95 96 97 98 99 100> x[,1] # return the first column of x[1] 1 11 21 31 41 51 61 71 81 91> x[1,] # return the first row of x[1] 1 2 3 4 5 6 7 8 9 10 when there's a minus, it just means that component is omitted> x[-1,] # return x less the first row[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 11 12 13 14 15 16 17 18 19 20 [2,] 21 22 23 24 25 26 27 28 29 30 [3,] 31 32 33 34 35 36 37 38 39 40 [4,] 41 42 43 44 45 46 47 48 49 50 [5,] 51 52 53 54 55 56 57 58 59 60 [6,] 61 62 63 64 65 66 67 68 69 70 [7,] 71 72 73 74 75 76 77 78 79 80 [8,] 81 82 83 84 85 86 87 88 89 90 [9,] 91 92 93 94 95 96 97 98 99 100 Given this context, I would double check the contents of test vs. test1. And don't let arrogant posts on this help forum discourage you. I hope this helps. -- View this message in context: http://r.789695.n4.nabble.com/Row-names-and-matrixs-tp3516372p3518836.html Sent from the R help mailing list archive at Nabble.com.