Appendix A. Properties of Variables

R has four different functions that tell you the type of a variable, namely class, typeof, mode, and storage.mode. For some variable types they all give the same answer, but for others they differ, and it gets a little bit complicated.

For the vast majority of code you write, you’ll only ever care about the class. The only time class can’t help you is when you are examining matrices or arrays, and you care about whether the variable contains numbers or characters (or some other type). In this case, you can use one of the other three type functions, or call one of the is.* functions (is.numeric, for example).

Table A-1 shows the values returned by class, typeof, mode, and storage.mode for various variable types.

Table A-1. Comparison of variable class, type, mode, and storage mode

class typeof mode storage.mode

Logical

logical

logical

logical

logical

Integer

integer

integer

numeric

integer

Floating Point

numeric

double

numeric

double

Complex

complex

complex

complex

complex

String

character

character

character

character

Raw byte

raw

raw

raw

raw

Categorical

factor

integer

numeric

integer

Null

NULL

NULL

NULL

NULL

Logical Matrix

matrix

logical

logical

logical

Numeric Matrix

matrix

double

numeric

double

Character Matrix

matrix

character

character

character

Logical Array

array

logical

logical

logical

Numeric Array

array

double

numeric

double

Character Array

array

character

character

character

List

list

list

list

list

Data Frame

data.frame

list

list

list

Function

function

closure

function

function

Environment

environment

environment

environment

environment

Expression

expression

expression

expression

expression

Call

call

language

call

language

Formula

formula

language

call

language

In R, vectors are variable types with a length, but no dimension (that is, dim returns NULL) and no attributes other than names. Vector types include numeric, logical, and character types, but also lists and expressions. The rule about no attributes means that factors are not vectors.

Tip

Lists are vectors. Factors are not vectors.

Related to vectors are atomic types. Atomic means that a type cannot contain other instances of that type within itself. The opposite of atomic is recursive: lists are the canonical example, since they can contain other lists. An object can only ever be atomic or recursive, never both.

Tip

Matrices and arrays are atomic.

Some objects are known as language objects. These variable types can be evaluated to run R code.

Table A-2 shows the values returned by is.vector, is.atomic, is.recursive, and is.language for various variable types.

Table A-2. Comparison of which variable types are vectors, atomic, recursive, or language objects

is.vectoris.atomicis.recursiveis.language

Logical

TRUE

TRUE

FALSE

FALSE

Integer

TRUE

TRUE

FALSE

FALSE

Floating Point

TRUE

TRUE

FALSE

FALSE

Complex

TRUE

TRUE

FALSE

FALSE

String

TRUE

TRUE

FALSE

FALSE

Raw Byte

TRUE

TRUE

FALSE

FALSE

Categorical

FALSE

TRUE

FALSE

FALSE

Null

FALSE

TRUE

FALSE

FALSE

Logical Matrix

FALSE

TRUE

FALSE

FALSE

Numeric Matrix

FALSE

TRUE

FALSE

FALSE

Character Matrix

FALSE

TRUE

FALSE

FALSE

Logical Array

FALSE

TRUE

FALSE

FALSE

Numeric Array

FALSE

TRUE

FALSE

FALSE

Character Array

FALSE

TRUE

FALSE

FALSE

List

TRUE

FALSE

TRUE

FALSE

Data Frame

FALSE

FALSE

TRUE

FALSE

Function

FALSE

FALSE

TRUE

FALSE

Environment

FALSE

FALSE

TRUE

FALSE

Expression

TRUE

FALSE

TRUE

TRUE

Call

FALSE

FALSE

TRUE

TRUE

Formula

FALSE

FALSE

TRUE

TRUE

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset