R Tutorial: How does R Distinguish Variables?

Richard Cotton shows how to identify object types in R using class(), typeof(), and str(). class() tells you what an object is like a data frame or matrix, while typeof() reveals the underlying storage type such as integer versus double. mode() and storage.mode() are legacy functions kept for old code compatibility.
Chapters
You've already seen the STR function for examining the structure of a variable. For example, if you call STR on the sleep data set, you can see that it's a data frame with 20 rows and three columns. And you can see some of the details of each column. Now you're going to see some more functions that interrogate your variables to determine their type. The most important of these functions is is called class. Going back to the sleep data set, if you call class on it, then you see the obvious result. That is the sleep variable has class data frame, just as you saw when you called the STR function. Most of the time, class is the only function that you need to use to interrogate the type of your variable. Sometimes, however, you need to dig a little deeper and there are three more functions to consider. Let's take a look at a matrix. Notice that the class of this object is matrix. This is what you might expect and it's useful to know, but it doesn't tell you everything. For example, there are different kinds of matrix. As well as numeric matrices, you also have logical or character matrices. So how do you find out what the matrix contains? In this case, you can use the typeof function. Here you can see that the type is integer. That is you have an integer matrix. Now let's look at a numeric matrix. This time, rather than containing integers, you have floating point numbers. Again, of course, the matrix has class matrix, but now when you call TypeOf, the return value is double. This is because TypeOf returns the type of variable that R understands in its internal C language code. In C, floating point numbers are called doubles. So that's what typeof returns. Here, the important point is that you can use TypeOf to precisely determine the contents of your variable, regardless of its class. This is especially useful once you start working with S3. There are two other functions for determining the type of object that you have, namely mode and storage mode. These functions exist solely for compatibility with older S code. So while you need to know that they exist, you should never need to use them. To summarise, most of the time you should use the class function to determine what kind of variable you have. Occasionally, TypeOf is useful as well. You may come across mode and storage mode in older code, but you shouldn't need to use them yourself. Now let's try some examples.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







