Re: [R]

About this list Date view Thread view Subject view Author view Attachment view

From: John Fox (jfox@mcmaster.ca)
Date: Tue 01 Apr 2003 - 01:19:35 EST


Message-id: <5.1.0.14.2.20030331101555.01e77420@mcmail.cis.mcmaster.ca>

At 04:41 PM 3/31/2003 +0200, volkswirt@gmx.net wrote:
>How can I remove all rows/collumns from a matrix that fulfill a certain
>condition? For instance: remove all negative elements from a [n,1]-matrix.
>I intended to write a function for this aim, but it does not work. Despite
>that it may help to get my point, when I ad it:
>
>kratek<-function(eine)
>{
>halt<-0
>for(index in 1:dim(eine)[1])
>{
>if(eine[(index-halt),1]<=0)
>{
>eine<-eine[-(index-halt),]
>halt<-halt+1
>}
>}
>eine
>}
>

I'm not entirely sure that I understand what you want, but perhaps the
following will help: Given a matrix A, the command A[,apply(A, 2,
function(x) all(x>=0))] will remove columns with negative elements, and
A[apply(A, 1, function(x) all(x>=0)),] will remove rows with negative elements.

Does that help?
  John

-----------------------------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario, Canada L8S 4M4
email: jfox@mcmaster.ca
phone: 905-525-9140x23604
web: www.socsci.mcmaster.ca/jfox
-----------------------------------------------------

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help


About this list Date view Thread view Subject view Author view Attachment view

This archive was generated by hypermail 2.1.3 : Tue 01 Jul 2003 - 09:11:33 EST