R programming - Data Structures

R programming - Data Structures

A program for statistical analysis - Part 7

Hitendra K's photo
Hitendra K
·Jul 21, 2022·

3 min read

Subscribe to our newsletter and never miss any upcoming articles

Play this article

In the previous article, we discussed data structures and their types. In this blog, we will discuss the same elaborately.

So there are various types of data structures :

1. Vectors

Vectors are the most basic data structures in R. It consists of Basic data types of a given length. These are one-dimensional data structures. There are various operations we can perform in vectors.

Vector arithmetic:

Arithmetic operations like addition, subtraction, and multiplication can be performed and the result is obtained as a vector datatype.

var1 <- c(3,8,4,5,0,11)
var2 <- c(4,11,0,8,1,2)

// Vector addition.
add.result <- var1+var2 
print(add.result)

// Vector subtraction.
sub.result <- var1-var2 
print(sub.result)

// Vector multiplication.
multi.result <- var1*var2 
print(multi.result)

// Vector division.
divi.result <- var1/var2 
print(divi.result)

//output
7 19 4 13 1 13

-1 -3 4 -3 -1 9
12 88 0 40 0 22
0.7500000 0.7272727  0.6250000 0.0000000 5.5000000

Vector Element Recycling

vectors with an unequal number of elements can be solved using arithmetic operations. The elements of the smaller vector are recycled to complete the operations.

var1 <- c(3,8,4,5,0,11)
var2 <- c(4,11)

add.result <- var1+var2
print(add.result)

sub.result <- var1-var2
print(sub.result)

//output
7 19 8 16 4 22
-1 -3 0 -6 -4 0

2. Lists

A list is an ordered collection of objects. It consists of a mixture of various data types. Therefore, A list can be defined as a special type of vector in which each element can be a different type.

Manipulating List elements

We can add, delete and update list elements. But we can update any element.

list2 <- list(matrix(c(3,9,5,1,-2,8), nrow = 2), c("Jan","Feb","Mar"), list(3,4,5))
list2[4] <- “HELLO”
print(list2[4])

//output
"Hello"

3. Dataframes

A dataframe is a table or a two-dimensional array-like structure in which each column contains the value of one variable and each row contains one set of values from each column.

Example of Dataframe:

Name = c("Amiya", "Raj", "Asish")
Language = c("R", "Python", "Java")
Age = c(22, 25, 45)
df = data.frame(Name, Language, Age)
print(df)

//Output
   Name  Language  Age
1  Amiya        R          22
2  Raj         Python    25
3  Asish     Java        45

4. Matrices

A matrix is an arrangement of numbers in rows and columns. Rows are the horizontally aligned values and vertically aligned values are called columns.

To create a matrix in R you need to use the function called matrix.

A = matrix(

    c(1, 2, 3, 4, 5, 6, 7, 8, 9),


    nrow = 3, ncol = 3, 

    byrow = TRUE                            
)

print(A)

//output
1 2 3
4 5 6 
7 8 9

5. Arrays

Arrays are data structures which store data of more than two dimensions. An array is a collection of a similar data type with contiguous memory allocation. In R, an array is created with the help of array() function. This function takes a vector as an input and uses the value in the dim parameter to create an array.

A = array(

    c(1, 2, 3, 4, 5, 6, 7, 8),


    dim = c(2, 2, 2)                       
)

print(A)

//output
, , 1

   1     3
   2    4

, , 2

    5    7
    6    8

6. Factors

Factors are also data objects that are used to categorize the data and store it as levels. Factors can store both strings and integers. Columns have a limited number of unique values so factors are very useful in columns. They can store both strings and integers. They are useful to categorize unique values in columns like “TRUE” or “FALSE”, or “MALE” or “FEMALE”, etc. It is very useful in data analysis for statistical modelling.

Factors are created with the help of factor() function by taking a vector as an input parameter.

fac = factor(c("Male", "Female", "Male",
               "Male", "Female", "Male", "Female"))

print(fac)

//output

Male   Female Male   Male   Female Male   Female
Levels: Female Male

That is up for this blog. we can discuss oop's concept libraries in R in the upcoming blogs. Follow Quasar community for more and also do subscribe for our newsletter.

 
Share this