What’s a variable in R?

I had a great discussion with a bunch of 9-11 year olds about what a variable is the other day. It occurred to me that I was coding for years before I understood what was ‘under the hood’ so to speak.

Here’s a quick rundown of variables in R.

Variables

We use the word ‘variable’ to describe things that can change, or that could take multiple values. In Excel, typically this is a column with a heading giving the variable name.

In R, it might be a column in a data frame we access with a name or using subscripts. Or it might be a standalone object.

Using variables

We can access and use variables a few different ways in R:
– If it’s a stand alone object, we can just call it by name.
– If it’s a part of a data frame, we can use the $ notation
– Alternatively, we can use subscripts

But what’s going on under the hood?

A variable is just a type of object – you can think of an object in code as just a thing with a name.
– R puts the ‘thing’ in a part of the computer’s memory
– It labels that part of the memory with the variable’s name.
– We can later update that object in memory with new values, or point to the same object using a different name.

If we code fruit <- ‘apple’, then the computer puts ‘apple’ somewhere in its memory and points to that using the labelĀ fruit.

If we code awesomeR <- TRUE, the computer puts TRUE somewhere in its memory and points to that using the label awesomeR.

If we code x <- 45, the computer puts 45 somewhere in its memory and points to it using the label x.

An infographic about variables in R.