Skip to content
Prev 199496 / 398502 Next

Counting non-empty levels of a factor

On Nov 8, 2009, at 9:11 AM, David Winsemius wrote:

            
Which may not have been what you asked for as this would demonstrate.  
You probably wnat the second solution:
mydata2 <- rbind(MyData, MyData)
 > result <- aggregate(mydata2$brand , by=list(mydata2$store) , length)
 > result
   Group.1 x
1       1 6
2       2 4
3       3 6

 > result <- aggregate(mydata2$brand , by=list(mydata2$store) ,  
function(x) nlevels(factor(x)))
 > result
   Group.1 x
1       1 3
2       2 2
3       3 3
David Winsemius, MD
Heritage Laboratories
West Hartford, CT