Hi, I want to write a matrix (n*m) in a file (Text file) such that the file will be as Result file (below). I use the below command but it write all numbers in one column,> write(paste(matrixname),file="test.txt",append=TRUE)how can I do this? Result file: 5 5 -1 -1 -1 -1 8 8 2 7 6 5 6 6 8 2 7 5 Matrix: [,1] [,2] [,3] [,4] [,5] [,6] [1,] 5 5 -1 -1 -1 -1 [2,] 8 8 2 7 6 5 [3,] 6 6 8 2 7 5 Regards, Amir
try this:> x <- matrix(1:25,5) > x[,1] [,2] [,3] [,4] [,5] [1,] 1 6 11 16 21 [2,] 2 7 12 17 22 [3,] 3 8 13 18 23 [4,] 4 9 14 19 24 [5,] 5 10 15 20 25> write.table(x, file='')"V1" "V2" "V3" "V4" "V5" "1" 1 6 11 16 21 "2" 2 7 12 17 22 "3" 3 8 13 18 23 "4" 4 9 14 19 24 "5" 5 10 15 20 25> write.table(x, file='', row.names=FALSE, col.names=FALSE)1 6 11 16 21 2 7 12 17 22 3 8 13 18 23 4 9 14 19 24 5 10 15 20 25 On Wed, Jun 2, 2010 at 9:34 AM, amir <amir at ac.upc.edu> wrote:> Hi, > > I want to write a matrix (n*m) in a file (Text file) ?such that the file > will be as Result file (below). > I use the below command but it write all numbers in one column, > >> write(paste(matrixname),file="test.txt",append=TRUE) > > how can I do this? > > Result file: > 5 ? ?5 ? -1 ? -1 ? -1 ? -1 > 8 ? ?8 ? ?2 ? ?7 ? ?6 ? ?5 > 6 ? ?6 ? ?8 ? ?2 ? ?7 ? ?5 > > Matrix: > ? ? [,1] [,2] [,3] [,4] [,5] [,6] > [1,] ? ?5 ? ?5 ? -1 ? -1 ? -1 ? -1 > [2,] ? ?8 ? ?8 ? ?2 ? ?7 ? ?6 ? ?5 > [3,] ? ?6 ? ?6 ? ?8 ? ?2 ? ?7 ? ?5 > > Regards, > Amir > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Hi Amir, Here is a suggestion: write.table(yourmatrix, "mymatrix.txt", col.names = FALSE, row.names FALSE, sep = "\t", quote = FALSE) After executing this, you will see a "mymatrix.txt" file in your working directory. Here, typing getwd() in the R console may also help. Do not forget to take a look at ?write.table. HTH, Jorge On Wed, Jun 2, 2010 at 9:34 AM, amir <> wrote:> Hi, > > I want to write a matrix (n*m) in a file (Text file) such that the file > will be as Result file (below). > I use the below command but it write all numbers in one column, > > > write(paste(matrixname),file="test.txt",append=TRUE) > > how can I do this? > > Result file: > 5 5 -1 -1 -1 -1 > 8 8 2 7 6 5 > 6 6 8 2 7 5 > > Matrix: > [,1] [,2] [,3] [,4] [,5] [,6] > [1,] 5 5 -1 -1 -1 -1 > [2,] 8 8 2 7 6 5 > [3,] 6 6 8 2 7 5 > > Regards, > Amir > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
On 10-06-02 10:34 AM, amir wrote:> I want to write a matrix (n*m) in a file (Text file) such that the file > will be as Result file (below). > I use the below command but it write all numbers in one column, > >> write(paste(matrixname),file="test.txt",append=TRUE) > > how can I do this? > ... > Matrix: > [,1] [,2] [,3] [,4] [,5] [,6] > [1,] 5 5 -1 -1 -1 -1 > [2,] 8 8 2 7 6 5 > [3,] 6 6 8 2 7 5x <- matrix(1:12, nrow = 4) sink("test.txt") cat("Matrix:\n") x sink() Also look at ?write.table Sean