Learn R Programming

varhandle (version 2.0.5)

pin.na: Pinpoint NAs in a vector, matrix or data.frame

Description

This function finds NAs (or defined missing values) in a vector, data.frame or matrix and returns a data.frame which contains two columns that includes the row number and column number of each NA.

Usage

pin.na(x, na.value = NA)

Value

If a vector is given, the index of NAs will be returned in a numeric vector format. In case of a given matrix or data.frame the function will return a data.frame with two columns, one indicating the row number and one indicating the column number. Each row will represent a location of a NA. In case no NA is found, the function will return NULL which makes it easy to use in if conditions using is.null.

Arguments

x

A vector, data.frame or matrix which you want to pinpoint its NAs. (Mandatory)

na.value

A vector containing the value that should be concidered as missing value. The default is NA, but you can add to it or change it to your preference. See the example. (Optional)

Author

Mehrad Mahmoudian

Details

This function provides a quick and easy way to locate and pinpoint NAs in a given vector, data.frame or matrix. This function is also used in inspect.na function of this package.

See Also

Examples

Run this code
    ## generate some variables
    # create a vector
    var1 <- 1:30
    # add NA at random places
    var1[runif(7, 1, 30)] <- NA
    # pinpoint NAs
    pin.na(var1)
    
    # create a matrix
    var2 <- matrix(runif(100, 10, 99), nrow = 10)
    # add NA at random places
    var2[runif(9, 1, 100)] <- NA
    # pinpoint NAs
    pin.na(var2)
    
    ## define your own missing values:
    var2[runif(5, 1, 100)] <- "."
    pin.na(var2, na.value = c(NA, "."))


Run the code above in your browser using DataLab