Re: [R] Mode Vs Class

From: <Bill.Venables_at_csiro.au>
Date: Tue, 08 Apr 2008 23:58:23 +1000


'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.

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. Received on Tue 08 Apr 2008 - 14:01:06 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 09 Apr 2008 - 11:30:27 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive