[R] Matrix

Richard M. Heiberger rmh at temple.edu
Mon Mar 6 18:24:50 CET 2017


## 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.



More information about the R-help mailing list