Praxeolitic
Praxeolitic

Reputation: 24099

Why can variables with the same type and value print() differently?

Variables that appear to have the same type and value can produce different behavior when given as arguments to the print() function.

#!/usr/bin/env Rscript

a <- quantile(c(1), 1.0)
b <- c(1)

stopifnot(a == b)

print(class(a))
print(a)
print(class(b))
print(b)

The above will produce the following.

[1] "numeric"
100%
   1
[1] "numeric"
[1] 1

Somehow print() knows that a is a quantile and b is not even though this information is not available in their values or in the types reported by class(). What's going on? Is there some sort of additional type information associated with a and b?

My understanding from the R documentation was that a and b are both of type vector with components of the numerical mode and that's all there is to know about their types.

Upvotes: 1

Views: 46

Answers (1)

thelatemail
thelatemail

Reputation: 93938

In addition, try taking a look at:

names(a)
#[1] "100%"

and

attributes(a)
#$names
#[1] "100%"

This has nothing to do with the class or mode or type of object. quantile() has a names= argument that attaches to the output if set, which it is by default, as per ?quantile

names: logical; if true, the result has a ‘names’ attribute.  Set to
       ‘FALSE’ for speedup with many ‘probs’.

Upvotes: 1

Related Questions