Skip to content
Prev 315088 / 398506 Next

aggregate data.frame based on column class

Please see in line.

On Fri, Jan 11, 2013 at 10:07 AM, Martin Batholdy
<batholdy at googlemail.com> wrote:
R is a language, so you just have to do the translation:

mt <- function(x) {
  if(is.numeric(x)) { # if x is numeric
    return(mean(x)) # compute the mean
  } else { # otherwise
    tab <- table(x) # tabulate x
    return(paste(paste(names(tab), # and format it for display
                       tab, sep=": "),
                 collapse=", "))
  }
}

aggregate(Dat, by=list(Dat$group), FUN=mt)

Best,
Ista