Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Type Conversion | Section
Practice
Projects
Quizzes & Challenges
Frågesporter
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.
Uppgift

Swipe to start coding

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

Lösning

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 13
single

single

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

close

bookType Conversion

Svep för att visa menyn

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.
Uppgift

Swipe to start coding

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

Lösning

Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 13
single

single

some-alt