ASI: Introduction to R
September 2, 2025
x <- 1:5
)R
equivalent is known as a data.frame
tbl_df
or tibble
(SQL-inspired)tibble
variant todayR
object
R
objectdata.frame
s are structured with vectors as columnsR
File
> New File
> R Script
(Or Ctrl+Shift+N
)DataImport.R
data.zip
from the workshop homepageR_Training
Extract to here
which should create a folder named data
Make sure your files are in data
not in data/data
data
directory using the Files
pane(You should see pigs.csv
in there)
pigs.csv
by clicking on it (View File
)
R
Click on the pigs.csv
and choose Import Dataset
then stop!
(Click Update
if you don’t see this)
We have a preview of the data
We also have a preview of the code we’re about to execute
Code Preview
Box
Click Import
Magic happens!!!
Ignore the red/blue text. This is just ‘helpful’ information
The code we copied has 3 lines:
readr
using library(readr)
readr
functions are about importing datareadr
contains the function read_csv()
read_csv()
tells R what to do with a csv fileThe code we copied has 3 lines:
R Environment
pigs
by using the file name (pigs.csv)The code we copied has 3 lines:
Excel-like
format
Close the preview by clicking the cross
read_csv()
pigs
is now in our R Environment
Environment Tab
click the broom icon (R Environment
Select the code we’ve just pasted and send it to the console
Reloading the packages won’t hurt
Check the Environment Tab
again and pigs
is back
View(pigs)
pigs
is known as a data.frame
R
equivalent to a spreadsheet
NA
tibble
is a data.frame
which prints nicely to your screen
Instead of View()
\(\implies\) preview by typing the object name
Gives a preview up to 10 lines with:
A tibble
60 X 3
len
, supp
, dose
<dbl>
, <chr>
, <chr>
I personally find this more informative than View()
data.frame
objects can be subset using square brackets [row, col]
$
data.frame
[1] 4.2 11.5 7.3 5.8 6.4 10.0 11.2 11.2 5.2 7.0 16.5 16.5 15.2 17.3 22.5
[16] 17.3 13.6 14.5 18.8 15.5 23.6 18.5 33.9 25.5 26.4 32.5 26.7 21.5 23.3 29.5
[31] 15.2 21.5 17.6 9.7 14.5 10.0 8.2 9.4 16.5 9.7 19.7 23.3 23.6 26.4 20.0
[46] 25.2 25.8 21.2 14.5 27.3 25.5 26.4 22.4 24.5 24.8 30.9 26.4 27.3 29.4 23.0
logical
, integer
, numeric
(i.e. doubles), character
len
values are all numeric
(or dbl
) readr
uses a variant called a tbl_df
or tbl
(pronounced tibble)
data.frame
with nice bonus features (e.g. prints a summary only)tidyverse
tidyverse
is a collection of thematically-linked packages
library(tidyverse)
loads all of these packages
readr
is one of these \(\implies\) usually just load the tidyverseReplace library(readr)
with library(tidyverse)
and execute
R
head()
and 2) glimpse()
pigs
R
head()
\(\implies\) x
and n
x
has no default value \(\implies\) we need to provide somethingn = 6L
means n
has a default value of 6 (L \(\implies\) integer
)R
Lower down the page you’ll see
Arguments
x
an object
n
an integer vector of length up to dim(x) (or 1, for non-dimensioned objects). Blah, blah, blah…
head()
prints the first part of an objectread_csv()
R
function read_csv()
read_csv()
read_csv()
read_csv(
file,
col_names = TRUE, col_types = NULL, col_select = NULL,
id = NULL, locale = default_locale(),
na = c("", "NA"), quoted_na = TRUE,
quote = "\"", comment = "",
trim_ws = TRUE,
skip = 0, n_max = Inf,
guess_max = min(1000, n_max),
name_repair = "unique",
num_threads = readr_threads(),
progress = show_progress(),
show_col_types = should_show_types(),
skip_empty_rows = TRUE,
lazy = should_read_lazy()
)
file
, col_names
etc.)col_names = TRUE
)read_csv()
All arguments
for the function were defined somewhere in the GUI.
First Row as Names
check-boxread_csv()
All arguments
for the function were defined somewhere in the GUI.
First Row as Names
check-box
Try clicking/unclicking a few more & try understand the consequences
read_csv()
Vs read.csv()
RStudio
now uses read_csv()
from readr
by defaultread.csv()
in older scripts (from utils
)readr
) version is:
tibble
utils
are read.*()
(csv, delim etc.)readr
has the functions read_*()
(csv, tsv, delim etc.)readxl
is for loading .xls
and xlsx
files.Import Dataset
Sheet1
looks pretty simple