Created
April 16, 2012 20:51
-
-
Save geofferyzh/2401432 to your computer and use it in GitHub Desktop.
RinAction - R Data Manipulation - missing variables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################################################# | |
## ------------------- Missing Values ----------------------########### | |
## 1. missing values in R are represented as NA (not available) # | |
## 2. Impossible values (devided by 0) are represented as NaN (not a number)# | |
############################################################################# | |
# Identify missing values using is.na() function | |
is.na(leadership[6:9]) | |
y <- c(1,2,3,NA) | |
is.na(y) | |
# Calculate vector length with missing (NAs) | |
a <- c(1,2,3,NA,NA,2,NA) | |
length(a[!is.na(a)]) | |
length(na.omit(a)) | |
sum(!is.na(a)) | |
# Recode extreme/incorrect values to missing | |
leadership[leadership$age == 99, "age"] <- NA | |
leadership | |
# Math calculation without missing | |
x <- c(1,2,NA,3) | |
y <- x[1] + x[2] + x[3] + x[4] | |
z <- sum(x) | |
# both y and z should b NA | |
y <- sum(x,na.rm=TRUE) | |
# Delete missing observations/records using the na.omit() function | |
newdata <- na.omit(leadership) | |
newdata |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment