##DATA STRUCTURE
#Vectors A vector is the basic data structure in R, or we can say vectors are the most basic R data objects. There are six types of atomic vectors such as logical, integer, character, double, and raw. “A vector is a collection of elements which is most commonly of mode character, integer, logical or numeric” A vector can be one of the following two types:
vec1 <- c(1,2,3,4,5)
vec1
## [1] 1 2 3 4 5
vec2 <- c('a', 'b', 'c', 4, 'T')
vec2
## [1] "a" "b" "c" "4" "T"
is.vector(vec2)
## [1] TRUE
comb = cbind(vec1,vec2)
comb
## vec1 vec2
## [1,] "1" "a"
## [2,] "2" "b"
## [3,] "3" "c"
## [4,] "4" "4"
## [5,] "5" "T"
In R, the list is the container. Unlike an atomic vector, the list is not restricted to be a single mode. A list contains a mixture of data types. The list is also known as generic vectors because the element of the list can be of any type of R object. “A list is a special type of vector in which each element can be a different type.”
obj1 <- c(1,2,3,4,5)
obj2 <- c('a', 'b', 'c', 4, 'T')
list1= list(c(obj1,obj2))
print(list1)
## [[1]]
## [1] "1" "2" "3" "4" "5" "a" "b" "c" "4" "T"
There is another type of data objects which can store data in more than two dimensions known as arrays. “An array is a collection of a similar data type with contiguous memory allocation.” Suppose, if we create an array of dimension (2, 3, 4) then it creates four rectangular matrices of two rows and three columns.
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.
myarr = array(c(1:32), c(4,4,2))
print(myarr)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 1 5 9 13
## [2,] 2 6 10 14
## [3,] 3 7 11 15
## [4,] 4 8 12 16
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 17 21 25 29
## [2,] 18 22 26 30
## [3,] 19 23 27 31
## [4,] 20 24 28 32
A matrix is an R object in which the elements are arranged in a two-dimensional rectangular layout. In the matrix, elements of the same atomic types are contained. For mathematical calculation, this can use a matrix containing the numeric element. A matrix is created with the help of the matrix() function in R.
mat1 = matrix(1:20, nrow = 4, ncol = 5)
mat1
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 5 9 13 17
## [2,] 2 6 10 14 18
## [3,] 3 7 11 15 19
## [4,] 4 8 12 16 20
row1 = mat1[1,]
row1
## [1] 1 5 9 13 17
col1 = mat1[,1]
col1
## [1] 1 2 3 4
A data frame is a two-dimensional array-like structure, or we can say it is a table in which each column contains the value of one variable, and row contains the set of value from each column.
stud_id = c(1:10)
stud_Name = c('Ade Oluwa', 'Bala Baba', 'Kings Lawal', 'Paul Azige', 'Nzebe Jude', 'Olusegun Bolanle', 'Olu Henry', 'Prince Charlse', 'Harry Papa', 'Ben Ole')
stud_Name
## [1] "Ade Oluwa" "Bala Baba" "Kings Lawal" "Paul Azige"
## [5] "Nzebe Jude" "Olusegun Bolanle" "Olu Henry" "Prince Charlse"
## [9] "Harry Papa" "Ben Ole"
stud_CGPA = c(2.5, 4.6, 3.7, 4.5, 2.89, 4.45, 4.49, 5.0, 4.22, 2.7)
df = data.frame(stud_id, stud_Name, stud_CGPA)
print(df)
## stud_id stud_Name stud_CGPA
## 1 1 Ade Oluwa 2.50
## 2 2 Bala Baba 4.60
## 3 3 Kings Lawal 3.70
## 4 4 Paul Azige 4.50
## 5 5 Nzebe Jude 2.89
## 6 6 Olusegun Bolanle 4.45
## 7 7 Olu Henry 4.49
## 8 8 Prince Charlse 5.00
## 9 9 Harry Papa 4.22
## 10 10 Ben Ole 2.70
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
id <- c(1:5)
name <- c("Tolulope Alade", "Kunle Ipaye", "Sidi Mola", "Henry Sele", "Julius Marcus")
jobTitle <- c("Programmer", "Analyst", "Backend Expert", "Site Engineer", "Frontend Expert")
emp = data.frame(id, name, jobTitle)
print(emp)
## id name jobTitle
## 1 1 Tolulope Alade Programmer
## 2 2 Kunle Ipaye Analyst
## 3 3 Sidi Mola Backend Expert
## 4 4 Henry Sele Site Engineer
## 5 5 Julius Marcus Frontend Expert
empSep = separate(emp, name, into = c('Last', 'First'), ' ')
print(empSep)
## id Last First jobTitle
## 1 1 Tolulope Alade Programmer
## 2 2 Kunle Ipaye Analyst
## 3 3 Sidi Mola Backend Expert
## 4 4 Henry Sele Site Engineer
## 5 5 Julius Marcus Frontend Expert
unite(empSep, 'FullName', Last, First, sep = ' ')
## id FullName jobTitle
## 1 1 Tolulope Alade Programmer
## 2 2 Kunle Ipaye Analyst
## 3 3 Sidi Mola Backend Expert
## 4 4 Henry Sele Site Engineer
## 5 5 Julius Marcus Frontend Expert
mutate(df, Marital_Status = c(T,F,F,T,T,F,F,F,F,T))
## stud_id stud_Name stud_CGPA Marital_Status
## 1 1 Ade Oluwa 2.50 TRUE
## 2 2 Bala Baba 4.60 FALSE
## 3 3 Kings Lawal 3.70 FALSE
## 4 4 Paul Azige 4.50 TRUE
## 5 5 Nzebe Jude 2.89 TRUE
## 6 6 Olusegun Bolanle 4.45 FALSE
## 7 7 Olu Henry 4.49 FALSE
## 8 8 Prince Charlse 5.00 FALSE
## 9 9 Harry Papa 4.22 FALSE
## 10 10 Ben Ole 2.70 TRUE
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 that factors are very useful in columns. It is very useful in data analysis for statistical modeling.
Factors are created with the help of factor() function by taking a vector as an input parameter.
Syntax
(ff <- factor(substring("statistics", 1:10, 1:10), levels = letters))
## [1] s t a t i s t i c s
## Levels: a b c d e f g h i j k l m n o p q r s t u v w x y z
kk = factor(letters[1:20], labels = "letter")
kk
## [1] letter1 letter2 letter3 letter4 letter5 letter6 letter7 letter8
## [9] letter9 letter10 letter11 letter12 letter13 letter14 letter15 letter16
## [17] letter17 letter18 letter19 letter20
## 20 Levels: letter1 letter2 letter3 letter4 letter5 letter6 letter7 ... letter20
pa = factor(1:9)
pa
## [1] 1 2 3 4 5 6 7 8 9
## Levels: 1 2 3 4 5 6 7 8 9