6.1 Data

The central component of everything we will be doing in R this semester is data. Even non-data science programs revolve around data. At a very basic level, a computer is just a fancy calculator that takes in numbers and spits out an answer. To a computer, even things like words and pictures are stored internally as numbers.

6.1.1 Numbers

R can handle many types of data. For example, one type of data is a number. Let’s start by

When you open RStudio, you will notice that the program is divided up into 3 or 4 panes. On the left, you should see a pane called Console. You can type programming language expressions into the Console after the > symbol, hit enter, and the expression will be evaluated.

Try entering a number after the > in the Console (e.g. 2), then Enter, and see what happens.

When you hit enter, the R interpreter reads in the line, evaluates it, and returns the answer. In this case, you entered 2, so the computer thinks “Hey, it’s a 2! Wow, a two! The result of 2 is… drum roll, please… 2!” and you get a two.

Cool! But not, I confess, particularly useful. Let’s fix that: next we’ll add two numbers together. At the prompt, enter two numbers separated by a plus sign, +

> 2 + 2

What do you get?

The plus sign, +, is called an operator. As you might expect, it is the addition operator. There are many other operators in R. Here are a few of the more useful ones. See if you can figure out what they do:

  • -
  • /
  • *
  • ^
  • %%
  • ==
  • !=
  • <

Enter each of these at the prompt, with a number on each side, and see what you get, e.g.

> 3 ** 2

or

> 5 == 5