When we 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. Values are converted to the simplest type required to represent all information. Coercing of an object from one type of class to another is known as explicit coercion. It is achieved through some functions which are similar to the base functions. But they differ from base functions as they are not generic and hence do not call S3 class methods for conversion. However, data concatenation and coercion are common operations in R. A logical vector is a vector that only contains TRUE and FALSE values. In R, true values are designated with TRUE, and false values with FALSE. While, each element in the vector is a string of one or more characters. Built in character vectors are letters and LETTERS which provide the 26 lower (and upper) case letters, respecitively.
The concatenate c() function is used to combine elements into a vector. When elements are combined from different classes, the c() function coerces to a common type, which is the type of the returned value.
Paste Functions in R
The paste0() and the paste() functions are used to concatenate strings and vectors after converting to character. Both are used extensively to define frequently used character strings, such as file paths, variable names, and strings generated by iterative functions.
Data Coercion in R
If elements of different modes are combined, R implicitly coerces all elements to the most informative class type. 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.
Additionally, R will automatically convert between built-in object types when appropriate. R will convert from more specific types to more general types.
Rules for coercion:
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
It is important to remember that a vector can only be composed of one data type. This means that we cannot have both a numeric and a character in the same vector. If we attempt to do this, the lower ranking type will be coerced into the higher ranking type.