Skip to content

sum(table(v)) == length(v)

2 messages · Sam Steingold, R. Michael Weylandt

#
Is it possible to violate the identity sum(table(v)) == length(v) ??
I see no way to do that and it holds in my small examples,
but it is violated in the huge set I have:

system.time(z <- unique(data.frame(u=U,s=S)))
tab1 <- table(z$u)
tab1 <- tab1[tab1>0] # S is factor so some counts were 0
tab2 <- table(z$s)
stopifnot(length(tab2) == nrow(z)) # yes
stopifnot(sum(tab1) == nrow(z))  ### no!
sum(tab1)
728587
length(tab1)
503374
length(tab2)
2112951
#
On Sun, Sep 16, 2012 at 7:34 PM, Sam Steingold <sds at gnu.org> wrote:
Quite easily:

x <- c(1:5, NA)

sum(table(x)) # 5

length(x) # 6

Perhaps look at the exclude= argument.

Cheers,
Michael