[R] swapping rows with columns

Jonathan Baron baron at cattell.psych.upenn.edu
Tue Mar 12 18:01:26 CET 2002

On 03/12/02 15:41, Arne Mueller wrote:
>I've read in a data file as a frame and now I'd like the columns to be
>rows and the rows to be columns. What's the easiest way to do this in R?
>> class(d)
>[1] "data.frame"
>> rownames(d)
>[1] "98x101" "40x98"  "30x40"  "0x30"  
>> colnames(d)
> [1] "H..sapiens"      "C..elegans"      "D..melanogaster"
> [5] "E..coli"         "M..tuberculosis" "B..subtilis"    
> [9] "A..pernix"       "P..horikoshii"   "M..jannaschii"  
>[13] "H..pylori"       "M..genitalium"  
>Do I have to recunstruct the new frame or matrix manually?

No.  The t() function flips the rows and columns of a matrix.  It
also works on a data frame that can be coerced into a matrix, but
it yields a matrix.  So you can say

d2 <- as.data.frame(t(d))

Jonathan Baron, Professor of Psychology, University of Pennsylvania
Home page: http://www.sas.upenn.edu/~baron
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list