[R] how to delete a matrix column

Petr Savicky savicky at cs.cas.cz
Sun Jun 17 21:46:09 CEST 2012


On Mon, Jun 18, 2012 at 01:24:21AM +0800, 李红旺 wrote:
> > a<-c(1,4)
> > a
> [1] 1 4
> > b<-a*5
> > b
> [1] 5 20
> 
> a is a very long vector , how can i get c(1:5,4:20)? i do not want to
> use a loop.

Hi.

How large are the numbers in a? If max(a) is not too large, try
the following.

  a <- c(1, 4, 3, 1)
  ma <- max(a)
  b <- matrix(a, nrow=4*ma+1, ncol=length(a), byrow=TRUE) +
       matrix(0:(4*ma), nrow=4*ma+1, ncol=length(a))
  out <- c(b[b <= rep(5*a, each=4*ma+1)])
  out

  [1]  1  2  3  4  5  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20  3  4  5
  [26]  6  7  8  9 10 11 12 13 14 15  1  2  3  4  5

Hope this helps.

Petr Savicky.



More information about the R-help mailing list