Skip to content
Prev 170818 / 398506 Next

frequency table for multiple variables

On Tue, Feb 17, 2009 at 10:00:40AM -0600, Marc Schwartz wrote:
Thanks alot, Marc. Neat and efficient, just what I wanted.

BTW, before I saw that you actually included code, I tried on my own,
and wrote this:

my.count <- function(data.frame, levels) {
  result.df <- data.frame(matrix(nrow=length(data.frame),ncol=levels))
  for (i in 1:length(data.frame)) {
    result.df[i,] <- table(factor(data.frame[[i]], levels = c(1:levels)))
  }
  result.df
}

which produces the same result. I take this to be a an instructive
example of unnecessary use of for-loops in R.