[R]
John Fox
jfox at mcmaster.ca
Mon Mar 31 17:19:35 CEST 2003
At 04:41 PM 3/31/2003 +0200, volkswirt at 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 at mcmaster.ca
phone: 905-525-9140x23604
web: www.socsci.mcmaster.ca/jfox
-----------------------------------------------------
More information about the R-help
mailing list