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

bookType Conversion

Numbers can be converted between different types using built-in functions. This allows you to work with values as integers, doubles, or complex numbers when needed.

Incorrect Conversion

Simply appending L to an existing variable does not convert it:

12
num <- 20 numL # Invalid way to convert
copy

Correct Conversion

Instead, you should use one of these functions:

  • as.integer();
  • as.double();
  • as.complex().
123
as.integer(3.8) as.double(5L) as.complex(7)
copy

Numerical types follow a hierarchy, which defines how values can be converted:

  • integer β†’ double β†’ complex conversions are always valid;
  • Converting a double into an integer truncates the decimal part;
  • A complex number can be converted only if its imaginary part is zero.
Task

Swipe to start coding

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

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 3
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

close

Awesome!

Completion rate improved to 2.27

bookType Conversion

Swipe to show menu

Numbers can be converted between different types using built-in functions. This allows you to work with values as integers, doubles, or complex numbers when needed.

Incorrect Conversion

Simply appending L to an existing variable does not convert it:

12
num <- 20 numL # Invalid way to convert
copy

Correct Conversion

Instead, you should use one of these functions:

  • as.integer();
  • as.double();
  • as.complex().
123
as.integer(3.8) as.double(5L) as.complex(7)
copy

Numerical types follow a hierarchy, which defines how values can be converted:

  • integer β†’ double β†’ complex conversions are always valid;
  • Converting a double into an integer truncates the decimal part;
  • A complex number can be converted only if its imaginary part is zero.
Task

Swipe to start coding

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

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 3
single

single

some-alt