R Programming
- Overview of R
- Installing R on Windows
- Download and Install RStudio on Windows
- Setting Your Working Directory (Windows)
- Getting Help with R
- Installing R Packages
- Loading R Packages
- Take Input and Print in R
- R Objects and Attributes
- R Data Structures
- R – Operators
- Vectorization
- Dates and Times
- Data Summary
- Reading and Writing Data to and from R
- Control Structure
- Loop Functions
- Functions
- Data Frames and dplyr Package
- Generating Random Numbers
- Random Number Seed in R
- Random Sampling
- Data Visualization Using R
Missing Values
Missing values are denoted by NA or NaN for q undefined mathematical operations.
- is.na() is used to test objects if they are NA
- is.nan() is used to test for NaN
- NA values have a class also, so there are integer NA, character NA, etc.
- A NaN value is also NA but the converse is not true
You can create a vector with NAs in it.
x <- c(1, 2, NA, 10, NA)
x
Output:
[1] 1 2 NA 10 NA
is.na() is a function that return a logical vector indicating which elements are NA. It returns TRUE or FALSE.
is.na(x)
Output:
[1] FALSE FALSE TRUE FALSE TRUE
x <- c(1, 2, NA, 10, NA, 0/0)
Output:
[1] 1 2 NA 10 NA NaN
Return a logical vector indicating which elements are NaN
is.nan(x)
Output:
[1] FALSE FALSE FALSE FALSE FALSE TRUE
Removing NA Values:
Extract elements excepts NA values.
x <- c(100, 200, NA, 300,NA, 400)
b <- is.na(x)
x[!b]
Extract elements excepts NAN values.
y <- c(100, 200, NA, 300,NA, 400, 0/0)
y
y[!is.na(y)]
Output:
[1] 100 200 300 400
There is another useful function called complete.cases(). You can use them like below.
x <- c(100, 200, NA, 400, NA, 500)
y <- c("x", "y", NA, "z", NA, "w")
c<-complete.cases(x, y)
Output:
> x
[1] 100 200 400 500
> y
[1] "x" "y" "z" "w"
You can use complete.cases on data frames too.
x <- c(100, 200, NA, 400, NA, 500)
y <- c("x", "y", NA, "z", NA, "w")
z<- c("a","b","c","d","e","f")
d<-data.frame(x,y,z)
d[complete.cases(d),]
Output:
x y z
1 100 x a
2 200 y b
4 400 z d
6 500 w f