Skip to content
Back to formatted view

Raw Message

Message-ID: <87sjae0z5h.fsf@gnu.org>
Date: 2012-09-19T16:12:58Z
From: Sam Steingold
Subject: drop zero slots from table?
In-Reply-To: <87392e2eqb.fsf@gnu.org> (Sam Steingold's message of "Wed, 19 Sep 2012 11:51:08 -0400")

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.