[R] table and unique seems to behave differently

Sarah Goslee @@r@h@go@|ee @end|ng |rom gm@||@com
Tue Dec 10 16:32:39 CET 2019


Back to the table part of the question, but using Duncan's example.

> x <- c(3.4, 3.4 + 1e-15)
> unique(x)
[1] 3.4 3.4
> table(x)
x
3.4
  2

The question was, why are these different.

table() only works on factors, so it converts the numeric vector to a
factor before tabulation.
factor() tries to do something sensible, and implicitly rounds the numeric data.

> factor(x)
[1] 3.4 3.4
Levels: 3.4

Whether you think that is actually sensible or not is up to you, but
if it isn't then you shouldn't use table.

That table uses factors is documented in ?table. A quick read of
?factor didn't find any explicit discussion, other than the
acknowledgement that factor() is lossy in:

     To transform a factor ‘f’ to approximately its
     original numeric values, ‘as.numeric(levels(f))[f]’ is recommended
     and slightly more efficient than ‘as.numeric(as.character(f))’.

You can't even get table() to do what you want by being explicit:

> table(factor(x, levels = unique(x)))
Error in `levels<-`(`*tmp*`, value = as.character(levels)) :
  factor level [2] is duplicated


Sarah

On Tue, Dec 10, 2019 at 9:18 AM Alain Guillet
<alain.guillet using uclouvain.be> wrote:
>
> Hi,
>
> I have a vector (see below the dput) and I use unique on it to get unique values. If I then sort the result of the vector obtained by unique, I see some elements that look like identical. I suspect it could be a matter of rounded values but table gives a different result: unlike unique output which contains "3.4  3.4", table has only one cell for 3.4.
>
> Can anybody know why I get results that look like incoherent between the two functions?
>
>
> Best regards,
> Alain Guillet
>

-- 
Sarah Goslee (she/her)
http://www.numberwright.com



More information about the R-help mailing list