try this: > x V2 V3 V4 [1,] 1 1 1 [2,] 2 2 2 [3,] 3 3 3 [4,] 4 4 4 [5,] 5 NA 5 [6,] NA NA 6 [7,] NA NA NA > offset <- c(0,2,1) > # add the control to the data and make two copies so we can offset > x.new <- rbind(offset, x, x) > result <- apply(x.new, 2, function(.col){ + .col[seq(nrow(x) - .col[1L] + 2L, length = nrow(x))] + }) > result V2 V3 V4 1 NA NA 2 NA 1 3 1 2 4 2 3 5 3 4 NA 4 5 NA NA 6
On Thu, Nov 4, 2010 at 11:47 AM, emj83 <stp08...@shef.ac.uk> wrote: > > Hi, > > Is there a quick way to go from this matrix: >> A > [,1] [,2] [,3] > [1,] 1 1 1 > [2,] 2 2 2 > [3,] 3 3 3 > [4,] 4 4 4 > [5,] 5 NA 5 > [6,] NA NA 6 > [7,] NA NA NA > > to this matrix: >> B > [,1] [,2] [,3] > [1,] 1 NA NA > [2,] 2 NA 1 > [3,] 3 1 2 > [4,] 4 2 3 > [5,] 5 3 4 > [6,] NA 4 5 > [7,] NA NA 6 > > without using a loop? > For example using a vector which describes how many NA's are required from > the "top" of the matrix- so in this case it would be c(0,2,1). > > Many thanks Emma > > > -- > View this message in context: > http://r.789695.n4.nabble.com/Matrix-Manipulation-tp3027266p3027266.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. > -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? ______________________________________________ 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.