Skip to Main Content

Learn R

This guide focuses on transformation and cleaning functions in R that are especially useful for working with tabular datasets.

Handling Missing Values

Test of Missing Values

#Use is.na for testing for missing values

> y<-c(1,3,5,7,NA,11,NA,NA,17)
> is.na(y)
[1] FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE  TRUE FALSE

 

Recode Values to Missing Values

# recode 99 to missing for variable v1
# select rows where v1 is 99 and recode column v1 
df$v1[df$v1==99] <- NA

Excluding Missing Values from Analyses

> x<-c(1,2,NA,3) 
> mean(x) # returns NA
[1] NA 
> mean(x,na.rm = TRUE) # returns 2
[1] 2

The function complete.cases() returns a logical vector indicating which cases are complete.

Liaison Librarian

Profile Photo
Martin Morris
he/him
Contact:
Schulich Library of Physical Sciences, Life Sciences and Engineering
Macdonald-Stewart Library Building
809 rue Sherbrooke Ouest
Montréal, Québec H3A 0C1
(514) 398 8140
Website Skype Contact: martinatmcgill

McGill LibrariesQuestions? Ask us!
Privacy notice