Intro to Coding

Part 2: Other kinds of variables

Parts: 1 2 3 X

  • character types (text data)
  • logical types (true/false data)

Other “kinds” of variables

Text as data

In part 1, we worked only with numbers. But lots of data is text (names, colors, species, etc).

Let’s say we want to store the name of our friend, George, in a variable called name.

The following code will not work (try running it):

Remember, computers are dumb. As written, it assumes George is some variable it needs to look up, but it doesn’t know of anything called George. So it throws an error (“Error: object ‘George’ not found”).

However, this code will work (run it – though it won’t print anything):

It didn’t print anything, but it worked. By putting quotes around "George", we are telling R, “This is just some text data, don’t try to interpret it or anything.”

R will now create a variable called name that refers to the text data “George”. You don’t see any output, as R did its job and had nothing more to say. The lack of an error is R’s way of saying “OK, all good.”

Run the cell below to see that the variable is, indeed, set up correctly:

Characters and strings

Each “letter” in a piece of text data is called a “character.” Note that characters are more than just letters: numbers, spaces, punctuation, and anything you can type is a character. So the variable below is valid text data:

This text has 14 total characters. They are: A, l, e, x, , t, h, e, , G, R, E, A, T, and !. Spaces, punctuation, etc are characters too!

Using class to see variable types

If you ever want to know what kind of thing a variable is (number? text? something else?) you can use the class function. We’ll talk more about functions in part 3, but for now just read and try to follow how it’s used:

It should say “numeric.” Which is pretty self-explanatory: 4 is a number.

Now let’s look at the name variable you created a few cells back, which holds the value “Alex the GREAT!” (make sure you ran that cell before running the one below!):

(If you get an error, you forgot to run the cell earlier where we define name)

It should say “character.” Even though “Alex the GREAT!” is actually a bunch of characters, wenever you have a “bunch” of the same kind of thing packed into one variable, R just shows you the base type. This will make more sense when we get to vectors later in this tutorial. So “character” might mean one letter or many.

Now you try:

I have defined (behind the scenes) two mystery variables named foo and bar. Write two lines of code in this cell to figure out which kind of variable each one is:

Reveal answer
class(foo)
class(bar)

This should print “character” then “numeric.” (foo is actually set to the word “cake” while bar is set to the number 17. So this makes sense.)

It can only be this or that (logical variables)

Often, data we care about is binary – this or that, yes or no. For example:

  • You have either graduated from college, or you have not.
  • A store is either open or closed.
  • An airline flight is either delayed or on time.

To see why we should care, let’s imagine a veterinarian’s office that tracks some information about pets in their care:

name species born last_weight spayed_or_neutered
Luna Cat 2021-04-12 9.4 Yes
Max Dog 2019-09-23 52.7 Yes
Pepper Rabbit 2023-01-08 4.2 No
Kiwi Bird 2022-06-17 0.3 No
Charlie Dog 2020-11-05 28.6 Yes


Look at the last column, spayed_or_neutered. It’a always Yes or No. We could leave it this way, but for many reasons, it will be easier to work with in the future if we convert it into a logical variable type, where each value is either TRUE or FALSE. Like this:


name species born last_weight spayed_or_neutered
Luna Cat 2021-04-12 9.4 TRUE
Max Dog 2019-09-23 52.7 TRUE
Pepper Rabbit 2023-01-08 4.2 FALSE
Kiwi Bird 2022-06-17 0.3 FALSE
Charlie Dog 2020-11-05 28.6 TRUE

Note that TRUE and FALSE are not text! See this example:

It says logical. Sometimes called “boolean” values, these get their own type because we use them so much.

Who cares?

Good question! We’re going to leave you hanging here for a second, because we first need to teach you a bit about functions in the final part of this tutorial: