Coercion

When you call a function with an argument of the wrong type, R will try to coerce values to a different type so that the function will work. There are two types of coercion that occur automatically in R: coercion with formal objects and coercion with built-in types.

With generic functions, R will look for a suitable method. If no exact match exists, then R will search for a coercion method that converts the object to a type for which a suitable method does exist. (The method for creating coercion functions is described in Creating Coercion Methods.)

Additionally, R will automatically convert between built-in object types when appropriate. R will convert from more specific types to more general types. For example, suppose that you define a vector x as follows:

> x <- c(1, 2, 3, 4, 5)
> x
[1] 1 2 3 4 5
> typeof(x)
[1] "double"
> class(x)
[1] "numeric"

Let’s change the second element of the vector to the word “hat.” R will change the object class to character and change all the elements in the vector to char:

> x[2] <- "hat"
> x
[1] "1"   "hat" "3"   "4"   "5"  
> typeof(x)
[1] "character"
> class(x)
[1] "character"

Here is an overview of the coercion rules:

  • Logical values are converted to numbers: TRUE is converted to 1 and FALSE to 0.

  • Values are converted to the simplest type required to represent all information.

  • The ordering is roughly logical < integer < numeric < complex < character < list.

  • Objects of type raw are not converted to other types.

  • Object attributes are dropped when an object ...

Get R in a Nutshell, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.