0% found this document useful (0 votes)
23 views3 pages

Introduction To R

The document provides instructions for installing R and RStudio, setting up working directories and projects in RStudio, and introduces some basic concepts in R including values and value types, variables, functions, and packages. It concludes with exercises asking the reader to identify components of functions and write functions based on descriptions.

Uploaded by

David Crabtree
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views3 pages

Introduction To R

The document provides instructions for installing R and RStudio, setting up working directories and projects in RStudio, and introduces some basic concepts in R including values and value types, variables, functions, and packages. It concludes with exercises asking the reader to identify components of functions and write functions based on descriptions.

Uploaded by

David Crabtree
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Installing RStudio

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.

Working Directories and Projects


R code is stored in R files that have the format “filename.Rmd”. You will want to create a folder
on your computer (maybe on your desktop or in your documents) where you will store and
organize all of your R files. I recommend making a folder on your desktop and calling it
something like “R Files”. Then, save all of your R files to that folder.

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.

Values, Value Types, and Variables


In programming, a value is information that can be manipulated by the computer. Every value
has a ‘type’ which represents what kind of information the value communicates. The most
intuitive value type is numerical: 2, -5, and ½ are all numerical values that R understands and can
perform mathematical operations on. For instance, writing “2 + 2” will return the numerical
value 4. Not all numerical values are the same: 2 is an integer while ½ or 0.5 is a ‘double.’ Other
value types include characters, like ‘c’ or ‘a’, and strings of characters, like ‘cat’. Still others
include Booleans, which can only be TRUE or FALSE. For instance, writing “2 > 3” will return
“FALSE”, because 2 is not greater than 3, and the phrase “2 > 3” is interpreted as a Boolean
statement asking whether the condition is true.

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)

i. What is the name of the function?


ii. What is/are the argument[s] of the function?
iii. What will be the output of the function?
In R, the mean() function calculates the arithmetic mean (average) of a set of numbers. It has the
form mean(x) and returns the mean of the numbers in the set x.
i. Suppose you want to calculate the mean of the set {2, 5, 8}, how would you do this in
R? NOTE: mean() takes only one argument, and mean({2,5,8}) is incorrect.

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?

You might also like