> x <- 5
ASI: Introduction to R
September 2, 2025
R
and RStudio
are two separate but connected things
R
is like the engine of your car
RStudio
is the ‘cabin’ we use to control the engine
R
R
does all the calculations, manages the data, generates plotsRStudio
helps manage our code, display the plots etcRStudio
I use R Projects
to manage each analysis
R_Training
in your home directoryRStudio
RStudio
will always open in a directory somewhereFiles
pane (bottom-right) to see where it’s lookinggetwd()
in the Console pane)R
We want RStudio to be looking in our new directory (R_Training
)
\(\implies\)R Projects make this easy
File
> New Project
> Existing Directory
R_Training
directory \(\implies\) Create Project
R Project
name is always the directory nameFile
> New File
> R Script
Introduction.R
This is the basic layout we often work with
bash
terminal (or PowerShell for Windows)Like we did earlier, in the R Console type:
Where have we created the object x
?
x
in our R Environment
Global Environment
R Environment
is like your desktop.RData
objectR
can be set to automatically save your environment on exitgit
tab will also appear for those who use git in their projectHelp
Tab for the sqrt()
function
base
base
is always installed and loaded with R
R
can be hit & miss
Help > Cheatsheets > RStudio IDE Cheat Sheet
Page 2 has lots of hints:
Ctrl + 1
places focus on the Script WindowCtrl + 2
places focus on the ConsoleCtrl + 3
places focus on the Help TabBest practice for analysis is to enter all code in the Script Window
RStudio
will:
#
Introduction.R
but don’t do anything elseTo send this to the Console:
x <- 1:5
then Ctrl+Enter
(Cmd+Enter
on OSX), orCtrl+Enter
(or Cmd+Enter
)Run
button
As well as creating objects, we can write general code
Enter this in your Script Window then send to the Console
When we executed length(x)
did we create a new object?
No, we just called the function length()
and executed it on x
The output of the function was simply printed to the console
x
is a vector
\(\implies\) fundamental structure in R
R
when we pass a vector
to a function, the entire vector is evaluated
Add the following to your script
Notice we have a value returned for each element of x
Discuss what you think the values mean as you write your comments
x
clearly contains numbers (i.e. integers)
typeof(x)
[]
R
has 6 types of atomic vectors \(\implies\) only 4 are commonly used
logical
: Can only contain TRUE
or FALSE
integer
: Only contains whole numbers
numeric
: Contains numbers with decimal points (aka doubles
)
character
A logical vector is returned by a logical test
x
c()
Introduction.R
R