R Language Coercion Implicit Coercion


Example

Coercion happens with data types in R, often implicitly, so that the data can accommodate all the values. For example,

x = 1:3
x
[1] 1 2 3
typeof(x)
#[1] "integer"

x[2] = "hi"
x
#[1] "1"  "hi" "3" 
typeof(x)
#[1] "character"

Notice that at first, x is of type integer. But when we assigned x[2] = "hi", all the elements of x were coerced into character as vectors in R can only hold data of single type.