Throughout this entire course, you will use code to do things with data. R can deal with many types of data, like numbers (3, 0, 2021), letters and words called characters (“Data”, “South Amboy”, “Tyler”), and many many others. For example, go to your console (the panel called “Console” with the >
character at the bottom) and type:
## [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.
You can also type letters and words as long as they are between two quotes "like this"
. In the console, type something like:
## [1] "This is a sentence!"
R is also very good at math. Try typing:
## [1] 13
Amazing! R can also do subtraction, multiplication, and division like you’re familiar with:
## [1] -7
## [1] 30
## [1] 0.3
## [1] 1
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.
## [1] TRUE
## [1] FALSE
!=
can be used to see if one value is not equal to something else. For example:
## [1] TRUE
## [1] FALSE
Try evaluating a few mathematical expressions in the box below. In an RMarkdown file, you can evaluate the code by clicking on the green arrow (or CMD + Enter on a Mac / Ctrl + Enter on PC):
Write something in quotes below and run the code:
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:
## [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:
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!
## [1] 54972
Create an object named year
with the value 2021.
Create an object named answer
with the value 15. Then, divide answer
by 3.
Use ==
to see whether answer
is equal to 12.
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:
## [1] 3
This is how all functions will work:
sqrt()
.sqrt(9)
). The value you give to the function (9
) is called an argument.Functions can take objects as their argument as well. Remember our object number
? Check it out!
## [1] 234.4611
You can store the output of a function into an object as well. For example:
To see the result, just type in the name of your object:
## [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:
## [1] 54972
Is this the same as number
?
## [1] 54972
It worked!
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.
Create an object called number
with the value 1444. What is the square root of number
?
Create an object called name
with your name. Now, go in the console and type ?abs
to open the documentation for the abs()
function. Come up with an example that shows what it does.
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.
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:
## [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()
.
Create a vector called prices
with the values 12, 11, and 8. Find the average price.
What happens if you run prices + 1
? What is this doing?
The function sum()
will add all the values in a vector. Run ?sum
to learn how it works. Find the total sum of the prices
object.
The seq()
function will make a sequence of numbers for you. For example:
## [1] 1 2 3 4 5
## [1] 0 10 20 30 40 50 60 70 80 90 100
## [1] 1 2 3 4 5
## [1] 1 2 3 4 5
Create a vector named values
containing numbers from 0 to 1000 in increments of 5. Write a comment explaining your code.
Find the sum of every number in values
.
Find the square root of each number in values
. Write a comment describing why this returned many numbers while Question 2 only had one number.
5. 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: