Here, we discuss R data types such as numeric (or double), integer, character, logical, complex and raw, including converting to each data type.
These data types also form the building blocks of data structures in R, and several data operations and mathematical operations can be performed on them.
Function | Usage | Example |
*type*() |
Create vector of type with length n | numeric(n) |
is.*type*() |
Check if object of type | is.integer(vector) |
as.*type*() |
Coerce or convert object to type | as.character(vector) |
type is numeric, integer, character, logical, or complex.
See examples for each data type below.
The numeric data type is used to store real numbers which includes whole numbers, fractions and decimals.
Create a numeric vector:
[1] 4
[1] 2.4
[1] 7.5
Create numeric of length 5:
[1] 0 0 0 0 0
Check if numeric:
[1] TRUE
The integer data type is used to store whole numbers.
An L
is used to assign as integer.
Create an integer vector:
[1] 4
[1] 3 6 9
Create integer of length 5:
[1] 0 0 0 0 0
Check if integer:
[1] TRUE
The character data type is used to store alphabets, words and special characters.
You can use quotation marks to create characters.
Create a character vector:
[1] "Level 10"
[1] "10"
Create character of length 5:
[1] "" "" "" "" ""
Check if character:
[1] TRUE
The logical data type is used to stores the boolean values of
TRUE
and FALSE
.
Create a logical vector:
[1] TRUE
[1] FALSE
Create logical of length 5:
[1] FALSE FALSE FALSE FALSE FALSE
Check if logical:
[1] TRUE
The complex data type is used to store complex numbers.
Create a complex vector:
[1] 3+2i
Create complex of length 5:
[1] 0+0i 0+0i 0+0i 0+0i 0+0i
Check if complex:
[1] TRUE
The raw data type is used to store fixed-length sequences of bytes.
Its holds 256 values: \[00, 01,..., 09,
0a,..., 0f, 10,\]\[..., 20,...,
a0,..., f0, f1,..., ff\], matching the values in the range \([0, 1,...,255]\), with other values
assigned NA
.
Convert numbers to raw:
[1] 00
[1] 01
[1] 0a
[1] 0b
[1] ff
[1] 00 01 09 0a 0f 10 20 a0 f0 f1 ff
Convert characters to raw:
[1] 61 62 63
Note:
For character, only characters containing numbers will be converted.
For logical, TRUE
will become 1 and
FALSE
will become 0.
For complex, only real part of the complex number will be kept.
Convert integer to numeric:
[1] 4
Convert character to numeric:
Warning: NAs introduced by coercion
[1] NA
[1] 10
Convert logical to numeric:
[1] 1
[1] 0
Convert complex to numeric:
Warning: imaginary parts discarded in coercion
[1] 3
Note:
For numeric, only the integer part of fractions and decimals will be kept.
For character, only characters containing numbers will be converted.
For logical, TRUE
will become 1 and
FALSE
will become 0.
For complex, only real part of the complex number will be kept.
Convert numeric to integer:
[1] 5
[1] 2
[1] 0
Convert character to integer:
Warning: NAs introduced by coercion
[1] NA
[1] 12
Convert logical to integer:
[1] 1
[1] 0
Convert complex to integer:
Warning: imaginary parts discarded in coercion
[1] 4
Note:
For numeric and integer, they will become characters containing numbers.
For logical, TRUE
will become "TRUE" and
FALSE
will become "FALSE".
For complex, they will become a character containing a complex number.
Convert numeric to character:
[1] "3"
[1] "2.2"
[1] "0"
Convert integer to character:
[1] "3"
Convert logical to character:
[1] "TRUE"
[1] "FALSE"
Convert complex to character:
[1] "2+3i"
Note:
For numeric, integer and complex, non-zero values will become
TRUE
while zero values will become
FALSE
.
For character, NA
will be returned.
Convert numeric to logical:
[1] TRUE
[1] TRUE
[1] FALSE
Convert integer to logical:
[1] TRUE
Convert character to logical:
[1] NA
[1] NA
Convert complex to logical:
[1] TRUE
Note:
For numeric, integer, imaginary part with zero value will be added to the real number values.
For character, only characters containing numbers will be converted.
For logical, TRUE
will become \(1 + 0i\), FALSE
will become
\(0 + 0i\).
Convert numeric to complex:
[1] 7+0i
[1] 3.2+0i
[1] 0+0i
Convert integer to complex:
[1] 8+0i
Convert character to complex:
Warning: NAs introduced by coercion
[1] NA
[1] 7+0i
Convert logical to complex:
[1] 1+0i
[1] 0+0i
The feedback form is a Google form but it does not collect any personal information.
Please click on the link below to go to the Google form.
Thank You!
Go to Feedback Form
Copyright © 2020 - 2024. All Rights Reserved by Stats Codes