Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Type Conversion | Section
Practice
Projects
Quizzes & Challenges
Quiz
Challenges
/
Essential R Programming for Absolute Beginners - 1768563985826

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.
Tâche

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

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 13
single

single

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

close

bookType Conversion

Glissez pour afficher le 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.
Tâche

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 desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 13
single

single

some-alt