Introduction to R and Rstudio

Session - import csv

Zoë Turner

Getting data into to R

There are packages to import all types of data and RStudio has wizards for just a few of the possibilities.

We are going to first import a csv using the wizard but also get the code to copy to scripts for reproducibility.

Import wizard

We will use the “Import Dataset” button (but you can also click on the file itself):

Screenshot of RStudio with Import Dataset drop down button highlighted as well as the file capacity_ae.csv in the Files tab.

Two ways of importing csv

The base R doesn’t use any packages but imports a data frame in R whereas {readr} is built on the base R and creates a neater tibble

Screenshot of the drop down menu when Import Dataset is selected in RStudio. From Text (readr)... is highlighted.

tibble = data frame

tibble is synonymous with data frame but acts differently

A tibble… is a modern reimagining of the data.frame…

Tibbles are data.frames that are lazy and surly: they do less (i.e. they don’t change variable names or types, and don’t do partial matching) and complain more (e.g. when a variable does not exist).

This forces you to confront problems earlier, typically leading to cleaner, more expressive code.

https://tibble.tidyverse.org/)

Find the file

Locate the file capacity_ae.csv

Screenshot of the blank import dataset wizard with the Browse... to locate files highlighted.

Data preview

When the file is located {readr} is used automatically and the data preview populates

Screenshot of Import Text wizard with file selected and data in the preview.

Getting the code for later

Whilst it’s possible just to select import, RStudio gives you the option to copy the code:

Screenshot of the code highlighted from the Import Wizard in RStudio.

Running the code

In the editor, once copied from the wizard or shared in a script:

library(readr) # Good practice to put this at the top of a script

capacity_ae <- read_csv("capacity_ae.csv")

End session