Skip to content

question about transpose

3 messages · Zoppoli, Gabriele (NIH/NCI) [G], David Winsemius, Joshua Wiley

#
Hi all,

if I transpose a matrix with t(data), the newly created colums do not appear to have the first row as header. How can I do to have all the newly created columns have their first row as a header?

Thanks


Gabriele Zoppoli, MD
Ph.D. Fellow, Experimental and Clinical Oncology and Hematology, University of Genova, Genova, Italy
Guest Researcher, LMP, NCI, NIH, Bethesda MD

Work: 301-451-8575
Mobile: 301-204-5642
Email: zoppolig at mail.nih.gov
#
On Jan 29, 2010, at 6:58 PM, Zoppoli, Gabriele (NIH/NCI) [G] wrote:

            
The term "header" doesn't really have meaning in R with reference to  
matrices. And the analog that might make sense, column names, fails to  
behave in the manner of your complaint.


 > M <- matrix(1:9, 3,3)
 > colnames(M) <- letters[1:3]
 > M
      a b c
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
 > t(M)
   [,1] [,2] [,3]
a    1    2    3
b    4    5    6
c    7    8    9

So exactly _what_ is your problem?
#
Hello,

Here are three examples.? I have a hunch you are looking for the last one.
?????? [,1] [,2] [,3]
First? "A"? "C"? "E"
Second "B"? "D"? "F"
???? First Second
[1,] "A"?? "B"
[2,] "C"?? "D"
[3,] "E"?? "F"
? [,1] [,2] [,3]
A "A"? "C"? "E"
B "B"? "D"? "F"
???? A?? B
[1,] "A" "B"
[2,] "C" "D"
[3,] "E" "F"


## My guess is that you are looking for this one.  What was in the
first column in the original matrix, is removed from the transposed
matrix and used as the column names
## "[,-1]" indicates everything but column 1
## ncol has to be specified because I used matrix() so I could include dimnames
## dimnames are set to be the contents of the first column of the
original matrix
???? [,1] [,2] [,3]
[1,] "a"? "c"? "e"
[2,] "b"? "d"? "f"
???? a?? b
[1,] "c" "d"
[2,] "e" "f"


HTH,


Joshua



On Fri, Jan 29, 2010 at 3:58 PM, Zoppoli, Gabriele (NIH/NCI) [G]
<zoppolig at mail.nih.gov> wrote:
--
Joshua Wiley
Senior in Psychology
University of California, Riverside
http://www.joshuawiley.com/