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

Get Learning R now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.