[R] how to get the rows that satisfy a specific condition

Fix Ace acefix at rocketmail.com
Tue Sep 30 05:35:50 CEST 2014


Sorry for my confusing question. Thanks for all the inputs. I think Sven E. Templer gave the answer I needed...


 

     On Sunday, September 28, 2014 4:51 AM, Sven E. Templer <sven.templer at gmail.com> wrote:
   

 in ?which read about arr.ind

following jims assumption (column instead of row indices is what you
want) this also works:

m <- matrix(1:20,4)
unique(which(m>11, arr.ind = T)[,"col"])

On 27 September 2014 12:23, Jim Lemon <jim at bitwrit.com.au> wrote:
> On Fri, 26 Sep 2014 10:15:14 PM Fix Ace wrote:
>> Hello, there,
>> I wonder if there is an easier way that I would only get the rows that
>> satisfies some condition. For example:I have the following matrix, and I
>> would like to output only the 3rd row and 4th row, since only these two
>> rows contain the numbers greater than 11
>> > a
>>
>>      [,1] [,2] [,3] [,4] [,5]
>> [1,]    1    5    9  13  17
>> [2,]    2    6  10  14  18
>> [3,]    3    7  11  15  19
>> [4,]    4    8  12  16  20
>>
>> > a>11
>>
>>      [,1]  [,2]  [,3] [,4] [,5]
>> [1,] FALSE FALSE FALSE TRUE TRUE
>> [2,] FALSE FALSE FALSE TRUE TRUE
>> [3,] FALSE FALSE FALSE TRUE TRUE
>> [4,] FALSE FALSE  TRUE TRUE TRUE
>> I have tried to use a[a>11, ] and it did not work.
>> Thanks a lot for the help:)
>
> Hi Fix Ace,
> I have to admit that I am unfamiliar with the system of arithmetic that
> you are employing in the above example. In no system with which I am
> conversant are 13, 17, 14 and 18 less than or equal to 11. I can only
> offer the desperate conjecture that you want the third to fifth columns of
> the matrix rather than the third and fourth rows. If this wild surmise
> happens to be the case, I suggest that you try this:
>
> testmat[,apply(testmat,2,function(x) return(max(x) > 11))]
>
> Jim
>
> ______________________________________________
> 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.


   
	[[alternative HTML version deleted]]



More information about the R-help mailing list