Run Your First Script
Read, run, and check R code
An R script is a saved set of instructions. You can run the instructions again, correct them, or adapt them for another analysis. This makes your work reproducible: you and another reader can see how the result was produced.
Open The Starter Script
- Open
exploring-statistics-r.Rprojin RStudio. - In the Output pane, select the Files tab.
- Open the
scriptsfolder. - Select
00-open-and-check-data.R.
The file opens in the Source pane. Read the comments at the top before running the code.
Run Code One Step At A Time
Place your cursor anywhere on a line of code and select Run. RStudio sends that line to the Console. You can also select several complete lines and run them together.
Run the starter script from top to bottom. Pause after each section and look for the result described by its comments.
The > prompt means R is ready for another command. If the prompt has not returned, R may still be calculating.
A + prompt means R thinks the command is unfinished. Press Esc, check for a missing parenthesis or quotation mark, and run the complete command again.
After the setup section runs:
- the Environment pane should contain an object named
eammi; and - the Console should show that the data contain 2,073 observations and 39 variables.
The first run may take a few minutes because the setup file checks for and prepares the R tools used throughout the workbook, including tidyverse.
The rest of this page introduces the central ideas you need to navigate R: objects, functions, data frames, the pipe, and comments. Examples of these features also appear at the end of 00-open-and-check-data.R, so you can practice with the script or review the ideas on this page by themselves.
Objects: Names R Remembers
An object is a named result stored in R’s memory. The assignment operator <- gives a result a name.
favorite_number <- 7
favorite_numberRead the first line as “store the value 7 in an object named favorite_number.” Running the object name prints the stored value.
You will see the object eammi throughout this workbook. It represents the data we are working with.
Functions: Actions R Performs
A function tells R to perform an action. Its name is followed by parentheses.
round(21.428, digits = 2)Here, round() performs the action, 21.428 is the value, and digits = 2 provides an instruction about the result.
When you meet a new function, ask three questions:
- What object or variable does it use?
- What action does it perform?
- What result should it return?
Data Frames: Observations And Variables
A data frame is a rectangular dataset.
- Each observation usually represents one participant or case.
- Each variable records one characteristic or measurement.
- Variables may contain numbers, categories, text, or missing responses.
The setup file reads the EAMMi2 teaching data and stores the resulting data frame as eammi.
These commands ask basic questions about that data frame:
nrow(eammi)
ncol(eammi)
names(eammi)nrow() counts observations, ncol() counts variables, and names() lists the variable names.
The Pipe: Work From Left To Right
The native pipe |> passes the result of one step into the next step.
eammi |>
count(Gender)Read this as: “begin with eammi, and then count how often each value of Gender appears.”
Older R materials may use %>%. This workbook was developed with R 4.6.0 and RStudio 2026.07.0+139. It uses |>, which is built into R and is the default pipe inserted by this version of RStudio. The chapter code also uses functions from tidyverse; the setup file prepares those functions for you.
Practice The Routine
Use the final examples in 00-open-and-check-data.R to practice:
- Read the comment above an example.
- Predict what will appear.
- Run the complete example.
- Find the result in the Console or Environment pane.
- Describe the result in one sentence.
You can now continue to Understanding EAMMi2 or use Troubleshooting if your result differs from the description above.
Comments: Notes For Readers
R ignores anything after
#on a line. These comments explain the purpose of the code to you and anyone else who reads the script.When you copy or adapt code, update its comments too. A short, accurate comment will help you understand the analysis when you return to it later.