Seksjon 1. Kapittel 13
single
Type 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:
12num <- 20 numL # Invalid way to convert
Correct Conversion
Instead, you should use one of these functions:
as.integer();as.double();as.complex().
123as.integer(3.8) as.double(5L) as.complex(7)
Numerical types follow a hierarchy, which defines how values can be converted:
integer → double → complexconversions are always valid;- Converting a
doubleinto anintegertruncates the decimal part; - A
complexnumber can be converted only if its imaginary part is zero.
Oppgave
Swipe to start coding
- Convert the number
9.85to aninteger. - Convert the
integer42, created withL, to adouble.
Løsning
Alt var klart?
Takk for tilbakemeldingene dine!
Seksjon 1. Kapittel 13
single
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår