Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Type Conversion | Basic Data Types and Vectors
R Introduction: Part I

Type Conversion

Indeed, while you can directly create integers in R by appending L to a number, converting existing values to integers requires a different approach. Simply appending L will cause an error, as demonstrated in the example below:

Certainly, that did not achieve the intended result.

Luckily, R simplifies the conversion process with straightforward functions like as.double(), as.integer(), and as.complex(). The function names are quite self-explanatory!

Numerical types follow a sort of 'hierarchy' where each integer can be converted to a double or a complex number, and each double can become a complex number. However, converting a double to an integer will result in truncation, not rounding, of the number to its integer component.

You cannot convert complex numbers to either double or integer unless the imaginary part is zero.

Give these functions a try!

Task

  1. Convert the number 9.85 to an integer.
  2. Convert the integer 42, created with L, to a double.

Everything was clear?

Section 2. Chapter 3
toggle bottom row
course content

Course Content

R Introduction: Part I

Type Conversion

Indeed, while you can directly create integers in R by appending L to a number, converting existing values to integers requires a different approach. Simply appending L will cause an error, as demonstrated in the example below:

Certainly, that did not achieve the intended result.

Luckily, R simplifies the conversion process with straightforward functions like as.double(), as.integer(), and as.complex(). The function names are quite self-explanatory!

Numerical types follow a sort of 'hierarchy' where each integer can be converted to a double or a complex number, and each double can become a complex number. However, converting a double to an integer will result in truncation, not rounding, of the number to its integer component.

You cannot convert complex numbers to either double or integer unless the imaginary part is zero.

Give these functions a try!

Task

  1. Convert the number 9.85 to an integer.
  2. Convert the integer 42, created with L, to a double.

Everything was clear?

Section 2. Chapter 3
toggle bottom row
some-alt