R Transpose Matrix Example


t(x) function will transpose a matrix or data frame. If it is a vector, it will be transposed to a one row matrix.

Following is a csv file example transpose.csv.


>x <- read.csv("transpose.csv",header=T,sep=",",dec=".");
>x <- t(x)


However, if you want to transpose the matrix as well as revise some elements, the above code can not do that. Following R code can read in a file into matrix, transpose it and then write to an output file. It do that by reading through each element, such that you may revise the elements as you like, e.g. substring or do some calculation during the process.




The result file is:



Download the csv file and the R source code:
Data File
R Source Code File
Output Data File

endmemo.com © 2024  | Terms of Use | Privacy | Home