Skip to contents

is.numeric() returns TRUE when typeof(x) is double or integer – testing is.numeric(x) || is.integer(x), is.numeric(x) || is.double(x), is.double(x) || is.integer(x), class(x) %in% c("numeric", "integer"), typeof(x) %in% c("double", "integer"), or inherits(x, c("numeric", "integer")) is thus redundant.

Usage

is_numeric_linter()

Details

NB: This linter plays well with class_equals_linter(), which can help avoid further is.numeric() equivalents like any(class(x) == c("numeric", "integer")).

See also

linters for a complete list of linters available in lintr.

Examples

# will produce lints
lint(
  text = "is.numeric(y) || is.integer(y)",
  linters = is_numeric_linter()
)
#> <text>:1:1: warning: [is_numeric_linter] Use `is.numeric(x)` instead of the equivalent `is.numeric(x) || is.integer(x)`. Use is.double(x) to test for objects stored as 64-bit floating point.
#> is.numeric(y) || is.integer(y)
#> ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

lint(
  text = 'class(z) %in% c("numeric", "integer")',
  linters = is_numeric_linter()
)
#> <text>:1:1: warning: [is_numeric_linter] Use is.numeric(x) instead of class(x) %in% c("integer", "numeric"). Use is.double(x) to test for objects stored as 64-bit floating point.
#> class(z) %in% c("numeric", "integer")
#> ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

lint(
  text = 'typeof(z) %in% c("double", "integer")',
  linters = is_numeric_linter()
)
#> <text>:1:1: warning: [is_numeric_linter] Use is.numeric(x) instead of typeof(x) %in% c("integer", "double"). Use is.double(x) to test for objects stored as 64-bit floating point.
#> typeof(z) %in% c("double", "integer")
#> ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

lint(
  text = 'inherits(z, c("numeric", "integer"))',
  linters = is_numeric_linter()
)
#> <text>:1:1: warning: [is_numeric_linter] Use is.numeric(x) instead of inherits(x, c("integer", "numeric")). Use is.double(x) to test for objects stored as 64-bit floating point.
#> inherits(z, c("numeric", "integer"))
#> ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# okay
lint(
  text = "is.numeric(y) || is.factor(y)",
  linters = is_numeric_linter()
)
#> ℹ No lints found.

lint(
  text = 'class(z) %in% c("numeric", "integer", "factor")',
  linters = is_numeric_linter()
)
#> ℹ No lints found.