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.

Summary of Data Type Functions and Usages in R
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.

1 Examples of Data Types in R

1.1 Numeric (or Double)

The numeric data type is used to store real numbers which includes whole numbers, fractions and decimals.

Create a numeric vector:

num1 = 4; num2 = 12/5; num3 = 7.5
num1; num2; num3
[1] 4
[1] 2.4
[1] 7.5

Create numeric of length 5:

numeric(5)
[1] 0 0 0 0 0

Check if numeric:

is.numeric(num1)
[1] TRUE

1.2 Integer

The integer data type is used to store whole numbers.

An L is used to assign as integer.

Create an integer vector:

int1 = 4L
int1
[1] 4
int2 = c(3L, 6L, 9L)
int2
[1] 3 6 9

Create integer of length 5:

integer(5)
[1] 0 0 0 0 0

Check if integer:

is.integer(int1)
[1] TRUE

1.3 Character

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:

chr1 = "Level 10"; chr2 = "10"
chr1; chr2
[1] "Level 10"
[1] "10"

Create character of length 5:

character(5)
[1] "" "" "" "" ""

Check if character:

is.character(chr1)
[1] TRUE

1.4 Logical

The logical data type is used to stores the boolean values of TRUE and FALSE.

Create a logical vector:

logi1 = TRUE; logi2 = FALSE
logi1; logi2
[1] TRUE
[1] FALSE

Create logical of length 5:

logical(5)
[1] FALSE FALSE FALSE FALSE FALSE

Check if logical:

is.logical(logi1)
[1] TRUE

1.5 Complex

The complex data type is used to store complex numbers.

Create a complex vector:

cplx = 3 + 2i
cplx
[1] 3+2i

Create complex of length 5:

complex(5)
[1] 0+0i 0+0i 0+0i 0+0i 0+0i

Check if complex:

is.complex(cplx)
[1] TRUE

1.6 Raw

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:

as.raw(0); as.raw(1)
[1] 00
[1] 01
as.raw(10); as.raw(11)
[1] 0a
[1] 0b
as.raw(255)
[1] ff
as.raw(c(0:1,9:10,15:16,32,160,240:241,255))
 [1] 00 01 09 0a 0f 10 20 a0 f0 f1 ff

Convert characters to raw:

charToRaw("abc")
[1] 61 62 63

2 Coercing or Converting to Other Data Types in R

2.1 Coerce or Convert to Numeric (or Double)

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:

int1 = 4L
as.numeric(int1)
[1] 4

Convert character to numeric:

chr1 = "Level 10"; chr2 = "10"
as.numeric(chr1); as.numeric(chr2)
Warning: NAs introduced by coercion
[1] NA
[1] 10

Convert logical to numeric:

logi1 = TRUE; logi2 = FALSE
as.numeric(logi1); as.numeric(logi2)
[1] 1
[1] 0

Convert complex to numeric:

cplx = 3 + 2i
as.numeric(cplx)
Warning: imaginary parts discarded in coercion
[1] 3

2.2 Coerce or Convert to Integer

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:

num1 = 5; num2 = 2.7; num3 = 0
as.integer(num1); as.integer(num2); as.integer(num3)
[1] 5
[1] 2
[1] 0

Convert character to integer:

chr1 = "Level 12"; chr2 = "12"
as.integer(chr1); as.integer(chr2)
Warning: NAs introduced by coercion
[1] NA
[1] 12

Convert logical to integer:

logi1 = TRUE; logi2 = FALSE
as.integer(logi1); as.integer(logi2)
[1] 1
[1] 0

Convert complex to integer:

cplx = 4 + 7i
as.integer(cplx)
Warning: imaginary parts discarded in coercion
[1] 4

2.3 Coerce or Convert to Character

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:

num1 = 3; num2 = 11/5; num3 = 0
as.character(num1); as.character(num2); as.character(num3)
[1] "3"
[1] "2.2"
[1] "0"

Convert integer to character:

int1 = 3L
as.character(int1)
[1] "3"

Convert logical to character:

logi1 = TRUE; logi2 = FALSE
as.character(logi1); as.character(logi2)
[1] "TRUE"
[1] "FALSE"

Convert complex to character:

cplx = 2 + 3i
as.character(cplx)
[1] "2+3i"

2.4 Coerce or Convert to Logical

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:

num1 = 7; num2 = 16/5; num3 = 0
as.logical(num1); as.logical(num2); as.logical(num3)
[1] TRUE
[1] TRUE
[1] FALSE

Convert integer to logical:

int1 = 8L
as.logical(int1)
[1] TRUE

Convert character to logical:

chr1 = "Level 7"; chr2 = "7"
as.logical(chr1); as.logical(chr2)
[1] NA
[1] NA

Convert complex to logical:

cplx = 5 + 2i
as.logical(cplx)
[1] TRUE

2.5 Coerce or Convert to Complex

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:

num1 = 7; num2 = 16/5; num3 = 0
as.complex(num1); as.complex(num2); as.complex(num3)
[1] 7+0i
[1] 3.2+0i
[1] 0+0i

Convert integer to complex:

int1 = 8L
as.complex(int1)
[1] 8+0i

Convert character to complex:

chr1 = "Level 7"; chr2 = "7"
as.complex(chr1); as.complex(chr2)
Warning: NAs introduced by coercion
[1] NA
[1] 7+0i

Convert logical to complex:

logi1 = TRUE; logi2 = FALSE
as.complex(logi1); as.complex(logi2)
[1] 1+0i
[1] 0+0i

Copyright © 2020 - 2024. All Rights Reserved by Stats Codes