<- 1 # Assign 1 to x
x 2 * x # Multiply x by 2
[1] 2
Motivating scenario: You have heard about R and RStudio, and maybe used them, but want a foundation so you can know what is going on as you do more and more with it.
Learning goals: By the end of this chapter you should be able to
R
is a computer program built for data analysis. As opposed to GUIs, like Excel, or click-based stats programs, R
is focused on writing and sharing scripts. This allows analyses to be shared and replicated, ensuring that data manipulation occurs in a script, thus preserving the integrity of the original data, while providing tremendous flexibility. R
has become the computer language of choice for most statistical work because it’s free, allows for reproducible analyses, makes great figures, and has many “packages” that support the integration of novel statistical approaches. In a recent paper, we used R
to analyze hundreds of Clarkia genomes and learn about the (Figure 1.1 from Sianta et al. (2024)). RStudio is an Integrated Development Environment (IDE)—a nice setup to interact with R
and make it easier to use.
More precisely, R is a programming language that runs computations, while RStudio is an integrated development environment (IDE) that provides an interface by adding many convenient features and tools. So just as the way of having access to a speedometer, rearview mirrors, and a navigation system makes driving much easier, using RStudio’s interface makes using R much easier as well.
Before opening RStudio, let’s get familiar with two simple ways to get started using R – (1) Using R as a calculator, and (2) Storing information by assigning values to variables.
R can perform simple (or complex) calculations. For example, entering 1 + 1
returns 2
, and entering 2^3
(two raised to the power of three) returns 8
. Try it yourself by running the code below, and then experiment with other simple calculations.
Storing values in variables allows for efficient (and less error-prone) analyses, while paving the way to more complex calculations. In R, we assign values to variables using the assignment operator, <-
. For example, to store the value 1
in a variable named x
, type x <- 1
. Now, 2 * x
will return 2
.
<- 1 # Assign 1 to x
x 2 * x # Multiply x by 2
[1] 2
But R
must “know” something before it can “remember” it. The code below aims to set y
equal to five and see what y
plus one is (it should be six). However, it returns an error. Run the code to see the error message, then fix it!
Now try assigning different numbers to x
and y
, or even using them together in a calculation, such as x + y
. This simple concept of assigning values will become a key tool as we move into statistical analysis.
The following sections introduce the very basics of R including:
Then we summarize the chapter, present practice questions, a glossary, a review of R functions and R packages introduced, and provide links to additional resources.