Converting
Indeed, while you can directly create integers in R by appending L
to a number, converting existing values to integers requires a different approach, as merely appending L
will cause an error, as shown in your example.
Certainly, that did not achieve the intended result.
R simplifies the conversion process with a set of straightforward functions that include as.double()
, as.integer()
, and as.complex()
. The function names are quite self-explanatory!
Numerical types follow a sort of 'hierarchy' where each integer
can be converted to a double
or a complex
number, and each double
can become a complex
number. However, you cannot convert complex numbers to either double
or integer
unless the imaginary part is zero. Converting a double
to an integer
will result in truncation, not rounding, of the number to its integer component.
Give these functions a try!
Tarefa
- Convert the number
9.85
to aninteger
. - Convert the number
23.8
to acomplex
number. - Convert the
integer
42
, created withL
, to adouble
.
Tudo estava claro?
Conteúdo do Curso
R Introduction: Part I
R Introduction: Part I
Converting
Indeed, while you can directly create integers in R by appending L
to a number, converting existing values to integers requires a different approach, as merely appending L
will cause an error, as shown in your example.
Certainly, that did not achieve the intended result.
R simplifies the conversion process with a set of straightforward functions that include as.double()
, as.integer()
, and as.complex()
. The function names are quite self-explanatory!
Numerical types follow a sort of 'hierarchy' where each integer
can be converted to a double
or a complex
number, and each double
can become a complex
number. However, you cannot convert complex numbers to either double
or integer
unless the imaginary part is zero. Converting a double
to an integer
will result in truncation, not rounding, of the number to its integer component.
Give these functions a try!
Tarefa
- Convert the number
9.85
to aninteger
. - Convert the number
23.8
to acomplex
number. - Convert the
integer
42
, created withL
, to adouble
.
Tudo estava claro?