Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Types and classes of variables

Tags:

types

class

r

Two R questions:

  1. What is the difference between the type (returned by typeof) and the class (returned by class) of a variable? Is the difference similar to that in, say, C++ language?
  2. What are possible types and classes of variables?
like image 223
Leo Avatar asked Jun 06 '11 21:06

Leo


People also ask

What are the 4 types of variables?

Introduction to Types of Variables in Statistics Such variables in statistics are broadly divided into four categories such as independent variables, dependent variables, categorical and continuous variables.

What are the 3 general classes of variables?

There are three types of categorical variables: binary, nominal, and ordinal variables.

What are the 5 Classification of variables?

There are different types of variables and having their influence differently in a study viz. Independent & dependent variables, Active and attribute variables, Continuous, discrete and categorical variable, Extraneous variables and Demographic variables.

What are the 6 types of variables?

In all there are six basic variable types: dependent, independent, intervening, moderator, controlled and extraneous variables.


1 Answers

In R every "object" has a mode and a class. The former represents how an object is stored in memory (numeric, character, list and function) while the later represents its abstract type. For example:

d <- data.frame(V1=c(1,2)) class(d) # [1] "data.frame" mode(d) # [1] "list" typeof(d) # list 

As you can see data frames are stored in memory as list but they are wrapped into data.frame objects. The latter allows for usage of member functions as well as overloading functions such as print with a custom behavior.

typeof(storage.mode) will usually give the same information as mode but not always. Case in point:

typeof(c(1,2)) # [1] "double" mode(c(1,2)) # [1] "numeric" 

The reasoning behind this can be found here:

The R specific function typeof returns the type of an R object

Function mode gives information about the mode of an object in the sense of Becker, Chambers & Wilks (1988), and is more compatible with other implementations of the S language

The link that I posted above also contains a list of all native R basic types (vectors, lists etc.) and all compound objects (factors and data.frames) as well as some examples of how mode, typeof and class are related for each type.

like image 59
diliop Avatar answered Sep 27 '22 16:09

diliop