Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Type Conversion | Section
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.
Oppgave

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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 13
single

single

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

close

bookType Conversion

Sveip for å vise menyen

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

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 desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 13
single

single

some-alt