[R] subsetting a matrix with specified no of columns

William Dunlap wdunlap at tibco.com
Thu Apr 8 18:40:41 CEST 2010


> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Lee William
> Sent: Thursday, April 08, 2010 4:40 AM
> To: r-help at r-project.org
> Subject: [R] subsetting a matrix with specified no of columns
> 
> Hello! All,
> 
> I am working on 10000x1000 matrix say 'mat' and i want to 
> subset this matrix
> in a fashion that in new matrix i get columns 
> 2,3,9,10,16,17,23,24.......so
> on. That is pair of columns after every interval of 7. I 
> tried following but
> i got an error which is obvious.
> 
> >dim(mat)
> [1] 10000   10
> 
> >a=mat[,c(seq(c(2,3),ncol(mat),7))]

You could use a logical subscript like
   a <- mat[ , seq_len(ncol(mat))%%7 %in% c(2,3)]

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com  

> Warning messages:
> 1: In if (n < 0L) stop("wrong sign in 'by' argument") :
>   the condition has length > 1 and only the first element will be used
> 2: In if (n > .Machine$integer.max) stop("'by' argument is 
> much too small")
> :
>   the condition has length > 1 and only the first element will be used
> 3: In if (dd < 100 * .Machine$double.eps) return(from) :
>   the condition has length > 1 and only the first element will be used
> 4: In 0L:n : numerical expression has 2 elements: only the first used
> 
> Is there any other way to do it?? Please, help!
> 
> regards
> Lee
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at 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.
> 



More information about the R-help mailing list