Data Coersion in R

Learn about data coercion in R.

Convert data types

Values from a data type can be converted into another as long as the desired data type can be represented by the value that is being converted, as in the case of converting TRUE to a string. This type of conversion between data types is called coercion.

Data coercion is accomplished using the as.<datatype>() command.

Possible Coercions

From

To

All

Character

Character (if appropriate) - Double - Logical - Complex - Factor

Integer

Character (if appropriate) - Integer - Logical - Complex - Factor

Double

Character (if appropriate) - Integer - Double - Complex

Logical

Integer - Double - Logical - Factor

Complex

All

Factor

The table above illustrates the directions for supported data coercions in R. The “From” column indicates which data types can be converted into the types listed in the “To” column.
There are numerous coercion scenarios. However, we will only cover the most important ones in this lesson.

Coercion to character/string

All data ...

Get hands-on with 1400+ tech skills courses.