[R] Mode Vs Class

Bill.Venables at csiro.au Bill.Venables at csiro.au
Tue Apr 8 15:58:23 CEST 2008


'mode' is a mutually exclusive classification of objects according to
their basic structure.  The 'atomic' modes are numeric, complex,
charcter and logical.  Recursive objects have modes such as 'list' or
'function' or a few others.  An object has one and only one mode.

'class' is a property assigned to an object that determines how generic
functions operate with it.  It is not a mutually exclusive
classification.  If an object has no specific class assigned to it, such
as a simple numeric vector, it's class is usually the same as its mode,
by convention.

Changing the mode of an object is often called 'coercion'.  The mode of
an object can change without necessarily changing the class.  e.g.

> x <- 1:16
> mode(x)
[1] "numeric"
> dim(x) <- c(4,4)
> mode(x)
[1] "numeric"
> class(x)
[1] "matrix"
> is.numeric(x)
[1] TRUE
> mode(x) <- "character"
> mode(x)
[1] "character"
> class(x)
[1] "matrix"

However:

> x <- factor(x)
> class(x)
[1] "factor"
> mode(x)
[1] "numeric"
> 

At this stage, even though x has mode numeric again, its new class,
'factor', inhibits it being used in arithmetic operations.

In practice, mode is not used very much, other than to define a class
implicitly when no explicit class has been assigned. 

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of Shubha Vishwanath Karanth
Sent: Tuesday, 8 April 2008 10:20 PM
To: r-help at stat.math.ethz.ch
Subject: [R] Mode Vs Class

Hi R,

Just came across the 'mode' of an object. What is the basic difference
between ?class and ?mode ... For example:

d <- data.frame(a = c(1,2), b = c(5,6))

class(d)

[1] "data.frame"

mode(d)

[1] "list"

But,

c <- c(2,3,5,6,7)

class(c)

[1] "numeric"

mode(c)

[1] "numeric" 

Could anyone help me out... 

Thanks,

shubha
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list