Chapter 3. Inspecting Variables and Your Workspace

So far, we’ve run some calculations and assigned some variables. In this chapter, we’ll find out ways to examine the properties of those variables and to manipulate the user workspace that contains them.

Chapter Goals

After reading this chapter, you should:

  • Know what a class is, and the names of some common classes
  • Know how to convert a variable from one type to another
  • Be able to inspect variables to find useful information about them
  • Be able to manipulate the user workspace

Classes

All variables in R have a class, which tells you what kinds of variables they are. For example, most numbers have class numeric (see the next section for the other types), and logical values have class logical. Actually, being picky about it, vectors of numbers are numeric and vectors of logical values are logical, since R has no scalar types. The “smallest” data type in R is a vector.

You can find out what the class of a variable is using class(my_variable):

class(c(TRUE, FALSE))
## [1] "logical"

It’s worth being aware that as well as a class, all variables also have an internal storage type (accessed via typeof), a mode (see mode), and a storage mode (storage.mode). If this sounds complicated, don’t worry! Types, modes, and storage modes mostly exist for legacy purposes, so in practice you should only ever need to use an object’s class (at least until you join the R Core Team). Appendix A has a reference table showing the relationships between class, ...

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.