Hi. On Wed, Jul 23, 2008 at 9:23 AM, Chris82 <[EMAIL PROTECTED]> 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@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@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.