whichNA: Discover which values are coerced to NA in as.numeric

Description Usage Arguments Value Examples

View source: R/whichNA.R

Description

Helps track down which values are introducing NA's in the as.numeric function. Can be run on a vector or on a dataframe. In the later case it can only check one variable at a time, and will return all columns for the rows that have the na coercion warning. I find this dataframe view helpful in diagnosing the problem.

When to use it? After seeing, "Warning message: NAs introduced by coercion" Run this function on your data and variable to find out exactly which rows are causing the issue. This is a warning that should not be ignored. Nothing is worse than discovering that all the rows greater than 999 had commas (1,000) and are now NA after doing a bunch of analysis.

Usage

1
whichNA(x, df = NULL)

Arguments

x

a vector or a variable name in quotes

df

optional, a data frame

Value

a data frame with all rows containing non valid numeric values

Examples

1
2
3
4
5
6
7
8
x <- c("a", 2, 3, "d")
whichNA(x)

df <- data.frame(x = c("a", 2, 3, "d"),
                 y = c(6, 7, 8, 9),
                 z = c("f", "g", "h", "i"),
                 stringsAsFactors = FALSE)
whichNA("x", df)

williamlief/liefTools documentation built on Jan. 1, 2020, 6:48 p.m.