[R] split-apply question

Henrique Dallazuanna wwwhsd at gmail.com
Fri Oct 2 14:49:30 CEST 2009


You can use aggregate:

aggregate(x[,c('x2','x3')], x['x1'], min)

On Fri, Oct 2, 2009 at 12:43 AM, Kavitha Venkatesan
<kavitha.venkatesan at gmail.com> wrote:
> Hi,
>
> I have a data frame that looks like this:
>
>>x
>
> x1  x2  x3
> A   1    1.5
> B   2    0.9
> B   3    2.7
> C   7    1.8
> D   7    1.3
>
> I want to "group" by the x1 column and in the case of multiple x$x1 values
> (e.g., "B")d, return rows that have the smallest values of x2. In the case
> of rows with only one value of x1 (e.g., "A"), return the row as is. How can
> I do that?  For example, in the above case, the output I want would be:
>
> x1  x2  x3
> A   1    1.5
> B   2    0.9
> C   7    1.8
> D   7    1.3
>
>
> Thanks!
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O




More information about the R-help mailing list