Mixing Objects

There are occasions when different classes of R objects get mixed together. You already know that vectors must contains elements that are all same type, R will automatically coerce to a single type when attempting to create a vector that combines multiple types.

 

x<- c(100, "Statistics with R", TRUE) #character
y <- c(TRUE, 200) #numeric
z <- c("a", TRUE) # character

class(x)
class(y)
class(z)

Output:

> class(x)
[1] "character"
> class(y)
[1] "numeric"
> class(z)
[1] "character"

Remember that the only rule about vectors says this is not allowed. When different objects are mixed in a vector, coercion occurs so that every element in the vector is of the same class.

Vectors

Explicit Coercion