1. Data

R can deal with many types of data, like numbers (3, 0, 2021), letters and words called characters (“Data”, “Gov 50”, “Professor Lo”), and many many others. For example, in the grey box below this message surrounded by the “`” character, type 3 and press the green arrow (or CMD + Enter on a Mac / Ctrl + Enter on PC):

3
## [1] 3

That was code! R knows that 3 is a number, and it simply reported it back to you. The [1] means that one value was reported back to you.

R is also very good at math. Try typing 3 + 10:

3 + 10
## [1] 13

Amazing! R can also do subtraction, multiplication, and division like you’re familiar with:

3 - 10
## [1] -7
3 * 10
## [1] 30
3 / 10
## [1] 0.3
-3 + 4
## [1] 1
(3 + 6) * 2
## [1] 18

You can also type letters and words as long as they are between two quotes "like this". In the console, type something like:

"This is a sentence!"
## [1] "This is a sentence!"

R can also use Boolean values, which are TRUE or FALSE depending on how a statement is evaluated. For example, == (two equal signs) will tell you whether or not the value on the left is equal to the value on the right.

3 == 3
## [1] TRUE
3 == 7
## [1] FALSE
3 > 7
## [1] FALSE
3 < 100
## [1] TRUE

!= can be used to see if one value is not equal to something else. For example:

3 != 4
## [1] TRUE
3 == 4
## [1] FALSE
5 %% 2 != 0
## [1] TRUE

Exercises to do together

  1. Divide 64 by 4.

  2. Check if 40 is equal to 23 plus 17 below.

  3. Find the remainder of 8135 divided by 13


2. Comments

Programming is difficult and code can be hard to understand. To make your life easier, you should always comment your code. This helps you and others to understand what’s going on. Comments also make it much easier to return to some code later and understand what it’s doing.

You can write comments with the # sign followed by normal text. This will look like code but it will not be evaluated as code:

# Average of two prices for microwaves on Amazon
(29 + 60) / 2
## [1] 44.5

3. Objects

Sometimes, you want to store data for later. An object in R will store data for you. For example, imagine you want to keep the value of a complicated calculation:

36 * 1527
## [1] 54972

Instead of trying to remember that number, you can store that data in an object. Then, the data will be there whenever you want it. In R, we make objects with the <- operator. Think of <- like an arrow, it stores the data on the right in the name on the left. In general, you can give objects any name you want. For example:

number <- 36 * 1527

Wait, nothing happened? This is because your answer is now stored in an object called number. Type number in the console and your answer will come back!

number
## [1] 54972

Exercises

  1. Create an object named answer with the value 15. Then, divide answer by 3.

  2. Use == to see whether answer is equal to 12.

  3. Assign your name to an object called name.

  4. Try adding 1 to the object containing your name. What happens?


4. Functions

Some operations are a little more complex than simple math like addition. A function is a pre-written piece of code that somebody else wrote for you to perform a specific task. For example, there will be functions for things like making plots. We will use many functions in this course. We will even learn how to write our own functions!

For example, how would you find the square root of a number? Well, thankfully there is a function called sqrt(). To run it, you should type sqrt() into the console and put the number that you want to find the square root of between the parentheses:

sqrt(9)
## [1] 3

This is how all functions will work:

Other functions include length(), min(), max(), range(), mean(), and sum().

Functions can take objects as their argument as well. Remember our object number? Check it out!

sqrt(number)
## [1] 234.4611

You can store the output of a function into an object as well. For example:

answer <- sqrt(number)

To see the result, just type in the name of your object:

answer
## [1] 234.4611

You can also perform math on objects just like we did earlier. Look, multiplying the square root times itself should get us back the original number:

answer * answer
## [1] 54972

Is this the same as number?

number
## [1] 54972
number == answer * answer
## [1] TRUE

It worked!

Tip: To learn how a function works, you can type ? followed by the name of the function (e.g. ?sqrt) in the console and press enter to open the documentation.

?sqrt

For example, the paste() function will connect two strings together:

name <- "Tyler"
paste("Hello", name)
## [1] "Hello Tyler"

Exercises

  1. Create an object called number with the value 1444. What is the square root of number?

5. Objects with Multiple Values

Objects can also store more than one value at once. Vectors are one very common way to store data. To create a vector, we use the function c() (the “c” stands for combine, because it combines multiple values separated by a comma). Just like before, you can use the <- syntax to create an object.

grades <- c(100, 95, 85)
names  <- c("Emma", "Bonnie", "Connor")

Some functions take vectors as arguments because they are meant to work with multiple values. For example, the mean() function will find the average of the values in a vector:

mean(grades)
## [1] 93.33333

We’ve already learned a lot of terminology. Above, we used a vector called grades as an argument in a function called mean().

Exercises

  1. Create a vector called prices with the values 12, 11, and 8. Find the average price.

  2. What happens if you run prices + 1? What is this doing? How about prices == 8?

For a few more exercises, try this link.