Data Structures in R | A Cheat Sheet to R Data Structures

1. Data Structures in R

In this article, we will study Data Structures in R. Also, will study every data object of Data Structures. Along with this, we will also learn R objects syntax, applications, and examples. Moreover, will study R Data Structures for different operations.

Data Structures in R | A Cheat Sheet to R Data Structures

Data Structures in R | A Cheat Sheet to R Data Structures

2.Introduction to Data Structures in R

In any programming language, if you are doing programming, you need to use various variables to store different data. Moreover, variables are reserved in a memory location to store values. Also, this implies that, once you create a variable you reserve some area in memory. Further, data structures are the only way of arranging data. So it can be used efficiently in a computer.
If we see in contrast to different programming languages like C and Java, R doesn’t have variables declared as some data type. Further, the variables are appointed with R-Objects and the knowledge form of the R-object becomes the datatype of the variable. Also, there are many types of R-objects. The popularly used ones are

  • Vector
  • Matrix
  • Array
  • Lists
  • DataFrames

Now, we will discuss each of these R-objects in brief

3. Vectors in R

The basic data structure in R is the vector. It comes in two parts: Atomic vectors and Lists. They have three common properties:

  • Type function, what it is?
  • Length function, how many elements it contains.
  • Attribute function, extra arbitrary metadata.

They differ in the types of their elements: All elements of an atomic vector must be the same type, whereas the elements of a list can have different types. We have discussed R Lists in our previous blog, Here we are going to discuss only Atomic Vectors.
Atomic Vectors
There are four common types of R Atomic Vectors:

  • Numeric Data Type
  • Integer Data Type
  • Character Data Type
  • Logical Data Type

Read Next: R Vectors – Types and R Vector Operations with Examples

4. Matrix – Data Structures in R

First of all, we will discuss what exactly matrices in Data Structures in R are. A matrix is a two-dimensional rectangular data set and thus it can be created using vector input to the matrix function. In addition, a matrix is a collection of numbers arranged into a fixed number of rows and columns. Usually, the numbers are the real numbers. By using a matrix function we can reproduce a memory representation of the matrix in R. Hence, the data elements must be of the same basic type.

A = matrix (
c(2 , 4, 3, 1, 5, 7)        # the data elements
nrow =2,                     # no. of rows
ncol =3,                      # no. of columns
byrow = TRUE)          # fill matrix by rows
A                                 # print the matrix
[,1]    [,2]    [,3] [1,]     2       4       3
[2,]     1       5       7

An element at the mth row, the nth column of A can be accessed by using this expression A[m, n]

A[2, 3]      # element at 2nd row, 3rd column
[1] 7

The entire mth row A can extract as A[m, ].

A[2,]        # the 2nd row
[1] 1 5 7

The entire nth column A can extracted as A[ ,n].
A[ ,3]       # the 3rd column
[1] 3

i. Uses of Matrices

Method to solve the matrices
a. Adding
To add two matrices, add the no. in matching positions:
For Example:
[1]  [2]                [1]  [2]                   [1]  [2] [1]   2   4       +   [1]   1    3      =     [1]      3   7
[2]  3    6            [2]   8    5             [2]     11  11
These are the calculations:
2+1=3 4+3=7
3+8=11 6+5=11
b. Negative
The negative of the matrix is also simple:
For Example:
[1] [2]             [1]    [2] [1]  2 -4     = [1]   -2      4
[2] -3 6         [2]    3     -6
c. Subtracting
To subtract two matrices, subtract the no. in matching positions:
For Example:
[1]  [2]                [1]  [2]                [1]  [2] [1]     3    8    –     [1]   4     0        = [1]   -1    8
[2]     4    6          [2]   1    -9           [2]     3    15
These are the calculations:
3-4=- 1 8-0=8
4-1=3 6-(- 9) =15
d. Multiply by a constant
We can multiply a matrix by some value:
For Example:
[1] [2]              [1]  [2] 2 *  [1]   4    0     =  [1]   8   0
[2]   1    -9        [2]   2 -18
e. Multiplying a matrix by another matrix
[1]  [2]  [3]                    [1]     [2]                    [1]    [2] [1]  1    2    3        *      [1]   7        8          =  [1]   58    64
[2] 4     5    6                [2]   9      10              [2]   139  154
[3] 11      12
The “dot product” is where you multiply matching members, sum up:
These are the calculations:
(1*7+2*9+3*7) + (1*8+2*10+3*12) = 58 64
(4*7+5*9+6*11) + (4*8+5*10+6*12) 139 154
f. Dividing
A/B = A * (1/B) = A * inverse (B)
g. Transposing
To “transpose” a matrix swaps rows and columns:
For Example:
[1]    [2]                  [1]    [2]   [3] [1]   6       4         = [1]    6       1     2
[2]   1     – 9            [2]    4      -9     4
[3]   2       4
h. Order of Multiplication
When you change the order of multiplication, the answer is different. It means AB is not equaled to BA.
i. Identity Matrix
The “identity matrix” is the equal of the number “1”
[1]   [2]   [3] [1]     1     0      0
[2]     0     1      0
[3]     0     0      1
It is a special matrix because when you multiply by it, the original matrix remains unchanged:
I * A = A
A * I = A
Read: R Lists – How to Create, Access, Manipulate, Merge Lists in R?

ii. Applications of Matrices

a. In geology, Matrices is been used for taking surveys and hence, used for plotting graphs, statistics, and studies in almost different fields.
b. To represent the real world data are like traits of people’s population. They are best representation method for plotting common survey things.
c. In robotics and automation, matrices are the best elements for the robot movements.
d. Matrices are used in calculating the gross domestic products in economics. Therefore, it helps in calculating the goods product Efficiency.
e. In computer-based application, matrices play a vital role in the projection of three-dimensional image into two-dimensional screen creating the realistic seeming motions.
f. In physical related applications, matrices can be applied in the study of an electrical circuit.
Learn more about R matrix.

5. Data Structures in R – Array

We use to call array in R Programming simply called the multi-dimensional Data structure. In this, data is stored in the form of matrices, row, and as well as in columns. We can use matrix level, row index, and column index to access the matrix elements.
Arrays in R are the data objects which can store data in more than two dimensions. An array is created using the array() function. We can use vectors as input. To create an array we can use this values in the dim parameter.
Knowledge of vector functions is also necessary with the concept of vectors.So, to learn this you can follow the below-mentioned link:
R Vector Function
For Example:
In this following example, we will create an array in R of two 3×3 matrices each with 3 rows and 3 columns.
# Create two vectors of different lengths.

vector1 <- c(2,9,3)
vector2 <- c(10,16,17,13,11,15)

# Take these vectors as input to the array.

result <- array(c(vector1,vector2),dim = c(3,3,2))
print(result)

When we execute the above code, it produces the following result
, , 1

[,1]   [,2]    [,3] [1,]      2     10     13
[2,]      9     16     11
[3,]      3     17     15

, , 2

[,1]   [,2]    [,3] [1,]       2   10      13
[2,]       9   16      11
[3,]       3   17      15

Read: Array in R Programming – Array Function and ways to create array

a. Different Operations on Rows and Columns

1. Naming Columns And Rows
We can give names to the rows, columns, and matrices in the array by using the dimnames parameter.
# Create two vectors of different lengths.

vector1 <- c(2,9,6)
vector2 <- c(10,15,13,16,11,12)
column.names <- c(“COL1″,”COL2″,”COL3”)
row.names <- c(“ROW1″,”ROW2″,”ROW3”)
matrix.names <- c(“Matrix1″,”Matrix2”)

# Take these vectors as input to the array.

result <- array(c(vector1,vector2),dim = c(3,3,2),dimnames = list(row.names,column.names,
matrix.names))
print(result)

When we execute the above code, it produces the following result
, , Matrix1

COL1   COL2     COL3
ROW1      2          10           16
ROW2      9          15           11
ROW3      6          13           12

, , Matrix2

COL1   COL2   COL3
ROW1       2         10          16
ROW2       9         15          11
ROW3       6         13          12
Read: R Data Frame Introduction & Operations with Examples
2. Accessing Array Elements
# We will create two vectors of different lengths.

vector1 <- c(2,9,6)
vector2 <- c(10,15,13,16,11,12)
column.names <- c(“COL1″,”COL2″,”COL3”)
row.names <- c(“ROW1″,”ROW2″,”ROW3”)
matrix.names <- c(“Matrix1″,”Matrix2”)

# Now, we will take these vectors as input to the array.

result <- array(c(vector1,vector2),dim = c(3,3,2),dimnames = list(row.names,column.names,
matrix.names))
print(result)

# Print the third row of the second matrix of the array.

print(result[3,,2])

# Print the element in the 1st row and 3rd column of the 1st matrix.

print(result[1,3,1])

# Print the 2nd Matrix.

print(result[,,2])

When we execute the above code, it produces the following result −
COL1 COL2 COL3
6         13   12
[1] 16
COL1   COL2   COL3
ROW1       2         10          16
ROW2       9         15          11
ROW3       6         13          12
Read: R Factor – Introduction to Factors & Factor Functions in R
3. Manipulating Array Elements
As the array is made up matrices in multiple dimensions, the operations on elements of an array are carried out by accessing elements of the matrices.
# Create two vectors of different lengths.

vector1 <- c(1,2,3)
vector2 <- c(3,4,5,6,7,8)

# Take these vectors as input to the array.

array1 <- array(c(vector1,vector2),dim = c(3,3,2))

# Create two vectors of different lengths.

vector3 <- c(3,2,1)
vector4 <- c(8,7,6,5,4,3)
array2 <- array(c(vector1,vector2),dim = c(3,3,2))

# create matrices from these arrays.

matrix1 <- array1[,,2]
matrix2 <- array2[,,2]

# Add the matrices.

result <- matrix1+matrix2
print(result)

When we execute the above code, it produces the following result
[,1]       [,2]      [,3] [1,]      3         11       11
[2,]      4         11       11
[3,]      4         11       11

Read: Control Structures in R Programming with Examples

4. Calculations Across R Array Elements
We will be using the apply()function for calculations in an array in R.
Syntax

apply(x, margin, fun)

Following is the description of the parameters used

  • x is an array.
  • a margin is the name of the dataset used.
  • fun is the function to be applied to the elements of the array.

For Example:
We use the apply() function below in different ways. To calculate the sum of the elements in the rows of an array across all the matrices.
# We will create two vectors of different lengths.

vector1 <- c(1,2,3)
vector2 <- c(3,4,5,6,7,8)

# Now, we will take these vectors as input to the array.

new.array <- array(c(vector1,vector2),dim = c(3,3,2))
print(new.array)

# Use apply to calculate the sum of the rows across all the matrices.

result <- apply(new.array, c(1), sum)
print(result)

When we execute the above code, it produces the following result
, , 1

[,1]    [,2]    [,3] [1,]    1        3       6
[2,]    2        4       7
[3,]    3        5       8

, , 2

[,1]    [,2]    [,3] [1,]     1        3       6
[2,]     2        4       7
[3,]     3        5       8
[1]  [20] [26] [32] Read more about Arrays in R.

6. List in R

Lists are the object which Contains elements of different types – like strings, numbers, vectors and another list inside it. A list can also contain a matrix or a function as its elements. The List is been created using list() Function. In other words, a list is a generic vector containing other objects.
For Example:
The variable x is containing copies of three vectors n, s, b and a numeric value 3.
n = c(2, 3, 5)
s =  c(“aa”,  “bb”,  “cc”,  “dd”, “ee” )
b = c(TRUE,  FALSE,  TRUE,  FALSE,  FALSE )
x = list( n, s, b, 3)        # x contains copies of n, s, b)
Read more about R list in detail

R Quiz

7. Data frame in R

First of all, we are going to discuss from where the concept of data frame came. The concept comes from the world of the statistical software used in empirical research. It generally refers to tabular data: a data structure representing the cases (rows), each of which consists of numbers of observation or measurement (columns).
A data frame is been used for storing data tables. It is a list of vectors of equal length.
For example:
The following variable df is data frame Containing three variables n, s, b.
n = c(2, 3, 5 )
S = c(“a” , “b” , “c”)
b = c(TRUE, FALSE, TRUE )
df = data.frame ( n, s, b ) # df is a data frame
A data frame is an array. Unlike an array, the data we store in the columns of the data frame can be of various types. I.e., one column might be a numeric variable, another might be a factor, and a third might be a character variable. All columns have to be the same length.
Characteristics of a data frame:

  • The column names should be non-empty.
  • The row names should be unique.
  • The data stored in a data frame can be of numeric, factor or character type.
  • Each column should contain the same number of data items.

Read more about Data frame in R

So, this was all in Data Structures in R. Hope you like our explanation.

8. Conclusion

As a result, we have studies Data Structures in R. Also, studied important R objects which are part of Data Structures in R. Furthermore, if you have any query regarding Data Structures in R, feel free to ask in the comment section.

2 Responses

  1. Shivangi says:

    The answer of the matrix(execution code is wrong). It should be (20,26,32) instead of (26,26,35). Correct it in case if anyone is new too it.

    • DataFlair Team says:

      Hey Shivangi,
      Thanks for pointing our typo. We have made the necessary changes. From now, you can learn Data structure in R programming freely.
      Keep visiting and keep learning

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.