Chapter 3 Importing data

Getting your own data into R is not always easy. There are several useful recent packages that can help you import your csv, Excel, or SPSS file. Please do note that the functions will treat variables differently whether they are numeric or not (e.g., factors, categorical variables, string variables). A variable with zeros and ones to signify women and men will be seen as a continuous variable.

To read in a csv-file:

install.packages("readr")
library("readr")
read_csv("/path_to_file/file.csv", col_names=TRUE ) # col_names=T implies that the first row has column names

To read in an Excel-file:

install.packages("readxl")
library("readxl")
read_excel("/path_to_file/file.xlsx", sheet = "Sheet1")

To read in an SPSS-file:

install.packages("haven")
library("haven")
read_sav("/path_to_file/file.sav")

Two ways to reduce frustration:

  1. When you open an RStudio-project, and you put your data file in the same folder as the .Rproj file, you do not need to specify the path, but can simply write read_sav("file.sav")

  2. You can also use read_sav( file.choose() ), which opens up a window that lets you select your datafile with point-and-click.

The first method is a bit better because it is more ‘reproducible’.

For this workshop, we’ll use data that are provided in the ggplot-package, so we do not need to read in our data.