12.9 Be careful about the numeric type

  1. Often “numeric” is treated as synonymous for double:
# create a double and integeger objects
one <- 1
oneL <- 1L
typeof(one)
#> [1] "double"
typeof(oneL)
#> [1] "integer"

# check their type after as.numeric()
one |> as.numeric() |> typeof()
#> [1] "double"
oneL |> as.numeric() |> typeof()
#> [1] "double"
  1. In S3 and S4, “numeric” is taken as either integer or double, when choosing methods:
sloop::s3_class(1)
#> [1] "double"  "numeric"
sloop::s3_class(1L)
#> [1] "integer" "numeric"
  1. is.numeric() tests whether an object behaves like a number
typeof(factor("x"))
#> [1] "integer"
is.numeric(factor("x"))
#> [1] FALSE

But Advanced R consistently uses numeric to mean integer or double type.