print("Hello, World!")
[1] "Hello, World!"
The primary pieces of software you are going to need for this class are
There are also a few optional pieces of software you’ll need for extra credit lessons, but we will cover when needed.
By default, RStudio has 3-4 main frames:
For today, we are mostly going to explore some basic features of R using the console, copying and pasting commands from the website rather than saving them in a script (which we will set up next week). All an .R
script does is save your code and pass it line-by-line to the console. After today, anything we want to save will be done through a script, anything we just need to run one time will be done in console
First, let’s try the traditional first command!
print("Hello, World!")
[1] "Hello, World!"
We can also use R like a basic calculator
1 + 1
[1] 2
The first two commands we ran simply spat the output out in the console
More often, though, we want to save the output to our R Environment (top right panel)
To do this, we need to assign the output to an object
R is a type of object-oriented programming environment. This means that R thinks of things in its world as objects, which are like virtual boxes in which we can put things: data, functions, and even other objects.
<-
, which is a less than symbol, <
, followed by a hyphen, -
.
=
(which is more common across other programming languages), and you may see this “in the wild”<-
for clarity and readability, and let’s try to use <-
in this class## assign value to object x using <-
<- 1 x
x
has a value of 1
## what's in x?
x
[1] 1
Note: the [1]
is just the index (order) number, if we had more than 1 thing in our object, that would be more useful
Quick exercise
Using the arrow, assign the output of
1 + 1
tox
. Next subtract 1 fromx
and reassign the result tox
. Show the value inx
.
R uses variety of data types and structures to represent and work with data. There are many, but the major ones that you’ll use most often are:
logical
numeric
(integer
& double
)character
vector
matrix
list
dataframe
Let’s see what type of object x
we created earlier is
typeof(x)
[1] "double"
What if we make it “1”?
<- "1"
x typeof(x)
[1] "character"
Understanding the nuanced differences between data types is not important right now. Just know that they exist and that you’ll gain an intuitive understanding of them as you become better acquainted with R.
To download a package from CRAN, use:
install.packages("<package name>")
NOTE Throughout this course, if you see something in triangle brackets (<...>
), that means it’s a placeholder for you to change accordingly.
Many packages rely on other packages to function properly. When you use install.packages()
, the default option is to install all dependencies. By default, R will check how you installed R and download the right operating system file type.
Quick exercise
Install the
tidyverse
package, which is really a suite of packages that we’ll use throughout the semester. Don’t forget to use double quotation marks around the package name:
install.packages("tidyverse")
Package libraries can loaded in a number of ways, but the easiest it to write:
library("<library name>")
where "<library name>"
is the name of the package/library. You will need to load these before you can use their functions in your scripts. Typically, they are placed at the top of the script file.
For example, let’s load the tidyverse
library we just installed:
## load library (note quirk that you don't need quotes here)
library(tidyverse)
Warning: package 'purrr' was built under R version 4.4.1
Warning: package 'lubridate' was built under R version 4.4.1
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.1.5
✔ forcats 1.0.0 ✔ stringr 1.5.1
✔ ggplot2 3.5.1 ✔ tibble 3.2.1
✔ lubridate 1.9.4 ✔ tidyr 1.3.1
✔ purrr 1.0.4
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
Notice that when you load the tidyverse (which, again, is actually loading a number of other libraries), you see a lot of output. Not all packages are this noisy, but the information is useful here because it shows all the libraries that are now loaded and ready for you to use.
It’s almost impossible to have every R function and nuance memorized, and you don’t have to. With all the user-written packages, it would be difficult to keep up. When stuck, there are a few ways to get help.
In the console, typing a function name immediately after a question mark will bring up that function’s help file (in RStudio, you should see in the bottom right panel):
## get help file for function
?median
Two question marks will search for the command name in CRAN packages (again, in the bottom right facet):
## search for function in CRAN
??median
At first, using help files may feel like trying to use a dictionary to see how to spell a word — if you knew how to spell it, you wouldn’t need the dictionary! Similarly, if you knew what you needed, you wouldn’t need the help file. But over time, they will become more useful, particularly when you want to figure out an obscure option that will give you exactly what you need.
For example, here’s another magic portal to the tidyverse
’s dplyr
website (you may spent a good amount of time here this semester)
Usually if you Google something like “<package name> R,” and the website will come up
You can find links to all the tidyverse
packages here
Google is a coder’s best friend. If you are having a problem, odds are a 1,000+ other people have too and at least one of them has been brave enough (people can be mean on the internet) to ask about it in a forum like StackOverflow, CrossValidated, or R-help mailing list.
If you are lucky, you’ll find the exact answer to your question. More likely, you’ll find a partial answer that you’ll need to modify for your needs. Sometimes, you’ll find multiple partial answers that, in combination, help you figure out a solution. It can feel overwhelming at first, particularly if it’s a way of problem-solving that’s different from what you’re used to. But it does become easier with practice.
We’re going to use a number of packages this semester. While we may need more than this list — and you almost certainly will in your own future work — let’s install these to get us started.
Quick exercise
Install the following packages using the
install.packages()
function:
devtools
knitr
- `rmarkdown`
quarto
There’s no official assignment this week, just make sure you have been able to install everything covered in this lesson. If you haven’t, please reach out ASAP!
If you really want something to do, start thinking about ideas for your final project!
Comments
For this section, let’s just open a blank
.R
script in RStudio (again, all these commands will be in a script in your class folder we set up next week)#
#
for your comments if you like, R treats them all the same##'
(two hashes and an apostrophe) is really useful in RStudio, as it automatically comments the next line (although this can be annoying at times too)