[R] Select rows based on multiple comparisons

Joshua Wiley jwiley.psych at gmail.com
Mon Jan 16 23:24:41 CET 2012


Hi,

Here's one option:


dat <- read.table(text = "Units  Var1 Var2 Var3
  1          B       2        2
  1          C       1       3
  2          D      3        3
  2          C      2        1
  2          A      1        2
  3          C      2        1
  3          A      1        3
  4          B      3        3
  4          C      2        2
  4          A      1        1", header = TRUE)


dat2 <- dat[as.logical(ave(dat$Var2, dat$Units, FUN = function(x) x ==
max(x))), ]

dat2

Cheers,

Josh

On Mon, Jan 16, 2012 at 2:13 PM, Felipe Nunes <felipnunes at gmail.com> wrote:
> Dear all,
>
> I have a data set in which the same unit appears 2 or 3 or 4 times. I need
> to aggregate this data to maintain only one unit by row. But I need to do
> that based on a comparison between the values of such units. I can't find a
> function to help me on that. I appreciate any help. Below I provide an
> example of what I want:
>
> This is my data:
>
> Units  Var1 Var2 Var3
>  1          B       2        2
>  1          C       1       3
>  2          D      3        3
>  2          C      2        1
>  2          A      1        2
>  3          C      2        1
>  3          A      1        3
>  4          B      3        3
>  4          C      2        2
>  4          A      1        1
>
> I want to produce a new data in which I keep, for each unit, the row with
> max number for Var2. For instance,
>
> Units  Var1 Var2 Var3
>  1          B       2       2
>  2          D      3        3
>  3          C      2        1
>  4          B      3        3
>
> Thanks,
>
>
> *Felipe Nunes*
> CAPES/Fulbright Fellow
> PhD Student Political Science - UCLA
> Web: felipenunes.bol.ucla.edu
>
>        [[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.



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
Programmer Analyst II, Statistical Consulting Group
University of California, Los Angeles
https://joshuawiley.com/



More information about the R-help mailing list