Skip to content
Prev 74867 / 398502 Next

filter data set unique, duplicate..

Hi, Anders/Dimitris,
Dimitris Rizopoulos wrote:
Beware this line. If "x" is not sorted as it is in "dat" then your rows 
will be misaligned.

Here's another solution using "by" though it's no more efficient than 
what Dimitris has given.

out <- by(dat[-1], dat[1], function(y) {
   max.na <- function(x)
     if(all(is.na(x))) NA else max(x, na.rm = TRUE)
   apply(y, 2, max.na)
})
out <- as.data.frame(do.call("rbind", out))
out <- cbind(x = as.numeric(row.names(out)), out)
out

HTH,

--sundar