MC³ LabCUHK Psychology

Lesson 4 · Working with data

Load data

Open the course dataset from a CSV, Excel or SPSS file and take a first look.

In this lesson

  • Tell R where your file is
  • Load CSV, Excel and SPSS files
  • Look at a dataset

You need

Datastudents.csvStudent Life Survey: 300 students, with their study habits, sleep, stress, wellbeing and exam results.
Datastudents.xlsxThe same survey saved as an Excel file.
Datastudents.savThe same survey saved as an SPSS file.
Scriptlesson-04.RAll the code in this lesson, ready to run.

Save the files in your project's data folder. You only need students.csv for the rest of the course; the Excel and SPSS versions are just for this lesson.

Where R looks

With your learn-r project open, R looks inside the project folder. So the file's path is:

"data/students.csv"
the data folder/the file

Use / between folders, even on Windows. Check where R is looking with getwd().

R code
getwd()
Output
[1] "/Users/you/Documents/learn-r"

You will see your own folder here.

Load a CSV file

CSV is the most common data format. read_csv() comes with the tidyverse:

R code
library(tidyverse)
students <- read_csv("data/students.csv")
Output
── Attaching core tidyverse packages ────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.1     ✔ readr     2.2.0
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.3     ✔ tibble    3.3.1
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
✔ purrr     1.2.2     
── Conflicts ──────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
Rows: 300 Columns: 16
── Column specification ──────────────────────────────────────────────
Delimiter: ","
chr (7): ID, Gender, Living, Motivation, Grade, Workshop, Used_Sup...
dbl (9): Age, StudyHours, Attendance, LearningStyle, Sleep, Stress...

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

The report says: 300 rows, 16 columns. chr = text columns, dbl = number columns.

Look at the data

R code
students
Output
# A tibble: 300 × 16
   ID    Gender   Age Living StudyHours Attendance Motivation
   <chr> <chr>  <dbl> <chr>       <dbl>      <dbl> <chr>     
 1 S001  Male      20 Hall           16         92 Medium    
 2 S002  Male      21 Hall           NA         88 Medium    
 3 S003  Female    19 Hall            0         86 Medium    
 4 S004  Male      19 Home            3         89 Medium    
 5 S005  Female    21 Hall           27         92 High      
 6 S006  Male      22 Home           19         56 Low       
 7 S007  Female    21 Hall           20         92 Medium    
 8 S008  Female    20 Hall            4         61 Medium    
 9 S009  Male      19 Home           16         82 High      
10 S010  Female    21 Home           17        100 High      
# ℹ 290 more rows
# ℹ 9 more variables: LearningStyle <dbl>, Sleep <dbl>, Stress <dbl>,
#   ExamScore <dbl>, Grade <chr>, Workshop <chr>, Wellbeing_T1 <dbl>,
#   Wellbeing_T2 <dbl>, Used_Support <chr>

This is a tibble, a tidy data frame. It shows the size, each column's type, and the first 10 rows.

R code
glimpse(students)
Output
Rows: 300
Columns: 16
$ ID            <chr> "S001", "S002", "S003", "S004", "S005", "S006"…
$ Gender        <chr> "Male", "Male", "Female", "Male", "Female", "M…
$ Age           <dbl> 20, 21, 19, 19, 21, 22, 21, 20, 19, 21, 23, 21…
$ Living        <chr> "Hall", "Hall", "Hall", "Home", "Hall", "Home"…
$ StudyHours    <dbl> 16, NA, 0, 3, 27, 19, 20, 4, 16, 17, 21, 22, 1…
$ Attendance    <dbl> 92, 88, 86, 89, 92, 56, 92, 61, 82, 100, 84, 1…
$ Motivation    <chr> "Medium", "Medium", "Medium", "Medium", "High"…
$ LearningStyle <dbl> 0, 0, 0, 0, 0, 1, 0, 0, 3, 2, 0, 1, 2, 3, 1, 1…
$ Sleep         <dbl> 6.0, 6.0, NA, 5.8, 5.5, 6.5, 6.2, 6.9, 6.6, 6.…
$ Stress        <dbl> 5, 6, 8, 5, 7, 6, 7, 3, 6, 6, 5, 5, 8, 7, 4, 5…
$ ExamScore     <dbl> 73, 75, 52, 43, 88, 67, 83, 39, 72, 70, 66, 87…
$ Grade         <chr> "B", "B", "C", "D", "A", "B", "A", "D", "B", "…
$ Workshop      <chr> "No", "No", "Yes", "No", "No", "No", "No", "Ye…
$ Wellbeing_T1  <dbl> 5.00, 1.33, 3.33, 5.00, 4.00, 2.67, 4.33, 4.33…
$ Wellbeing_T2  <dbl> 5.00, 1.33, 4.33, 3.33, 4.00, 2.00, 5.33, 3.33…
$ Used_Support  <chr> "No", "No", "Yes", "Yes", "Yes", "Yes", "Yes",…

glimpse() shows every column on its side. Other handy views:

  • head(students): first 6 rows
  • View(students): a spreadsheet view (capital V)
  • names(students): column names

Excel and SPSS files

Same data, other formats:

R code
library(readxl)
students_xl <- read_excel("data/students.xlsx")

library(haven)
students_sav <- read_sav("data/students.sav")
head(students_sav$LearningStyle)
Output
<labelled<double>[6]>: Preferred learning style
[1] 0 0 0 0 0 1

Labels:
 value           label
     0          Visual
     1    Kinaesthetic
     2        Auditory
     3 Reading/Writing

SPSS files can keep labels for number codes (0 = Visual, ...). as_factor() swaps the numbers for the labels:

R code
head(as_factor(students_sav$LearningStyle))
Output
[1] Visual       Visual       Visual       Visual       Visual      
[6] Kinaesthetic
Levels: Visual Kinaesthetic Auditory Reading/Writing

For Excel sheets other than the first, use read_excel("file.xlsx", sheet = 2).

When it fails

R code
students <- read_csv("data/student.csv")
Output
Error:
! 'data/student.csv' does not exist in current working
  directory: '/Users/you/Documents/learn-r'.

R says which file it looked for and where. Check:

  1. the file name (spelling, capitals, .csv),
  2. that the file is in data,
  3. that your project is open.

Exercises

Exercise 4.1 Dataset: students.csv

Load and check

  1. Load students.csv again as students, adding show_col_types = FALSE to hide the report.
  2. How many rows and columns? (dim())
  3. Which columns are text?
Show answerHide answer
R code
students <- read_csv("data/students.csv", show_col_types = FALSE)
dim(students)
glimpse(students)
Output
[1] 300  16
Rows: 300
Columns: 16
$ ID            <chr> "S001", "S002", "S003", "S004", "S005", "S006"…
$ Gender        <chr> "Male", "Male", "Female", "Male", "Female", "M…
$ Age           <dbl> 20, 21, 19, 19, 21, 22, 21, 20, 19, 21, 23, 21…
$ Living        <chr> "Hall", "Hall", "Hall", "Home", "Hall", "Home"…
$ StudyHours    <dbl> 16, NA, 0, 3, 27, 19, 20, 4, 16, 17, 21, 22, 1…
$ Attendance    <dbl> 92, 88, 86, 89, 92, 56, 92, 61, 82, 100, 84, 1…
$ Motivation    <chr> "Medium", "Medium", "Medium", "Medium", "High"…
$ LearningStyle <dbl> 0, 0, 0, 0, 0, 1, 0, 0, 3, 2, 0, 1, 2, 3, 1, 1…
$ Sleep         <dbl> 6.0, 6.0, NA, 5.8, 5.5, 6.5, 6.2, 6.9, 6.6, 6.…
$ Stress        <dbl> 5, 6, 8, 5, 7, 6, 7, 3, 6, 6, 5, 5, 8, 7, 4, 5…
$ ExamScore     <dbl> 73, 75, 52, 43, 88, 67, 83, 39, 72, 70, 66, 87…
$ Grade         <chr> "B", "B", "C", "D", "A", "B", "A", "D", "B", "…
$ Workshop      <chr> "No", "No", "Yes", "No", "No", "No", "No", "Ye…
$ Wellbeing_T1  <dbl> 5.00, 1.33, 3.33, 5.00, 4.00, 2.67, 4.33, 4.33…
$ Wellbeing_T2  <dbl> 5.00, 1.33, 4.33, 3.33, 4.00, 2.00, 5.33, 3.33…
$ Used_Support  <chr> "No", "No", "Yes", "Yes", "Yes", "Yes", "Yes",…

300 rows, 16 columns. Text (chr) columns: ID, Gender, Living, Motivation, Grade, Workshop, Used_Support.

Exercise 4.2 Dataset: students.csv

First questions

  1. What is student S001's exam score?
  2. How many students skipped the Sleep question?
  3. What is the average sleep of those who answered?
Show hintHide hint

head() shows S001. Count NAs with sum(is.na(...)). mean() needs na.rm = TRUE.

Show answerHide answer
R code
head(students, 1)
sum(is.na(students$Sleep))
mean(students$Sleep, na.rm = TRUE)
Output
# A tibble: 1 × 16
  ID    Gender   Age Living StudyHours Attendance Motivation
  <chr> <chr>  <dbl> <chr>       <dbl>      <dbl> <chr>     
1 S001  Male      20 Hall           16         92 Medium    
# ℹ 9 more variables: LearningStyle <dbl>, Sleep <dbl>, Stress <dbl>,
#   ExamScore <dbl>, Grade <chr>, Workshop <chr>, Wellbeing_T1 <dbl>,
#   Wellbeing_T2 <dbl>, Used_Support <chr>
[1] 10
[1] 6.412414

S001 scored 73. 10 students skipped Sleep; the others sleep 6.4 hours a night on average.

Key points

  • Open your project, then load with a path like "data/students.csv".
  • read_csv(), read_excel() (readxl), read_sav() (haven).
  • Look first: glimpse(), head(), View().