Introduction To R
Introduction To R
Go to https://posit.co/download/rstudio-desktop/ and follow the two steps. Step one will have
you download and install R (click the link for either Mac or Windows and follow the on-screen
instructions). Step 2 will have you download and install RStudio (again select either Mac or
Windows and follow the on-screen instructions). If you run into trouble here or want help, just
email me and we’ll meet for like 5 minutes to get it set up – it won’t take long at all.
One way to organize R files is with projects. Projects allow you to bundle related R files together
in a self-contained folder. To create a new project, open R, and click File -> New Project -> New
Directory -> New Project. The name of the directory should be related to the theme of your
project. Name this project, “learning_R”. Make sure the project is a subdirectory of your “R
Files” folder. This will ensure that the project folder is created inside of “R Files.”
Once the project is created, you can create an R file by going to File -> New File -> R
Markdown -> Ok. Save the document by clicking File -> Save and writing a name for the file. I
will explain more about coding in R MarkDown later, but you should now understand the basics
for creating files in R.
Variables are values that can change. These are typically named with strings or characters. In R,
you can create a variable by assigning values to an object name. For instance, writing x = 2 will
create a variable named x and assign it the value of 2. If you then write x + 2, R will return 4.
You can change the value of the variable to, for instance, x = 3, at which point x + 2 will return
5. Generally, you can assign values to variables using [var_name] = [value], and any future
reference to [var_name] will be as if [value] is being used instead.
Functions
In math, functions have three components: a name, an argument (sometimes called a parameter),
and an output. Consider the function f(x) = y. The name of the function is f, the argument in
parentheses is x, and the output is y. Functions also exist in programming and consist of the same
three components. Consider the function print(“Hello World”). The name of the function is print,
the argument is the string, “Hello World”, and the output, which is printed to the console, is also
“Hello World”. Function names are often verbs, indicating that the user is asking the computer to
“do” something, like ‘print’.
In general, functions take arguments in and “spit” output out. They take the form
name(arguments) -> output. They range from the fairly simple, like the print() function, to more
complex, like the linear regression function, lm(). Functions can have multiple arguments, which
are separated by commas. Consider the function rep(2, 5). The name of the function is rep, which
stands for ‘repeat’ (a verb). There are two arguments: 2 and 5. This tells R to repeat the number 2
five times, and the output therefore is 5 twos. Function arguments are often labeled generically.
In the case of rep, the arguments are labeled rep(x, times), where x is the number to be repeated
and times is how many times x should be repeated.
To create a new function in R, you use the ‘function’ function! It has the form
Name <- function(arguments) { transformation }
where ‘name’ is the name you wish to give your function, ‘arguments’ are the comma-separated
arguments your function will use in its transformation, and ‘transformation’ is the operation you
wish to perform on the arguments. We will practice this next week in R, but there is an exercise
below asking you to sketch out what this function might look like based on the information you
have so far.
Packages
Because R is open-source, users can contribute their own code and homemade functions for the
broader community to use. These are stored in packages, which can be downloaded by anyone. R
comes with a set of built-in functions like those I’ve discussed so far, but some popular functions
you will encounter online and in class are only accessible through downloading packages.
Thankfully, downloading and installing packages is easy. To download and install a package,
write install.packages(“name”) where name is the name of the package you wish to download.
The most commonly used package is Tidyverse, so you might try typing
install.packages(“tidyverse”) after getting your R up and running.
You only need to install a package once. After you do so, you can call the functions inside a
package by referencing the package at the beginning of your code script with library(name).
Most R scripts you encounter will have library(tidyverse) at the top of the script. We will talk
more about Tidyverse and its functions next week.
Exercises
Consider the function: sum(2, 3)
Suppose you want to write a function that takes any number (call it x), squares it, and adds 2 to
the squared number. How would you write the transformation of this function in R?