R Data Types: Vector, List, Matrix, Array, and Data frame

Every programming language has its data types, and almost all programming languages share some common data types like Integer, String, Character, Array, etc. R has an extensive type of data types, including vectors (numerical, character, logical), matrices, data frames, arrays, factors, and lists.

R Data Types

In the R language, the variables are not declared as some data type. The variables are assigned with R-Objects, and the data type of the R-object becomes the data type of the variable, unlike other programming languages.

R lang has various R-objects, which are the following.

  1. Vector
  2. List
  3. Factor
  4. Matrix
  5. Array
  6. DataFrame

Among all the above R-objects, the vector is the simplest data type in R language.

R Vector Data type

vector in R is a series of data items of the same basic type. Members in a vector are called components. When you want to create a vector with more than one element, use the c() function, combining them into a single vector.

x <- c(11, 21, 5.2, 6, -19, 0.46)
y <- c("eleven", "eight", "one")
z <- c(TRUE, FALSE, TRUE, FALSE, TRUE, FALSE)

x
typeof(x)
y
typeof(y)
z
typeof(z)

Output

[1] 11.00 21.00 5.20 6.00 -19.00 0.46
[1] "double"
[1] "eleven" "eight" "one"
[1] "character"
[1] TRUE FALSE TRUE FALSE TRUE FALSE
[1] "logical"

We have created three types of vectors using the c() function. To check the R object’s data type, use the typeof() function.

There are six types of vector objects available in R.

  1. Logical vector
  2. Numeric vector
  3. Integer vector
  4. Complex vector
  5. Character vector
  6. Raw vector

Logical Vector

logical vector is a vector that only contains TRUE and FALSE values, which means it only contains boolean values.

logical_rv <- TRUE

logical_rv
class(logical_rv)

Output

[1] TRUE
[1] "logical"

The class() function determines the class of an object or its “internal” type.

Numeric Vector

The numeric vectors create a real vector of the specified length. The elements of the vector are all equal to 0. The numeric vectors can have values like 11, 21.19, 1.1, etc.

numeric_rv <- 11.21

numeric_rv
class(numeric_rv)

Output

[1] 11.21
[1] "numeric"

And we get the numeric vector. It has as.numeric() method, which is a generic function. To check if it is a numeric value or not, use the is.numeric() method.

Integer Vector

The integer() method creates or tests for objects of type “integer“. The current implementation of R uses the 32-bit integers for integer vectors. Integer vectors can have values like 21L, 19L, 0L, etc.

integer_rv <- 21L
integer_rv
class(integer_rv)

Output

[1] 21
[1] "integer"

The as.integer() function tries to coerce its argument to be of integer type. To check if the value is an integer or not, use the is.integer() function.

Complex Vector

The complex vector in R can be specified either by providing its length, its real and imaginary parts, or modulus and argument. The complex vector can have values like 1 + 2i, 2 + 5i, 1 + 0i, etc.

complex_rv <- 19 + 21i
complex_rv
class(complex_rv)

Output

[1] 19+21i
[1] "complex"

The as.complex() function tries to control its argument to be of complex type. To check if the data type is complex or not, use the is.complex() method. All forms of NA and NAN are coerced to a complex NA, for which both the real and imaginary parts are NA.

Character Vector

Text in R is represented by character vectors. A character vector is a vector consisting of characters. To create a character vector, use the characters in double or single-quotes. You can have character vector values like x‘ , “data”,TRUE“, ‘21.19‘.

character_rv <- "a + b + c"
character_rv
class(character_rv)

Output

[1] "a + b + c"
[1] "character"

You can assign a value to a character vector using the assignment operator (<-), the same way you do for all other data types variables. To find out the length of the vector, use the length() function. To check if the variable holds a character vector or not, use the is.character() function.

Raw Vector

The raw vector data type is intended to hold raw bytes. A raw vector is printed with each byte separately represented as a pair of hex digits. If you want to see a character representation (with escape sequences for non-printing characters), use the rawToChar() function.

The Hello” is stored as 48 65 6c 6c 6f as raw values. Let’s see with examples.

raw <- raw(8)
raw
class(raw)

raw_rv <- charToRaw("Database")
raw_rv
class(raw_rv)

Output

[1] 00 00 00 00 00 00 00 00
[1] "raw"
[1] 44 61 74 61 62 61 73 65
[1] "raw"

To convert raw to a character in R, use the rawToChar() function.

raw_rv <- charToRaw("Database")
raw_rv
class(raw_rv)

char_rv <- rawToChar(raw_rv)
char_rv
class(char_rv)

Output

[1] 44 61 74 61 62 61 73 65
[1] "raw"
[1] "Database"
[1] "character"

The as.raw() function tries to curb its argument to be of raw type. The answer will be 0 unless the coercion succeeds.
The is.raw() function returns TRUE if and only if typeof(x) == “raw”.

List in R

A list is an R-object containing different types of elements inside it like vectors, functions, and even another list inside it. A list is an ordered collection of elements under one name.

r_list <- list(c(11, 19, 21), 19.3, cos, TRUE)
r_list

Output

[[1]]
[1] 11 19 21

[[2]]
[1] 19.3

[[3]]
function (x) .Primitive("cos")

[[4]]
[1] TRUE

To access list elements one by one, use the [[ ]] brackets. Let’s access the third element of the list. Remember, the list index starts at 1 and not 0.

r_list <- list(c(11, 19, 21), 19.3, cos, TRUE)
r_list[[3]]

Output

function (x) .Primitive("cos")

Factor in R

The factor stores the nominal values as a vector of integers in the specific range and an internal vector of character strings mapped to these integers.

rv <- c(rep("adam", 3), rep("eve", 4))
r_factor <- factor(rv)
r_factor

Output

[1] adam adam adam eve eve eve eve
Levels: adam eve

R treat factors as nominal variables and ordered factors as ordinal variables in statistical procedures and graphical analyses.

Matrix in R

A matrix is a two-dimensional rectangular data structure that can be created using a vector input to the matrix function. The as.matrix() function attempts to turn its argument into the matrix. The is.matrix() function tests if its argument is a (strict) matrix.

mtrx <- matrix(c("a", "b", "c", "d"), nrow = 2, ncol = 2, byrow = TRUE)
mtrx

Output

      [,1]   [,2]
[1,]   "a"   "b"
[2,]   "c"   "d"

Array in R

Array in R is a list or vector with two or more dimensions. An array is like a stacked matrix, and a matrix is a two-dimensional array.

rv <- c(11, 19, 18)
rv2 <- c(21, 6, 29, 46, 37, 38)

result <- array(c(rv, rv2), dim = c(3, 3, 2))
print(result)

Output

, , 1

    [,1] [,2] [,3]
[1,] 11   21   46
[2,] 19    6   37
[3,] 18   29   38

, , 2

     [,1] [,2] [,3]
[1,]  11   21   46
[2,]  19    6   37
[3,]  18   29   38

Data Frame in R

Data Frame in R is a table or two-dimensional array-like structure in which a row contains a set of values, and each column holds values of one variable.

streaming <- data.frame(
 service_id = c(1:5),
 service_name = c("Netflix", "Disney+", "HBOMAX", "Hulu", "Peacock"),
 service_price = c(18, 10, 15, 7, 12),
 stringsAsFactors = FALSE
)
# Print the data frame.
print(streaming)

Output

   service_id   service_name  service_price
1    1           Netflix        18
2    2           Disney+        10
3    3           HBOMAX         15
4    4           Hulu            7
5    5           Peacock        12

Unlike a matrix, in a data frame, each column can contain different modes of data. Data Frames are created using the data.frame() function.

Useful inbuilt R Functions

Inbuilt functions are also the data types in R.

length(object)
str(object)
class(object)
names(object)

c(object,object,...)
cbind(object, object, ...)
rbind(object, object, ...)

That is it for the R Data Types example.

Leave a Comment