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 |
|
|
|
|
Integer |
|
|
|
|
Floating Point |
|
|
|
|
Complex |
|
|
|
|
String |
|
|
|
|
Raw byte |
|
|
|
|
Categorical |
|
|
|
|
Null |
|
|
|
|
Logical Matrix |
|
|
|
|
Numeric Matrix |
|
|
|
|
Character Matrix |
|
|
|
|
Logical Array |
|
|
|
|
Numeric Array |
|
|
|
|
Character Array |
|
|
|
|
List |
|
|
|
|
Data Frame |
|
|
|
|
Function |
|
|
|
|
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.