[R] how to fetch rows with certain characteristics
Waverley @ Palo Alto
waverley.paloalto at gmail.com
Thu Oct 29 17:36:33 CET 2009
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
More information about the R-help
mailing list