Lesson 4 · Working with data
Load data
Open the course dataset from a CSV, Excel or SPSS file and take a first look.
Where R looks
With your learn-r project open, R looks inside the project folder. So the file's path is:
data folder/the file
Use / between folders, even on Windows. Check where R is looking with getwd().
getwd()[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:
library(tidyverse)
students <- read_csv("data/students.csv")The report says: 300 rows, 16 columns. chr = text columns, dbl = number columns.
Look at the data
students# 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.
glimpse(students)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 rowsView(students): a spreadsheet view (capital V)names(students): column names
Excel and SPSS files
Same data, other formats:
library(readxl)
students_xl <- read_excel("data/students.xlsx")
library(haven)
students_sav <- read_sav("data/students.sav")
head(students_sav$LearningStyle)<labelled<double>[6]>: Preferred learning style
[1] 0 0 0 0 0 1
Labels:
value label
0 Visual
1 Kinaesthetic
2 Auditory
3 Reading/WritingSPSS files can keep labels for number codes (0 = Visual, ...). as_factor() swaps the numbers for the labels:
head(as_factor(students_sav$LearningStyle))[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
students <- read_csv("data/student.csv")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:
- the file name (spelling, capitals,
.csv), - that the file is in
data, - that your project is open.
Exercises
Load and check
- Load
students.csvagain asstudents, addingshow_col_types = FALSEto hide the report. - How many rows and columns? (
dim()) - Which columns are text?
Show answerHide answer
students <- read_csv("data/students.csv", show_col_types = FALSE)
dim(students)
glimpse(students)[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.
First questions
- What is student S001's exam score?
- How many students skipped the
Sleepquestion? - 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
head(students, 1)
sum(is.na(students$Sleep))
mean(students$Sleep, na.rm = TRUE)# 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().