rstatisticss-plus

In R, what is a good way to aggregate String data


In R (or S-PLUS), what is a good way to aggregate String data in a data frame?

Consider the following:

myList <- as.data.frame(c("Bob", "Mary", "Bob", "Bob", "Joe"))

I would like the output to be:

 [Bob,  3
  Mary, 1
  Joe,  1]

Currently, the only way I know how to do this is with the summary function.

> summary(as.data.frame(myList))

 Bob :3                                
 Joe :1                                
 Mary:1      

This feels like a hack. Can anyone suggest a better way?


Solution

  • Using table, no need to sort:

    ctable <- table(myList);
    counts <- data.frame(Name = names(ctable),Count = as.vector(ctable));