From: Henrik Bengtsson <hb_at_stat.berkeley.edu>

Date: Wed, 23 Jul 2008 11:11:05 -0700

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. Received on Wed 23 Jul 2008 - 18:29:37 GMT

Date: Wed, 23 Jul 2008 11:11:05 -0700

Hi.

On Wed, Jul 23, 2008 at 9:23 AM, Chris82 <rubenbauar_at_gmx.de> wrote:

*>
*

> Hello,

*>
**> I have a problem to flip a 200x200 matrix, which is imported by a .asc file.
**> I want to flip the matrix like in a created example below:
**>
**>> b <- matrix(1:9,3,3,byrow=T)
**>> b
**>
**> [,1] [,2] [,3]
**> [1,] 1 2 3
**> [2,] 4 5 6
**> [3,] 7 8 9
**>
**>> b1 <- apply(t(b),1,rev)
**>
**>> b1
**> [,1] [,2] [,3]
**> [1,] 7 8 9
**> [2,] 4 5 6
**> [3,] 1 2 3
**>
*

Your flip is nothing but reordering the rows, so your apply(...) is a bit overkill. This can be done as:

X <- matrix(1:9, nrow=3, ncol=3, byrow=TRUE);
print(X);

Y <- X[nrow(X):1,];

print(Y);

This does also work for data frames:

Z <- as.data.frame(X);

Z <- Z[nrow(Z):1,];

print(Y);

> but my R Script doesn't work so. I don't know where the problem is.

*> What's the different between a created matrix and a imported matrix?
**>
**> Here is my r Script:
**>
**> a <- read.table("c:/Test/Test.asc", header=FALSE, sep=" ")
**> dim(a)
**> b <- matrix(a, nrow=200, byrow=TRUE)
**> d <- apply(t(b),1,rev)
**> write.table(d, file = "c:/Test/output.asc", sep = " ", row.names = FALSE,
**> col.names = FALSE)
*

Your mistake is most likely that you use byrow=TRUE, which results in placing the *first column* of data.frame read by read.table() into the *first row* of the matrix, and so on. Have you looked what 'a' and 'b' looks like? I guess that when you look at 'b' it is not what you expect.

Your probably better off by just reordering the rows as suggested above.

Cheers

Henrik

*>
*

> My output ist like this

*>
**> input:
**>
**> 1 2 3
**> 4 5 6
**> 7 8 9
**>
**> output:
**>
**> 3 2 1
**> 6 5 4
**> 9 8 7
**>
**> but it should be like this, as in the example:
**>
**> 7 8 9
**> 4 5 6
**> 1 2 3
**>
**> thanks.
**> --
**> View this message in context: http://www.nabble.com/Flip-Matrix-form-file--tp18614800p18614800.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.
**>
*

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. Received on Wed 23 Jul 2008 - 18:29:37 GMT

Archive maintained by Robert King, hosted by
the discipline of
statistics at the
University of Newcastle,
Australia.

Archive generated by hypermail 2.2.0, at Wed 23 Jul 2008 - 23:32:16 GMT.

*
Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help.
Please read the posting
guide before posting to the list.
*