[R] how to fetch rows with certain characteristics

Ista Zahn istazahn at gmail.com
Thu Oct 29 20:10:44 CET 2009


I still don't understand. Please look at the example I provided in my
last response. What is it that you want that my example does not do?

On Thu, Oct 29, 2009 at 3:08 PM, Waverley @ Palo Alto
<waverley.paloalto at gmail.com> wrote:
> The reason %in% does not work is that there are might be values which
> are not min in other class which are the same as the min of different
> classes.  In the example I provided before, this situation did not
> exist.  See the new example:
>
>> + 1.2 1
>> + 1.3 1
>> + 1.5 1
>> + 1.1 2
>> + 1.2 2
>> + 9.9 2
>> + 0.1 3
>> + 1.1 3
>> + 1.9 3
>
> if you are using %in%, then
> 1.2 2
> 1.1 3
> will also show up in the final result.
>
>
> That is why I need those row index of the min value of each class. If
> I can get those, that would be best.
> Thanks.
>
> On Thu, Oct 29, 2009 at 11:58 AM, Ista Zahn <istazahn at gmail.com> wrote:
>> Hi,
>> I guess I don't understand why you think %in% won't work.
>>
>>> x <- read.table(textConnection("1.2 1
>> + 1.2 1
>> + 1.3 1
>> + 1.5 1
>> + 2.1 2
>> + 2.0 2
>> + 9.9 2
>> + 1.4 3
>> + 1.8 3
>> + 1.9 3") )
>>> x <- as.matrix(x)
>>> x.min <- tapply(x[,1], x[,2], min)
>>> x[x[,1] %in% x.min,]
>>> ## all matches
>>      V1 V2
>> [1,] 1.2  1
>> [2,] 1.2  1
>> [3,] 2.0  2
>> [4,] 1.4  3
>>> ## unique matches
>>> unique(x[x[,1] %in% x.min,])
>>      V1 V2
>> [1,] 1.2  1
>> [2,] 2.0  2
>> [3,] 1.4  3
>>
>> -Ista
>> On Thu, Oct 29, 2009 at 12:36 PM, Waverley @ Palo Alto
>> <waverley.paloalto at gmail.com> wrote:
>>> Thanks.  That works.
>>>
>>> However, in my own case, there are more columns of other kinds of
>>> data.  So to me, it is more important to get the row index of those
>>> that has the min values of particular column in particular class
>>> (which is another column).
>>>
>>> Can you help more as how to get those row index? One issue is that for
>>> some class they may share the same min value so that using %in% does
>>> not work.  My goal is to reduce the original matrix size and get the
>>> result back in the original matrix format.
>>>
>>>
>>> Thanks.
>>>
>>> On Wed, Oct 28, 2009 at 11:55 PM, Ista Zahn <istazahn at gmail.com> wrote:
>>>> There are various ways, including
>>>>
>>>> x <- read.table(textConnection("1.2 1
>>>> + 1.3 1
>>>> + 1.3 1
>>>> + 1.5 1
>>>> + 2.1 2
>>>> + 2.0 2
>>>> + 9.9 2
>>>> + 1.4 3
>>>> + 1.8 3
>>>> + 1.9 3") )
>>>>
>>>> x <- as.matrix(x)
>>>>
>>>> x.min <- cbind(tapply(x[,1], x[,2], min), unique(x[,"V2"]))
>>>>
>>>> Most of that is just formatting it in the way you requested. All you
>>>> need to compute the values is
>>>>
>>>> tapply(x[,1], x[,2], min)
>>>>
>>>> -Ista
>>>>
>>>> On Thu, Oct 29, 2009 at 1:47 AM, Waverley @ Palo Alto
>>>> <waverley.paloalto at gmail.com> wrote:
>>>>> Hi,
>>>>>
>>>>> I have a matrix, first column is of certain values, second column is
>>>>> the class labels or a factor.
>>>>> e.g.
>>>>>
>>>>> 1.2 1
>>>>> 1.3 1
>>>>> 1.3 1
>>>>> 1.5 1
>>>>> 2.1 2
>>>>> 2.0 2
>>>>> 9.9 2
>>>>> 1.4 3
>>>>> 1.8 3
>>>>> 1.9 3
>>>>>
>>>>> I want to find out what is the min values of column 1 for each
>>>>> corresponding class (column 2).  For the above example, I want to
>>>>> return a matrix of
>>>>> 1.2 1
>>>>> 2.0 2
>>>>> 1.3 3
>>>>>
>>>>> Can someone suggest how to code for that?  The second column can be of
>>>>> characters.
>>>>>
>>>>> Thanks much.
>>>>>
>>>>>
>>>>> --
>>>>> Waverley @ Palo Alto
>>>>>
>>>>> ______________________________________________
>>>>> 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.
>>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> Ista Zahn
>>>> Graduate student
>>>> University of Rochester
>>>> Department of Clinical and Social Psychology
>>>> http://yourpsyche.org
>>>>
>>>
>>>
>>>
>>> --
>>> Waverley @ Palo Alto
>>>
>>
>>
>>
>> --
>> Ista Zahn
>> Graduate student
>> University of Rochester
>> Department of Clinical and Social Psychology
>> http://yourpsyche.org
>>
>
>
>
> --
> Waverley @ Palo Alto
>



-- 
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org




More information about the R-help mailing list