1) What R and RStudio Are, and Why You Use Both
- R is a programming language designed for statistics, data analysis, simulation, and visualization.
- RStudio (now under Posit) is an IDE (Integrated Development Environment): a more convenient interface for working with R.
Key point:
- You must install R first (because R is the engine).
- Then you can install RStudio (because it is the user-friendly workspace that runs R).
In practice, most people write and run R code inside RStudio because it makes scripting, plotting, and managing files much easier.
2) Installing R and RStudio (Practical Notes)
After installation, one concept you should understand early is the working directory:
- The working directory is the default folder where R looks for files (data files, scripts, outputs) unless you provide full file paths.
- If R “cannot find” a file you are trying to read, the cause is often that the working directory is not pointing to the right folder.
Typical workflow:
- Decide a clear folder for your project (for example,
time_series_project/). - Save scripts and datasets there.
- Set your working directory to that folder, so file loading becomes easy and consistent.
3) Understanding the RStudio Interface (The 4 Main Panes)
When you open RStudio, you typically see four panes. Each has a specific role.
3.1 Console (bottom-left by default)
- This is where you can type R commands one line at a time.
- Press Enter to execute the command immediately.
- It also works like a calculator (for quick checks).
Use the Console for:
- Quick experiments
- Testing a command
- Simple calculations
3.2 Script Editor (top-left by default)
Typing everything in the Console is inefficient for real work. The Script Editor is where you write code in a file you can save and reuse.
How to create a script:
File → New File → R Script
Why scripts matter:
- You keep a record of your work.
- You can rerun the same analysis later.
- You can debug and revise code more systematically.
How to run code from a script:
- Run a single line or selected lines
- Put your cursor on the line (or highlight a block).
- Click Run or use:
- Windows:
Ctrl + Enter - Mac:
Cmd + Enter
- Windows:
- Run the entire script
- Click Source (runs all code in the script).
- “Source with Echo” also prints the commands to the Console so you can see what ran.
There is also a command version:
source("filename.R", echo = TRUE)echo = TRUEprints the code as it runs.- If the file is not in your working directory, you must provide a full path.
3.3 Environment and History (top-right by default)
Environment
- Shows objects currently stored in memory: variables, datasets, models, functions you created.
- Example: if you run
x <- 10, you will seexappear here. - This is useful for confirming what data is loaded and what objects exist.
History
- Stores a list of commands you executed previously in the current RStudio session.
- Helpful when you want to reuse a command without retyping.
3.4 Plots / Packages / Help (bottom-right by default)
This pane has multiple tabs. Three core ones:
Packages
- R’s capabilities expand through packages (add-on libraries).
- Some packages come with base R; others must be installed.
Two different actions matter:
- Install a package (usually done once per computer):
install.packages("packageName")
- Load a package (usually done each time you start a new R session):
library(packageName)
Common mistake:
- People install a package but forget to load it with
library(...), then wonder why commands are not found.
Plots
- Any graph you create appears here.
- R has a simple built-in plotting function:
plot(). - Many people later use more advanced visualization tools like
ggplot2, but basicplot()is enough for many tasks.
Help
- R has built-in documentation for commands and datasets.
- You can access help in two equivalent ways:
?plot
help("plot")
This opens a help page explaining:
- What the command does
- What inputs (arguments) it accepts
- Examples of usage
4) Packages and Built-in Example Datasets (A Simple Demonstration)
R includes a package called datasets (often available by default). It contains sample datasets useful for learning.
4.1 Listing available datasets
You can list datasets in that package using:
data(package = "datasets")
This typically opens a list of dataset names and descriptions.
4.2 Getting information about a dataset
One dataset in datasets is AirPassengers, a classic monthly time series of airline passengers.
To view documentation:
help(AirPassengers)
This gives:
- A description of what the data represents
- The time range and frequency (monthly)
- The source and other notes
4.3 Plotting the dataset
You can plot it using:
plot(
AirPassengers,
main = "International Airline Passengers, 1949–1960",
ylab = "Number of Passengers (in thousands)",
xlab = ""
)
What these arguments mean:
AirPassengers(first input): the data you want to plot.main: the plot title shown at the top.ylab: label for the vertical axis (y-axis).xlab: label for the horizontal axis (x-axis). Here it is set to empty ("") to hide the label.
This plot is a standard time series line plot, showing how passenger volume changes over time and typically revealing patterns like trend and seasonality.
5) The Main Practical Takeaways
- Install R first, then RStudio.
- Use the Script Editor for real work (reproducible code), and the Console for quick tests.
- Learn the difference between:
install.packages()(install once)library()(load each session)
- Use Help (
?command) whenever you forget syntax. - Use Plots to visually inspect data quickly, especially for time series.
