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

From: My Coyne <mcoyne_at_boninc.com>
Date: Tue, 15 Jan 2008 17:37:14 -0500

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

This works great. I'm learning this R slowly... It is quite powerful.

Thanks

-----Original Message-----
From: Charles C. Berry [mailto:cberry_at_tajo.ucsd.edu] Sent: Tuesday, January 15, 2008 3:47 PM
To: My Coyne
Cc: r-help_at_stat.math.ethz.ch
Subject: RE: [R] Newbie Question -- Remove entire column in a matrix

On Tue, 15 Jan 2008, My Coyne wrote:

> Really Cool. THANK YOU
>
> Both works, only that New.mat is t is being transposed. So, I added a
> minor change:
>
> New.mat <- matrix (mat [,colSums(mat==0) == 0], byrow = TRUE)

I do not think that is what you want. If there are k columns after subscripting mat, the result will be a k*nrow(mat) by 1 matrix.

The code below does not transpose the result.

It will return a vector if only one column has colSums( mat==0 ) == 0.

If a vector is not what you want, use the drop=FALSE argument

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

Chuck

>
>
>
>
>
>
> -----Original Message-----
> From: Charles C. Berry [mailto:cberry_at_tajo.ucsd.edu]
> Sent: Tuesday, January 15, 2008 12:34 PM
> To: My Coyne
> Cc: r-help_at_stat.math.ethz.ch
> Subject: Re: [R] Newbie Question -- Remove entire column in a matrix
>
> 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
>
>
>
>

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

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. Received on Tue 15 Jan 2008 - 22:43:46 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 15 Jan 2008 - 23:30:07 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive