Type 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:
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 β complex
conversions are always valid;- Converting a
double
into aninteger
truncates the decimal part; - A
complex
number can be converted only if its imaginary part is zero.
Swipe to start coding
- Convert the number
9.85
to aninteger
. - Convert the
integer
42
, created withL
, to adouble
.
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 2.27
Type 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:
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 β complex
conversions are always valid;- Converting a
double
into aninteger
truncates the decimal part; - A
complex
number can be converted only if its imaginary part is zero.
Swipe to start coding
- Convert the number
9.85
to aninteger
. - Convert the
integer
42
, created withL
, to adouble
.
Solution
Thanks for your feedback!
single