[R] drop zero slots from table?

Sam Steingold sds at gnu.org
Wed Sep 19 18:12:58 CEST 2012


Function
--8<---------------cut here---------------start------------->8---
sorted.table <- function (vec) {
  tab <- table(vec)
  tab <- tab[tab > 0]
  sort(tab, decreasing=TRUE)
}
--8<---------------cut here---------------end--------------->8---
does what I want but it prints "vec" instead of the name of its
argument:
--8<---------------cut here---------------start------------->8---
> sorted.table(foo$bar)
vec
  A  B
  10 3
--8<---------------cut here---------------end--------------->8---
how do I pass all arguments of sorted.table() on to table() as is?
thanks!

> * Sam Steingold <fqf at tah.bet> [2012-09-19 11:51:08 -0400]:
>
> I find myself doing
> tab <- table(...)
> tab <- tab[tab > 0]
> tab <- sort(tab,decreasing=TRUE)
> all the time.
> I am wondering if the "drop 0" (and maybe even sort?) can be effected by
> some magic argument to table() which I fail to discover in the docs?
> Obviously, I could use droplevels() to avoid 0 counts in the first
> place, but I do not want to drop the levels in the data.

-- 
Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000
http://www.childpsy.net/ http://memri.org http://thereligionofpeace.com
http://www.PetitionOnline.com/tap12009/ http://dhimmi.com
Beauty is only a light switch away.




More information about the R-help mailing list