[R] Newbie Question -- Remove entire column in a matrix

Charles C. Berry cberry at tajo.ucsd.edu
Tue Jan 15 18:33:40 CET 2008


On Tue, 15 Jan 2008, My Coyne wrote:

>
>
> I'm ordering "The R Book" and hope to learn a lot more about R. In a
> meantime, I have a matrix of digits and I would like to look for a column
> that contains zeroes and remove the entire column from the matrix.  I can
> write a piece of R code to do that (and it works); however, I think the
> R-experts  have more tricks to do such a 'simple' thing.

Is one of these what you want?

 	new.mat <- mat[ , colSums( mat==0 ) == 0 ]

 	new.mat <- mat[ , colSums( mat==0 ) < nrow(mat) ]


HTH,

Chuck

>
>
>
> Many thanks in advance.
>
>
>
> --My Coyne
>
>
>
>
>
>
> 	[[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.
>

Charles C. Berry                            (858) 534-2098
                                             Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu	            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901




More information about the R-help mailing list