Hello, Is there a function in R which can transform, let say a vector: c(1:4) to a matrix where the vector is repeated but "projected" +1 one step down for every (new) column. I want the output below from the vector above, like this: p<-c(1,2,3,4,0,0,0,0,1,2,3,4,0,0,0,0,1,2,3,4,0,0,0,0,1,2,3,4) matrix(p,7,4) best regards / Peter [[alternative HTML version deleted]]
## 1. ## This could be captured into a function tmp <- matrix(0, 7, 4) tmp diag(tmp) <- 1 diag(tmp[-1,]) <- 2 diag(tmp[-(1:2),]) <- 3 diag(tmp[-(1:3),]) <- 4 tmp ## 2. v <- 1:4 v2 <- c(v, rep(0, length(v))) ## this generates a warning that can safely be ignored (or turned off) matrix(v2, length(v2)-1, length(v)) On Mon, Mar 6, 2017 at 11:18 AM, Peter Thuresson <peter.thuresson at umea.se> wrote:> Hello, > > Is there a function in R which can transform, let say a vector: > > c(1:4) > > to a matrix where the vector is repeated but "projected" +1 one step down for every (new) column. > I want the output below from the vector above, like this: > > p<-c(1,2,3,4,0,0,0,0,1,2,3,4,0,0,0,0,1,2,3,4,0,0,0,0,1,2,3,4) > > matrix(p,7,4) > > best regards / Peter > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
Hello, Try the following. proj <- function(x){ n <- length(x) y <- numeric(n) z <- rep(c(x, y), times = n) z <- z[-((length(z) - n + 1):length(z))] matrix(z, ncol = n) } proj(1:4) proj(1:5) Hope this helps, Rui Barradas Em 06-03-2017 16:18, Peter Thuresson escreveu:> Hello, > > Is there a function in R which can transform, let say a vector: > > c(1:4) > > to a matrix where the vector is repeated but "projected" +1 one step down for every (new) column. > I want the output below from the vector above, like this: > > p<-c(1,2,3,4,0,0,0,0,1,2,3,4,0,0,0,0,1,2,3,4,0,0,0,0,1,2,3,4) > > matrix(p,7,4) > > best regards / Peter > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >
Effectively you want a circulant matrix but filled by column. Example input vector and number of columns x = c(1:8,19:20) nc = 5 For the result you specifically describe, the following generalises for any vector and any arbitrary number of columns 'nc', padding with zeros as necessary. matrix(c(rep(c(x,rep(0,nc)),nc-1),x), ncol=nc) For the unpadded column circulant on the same inputs nx = length(x) ind = 1+ outer(0:(nx-1),0:(1-nc),'+') %% nx matrix(x[ind], ncol=nc) Cheers> On 6 Mar 2017, at 16:18, Peter Thuresson <peter.thuresson at umea.se> wrote: > > Hello, > > Is there a function in R which can transform, let say a vector: > > c(1:4) > > to a matrix where the vector is repeated but "projected" +1 one step down for every (new) column. > I want the output below from the vector above, like this: > > p<-c(1,2,3,4,0,0,0,0,1,2,3,4,0,0,0,0,1,2,3,4,0,0,0,0,1,2,3,4) > > matrix(p,7,4) > > best regards / Peter > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
Assuming that the input is x <- 1:4, try this one-liner:> embed(c(0*x[-1], x, 0*x[-1]), 4)[,1] [,2] [,3] [,4] [1,] 1 0 0 0 [2,] 2 1 0 0 [3,] 3 2 1 0 [4,] 4 3 2 1 [5,] 0 4 3 2 [6,] 0 0 4 3 [7,] 0 0 0 4 On Mon, Mar 6, 2017 at 11:18 AM, Peter Thuresson <peter.thuresson at umea.se> wrote:> Hello, > > Is there a function in R which can transform, let say a vector: > > c(1:4) > > to a matrix where the vector is repeated but "projected" +1 one step down for every (new) column. > I want the output below from the vector above, like this: > > p<-c(1,2,3,4,0,0,0,0,1,2,3,4,0,0,0,0,1,2,3,4,0,0,0,0,1,2,3,4) > > matrix(p,7,4) >-- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
Change the "4" in embed(c(0*x[-1], x, 0*x[-1]), 4) to length(x) and it will generalize to other length vectors. This solution is not as compact, but it illustrates a relatively obscure R function:> x <- 1:4 > ncol <- length(x) > zeros <- rep(0, ncol - 1) > toeplitz(c(zeros, x, zeros))[-(1:(ncol-1)), 1:ncol][,1] [,2] [,3] [,4] [1,] 1 0 0 0 [2,] 2 1 0 0 [3,] 3 2 1 0 [4,] 4 3 2 1 [5,] 0 4 3 2 [6,] 0 0 4 3 [7,] 0 0 0 4 ------------------------------------- David L Carlson Department of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Gabor Grothendieck Sent: Tuesday, March 7, 2017 7:21 AM To: Peter Thuresson <peter.thuresson at umea.se> Cc: R-help at r-project.org Subject: Re: [R] Matrix Assuming that the input is x <- 1:4, try this one-liner:> embed(c(0*x[-1], x, 0*x[-1]), 4)[,1] [,2] [,3] [,4] [1,] 1 0 0 0 [2,] 2 1 0 0 [3,] 3 2 1 0 [4,] 4 3 2 1 [5,] 0 4 3 2 [6,] 0 0 4 3 [7,] 0 0 0 4 On Mon, Mar 6, 2017 at 11:18 AM, Peter Thuresson <peter.thuresson at umea.se> wrote:> Hello, > > Is there a function in R which can transform, let say a vector: > > c(1:4) > > to a matrix where the vector is repeated but "projected" +1 one step down for every (new) column. > I want the output below from the vector above, like this: > > p<-c(1,2,3,4,0,0,0,0,1,2,3,4,0,0,0,0,1,2,3,4,0,0,0,0,1,2,3,4) > > matrix(p,7,4) >-- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.