Hi Felipe, Try
x <- structure(list(Units = c(1L, 1L, 2L, 2L, 2L, 3L, 3L, 4L, 4L, 4L), Var1 = structure(c(2L, 3L, 4L, 3L, 1L, 3L, 1L, 2L, 3L, 1L ), .Label = c("A", "B", "C", "D"), class = "factor"), Var2 = c(2L, 1L, 3L, 2L, 1L, 2L, 1L, 3L, 2L, 1L), Var3 = c(2L, 3L, 3L, 1L, 2L, 1L, 3L, 3L, 2L, 1L)), .Names = c("Units", "Var1", "Var2", "Var3"), class = "data.frame", row.names = c(NA, -10L)) do.call(rbind, lapply(with(x, split(x, Units)), function(d) d[which.max(d[, 3]),])) # Units Var1 Var2 Var3 # 1 1 B 2 2 # 2 2 D 3 3 # 3 3 C 2 1 # 4 4 B 3 3 HTH, Jorge.- On Mon, Jan 16, 2012 at 5:13 PM, Felipe Nunes <> 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@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]] ______________________________________________ R-help@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.