Data Structure in
Data Structure in
Creating Vectors:-
A vector is simply a list of items that are of the same type.
To combine the list of items to a vector, use the c() function and separate
the items by a comma.
In the example below, we create a vector variable called fruits, that
combine strings:
Example
# Vector of strings
fruits <- c("banana", "apple", "orange")
# Print fruits
fruits
Example
# R program to illustrate Vector
Example
# Vector of numerical values
numbers <- c(1, 2, 3)
# Print numbers
numbers
Example
# Vector with numerical values in a sequence
numbers <- 1:10
numbers
You can also create numerical values with decimals in a sequence, but
note that if the last element does not belong to the sequence, it is not used:
Example
# Vector with numerical decimals in a sequence where the last element is not
used
numbers2 <- 1.5:6.3
numbers2
Result:
Vector Length
To find out how many items a vector has, use the length() function:
Example
fruits <- c("banana", "apple", "orange")
length(fruits)
Sort a Vector
To sort items in a vector alphabetically or numerically, use
the sort() function:
Example
Example
Change an Item
To change the value of a specific item, refer to the index number:
Example
fruits <- c("banana", "apple", "orange", "mango", "lemon")
# Print fruits
fruits
Repeat Vectors
repeat_each
Example
Repeat the sequence of the vector:
repeat_times <- rep(c(1,2,3), times = 3)
repeat_times
Example
repeat_indepent
numbers
To make bigger or smaller steps in a sequence, use the seq() function:
Example
numbers <- seq(from = 0, to = 100, by = 20)
numbers
Note: The seq() function has three parameters: from is where the
sequence starts, to is where the sequence stops, and by is the interval of
the sequence.
Example
Lists are the R objects which contain elements of different types like −
numbers, strings, vectors and another list inside it. A list can also contain
a matrix or a function as its elements. List is created using list() function.
A list in R can contain many different data types inside it. A list is a
collection of data which is ordered and changeable.
[[1]]
[1] "Red"
[[2]]
[1] "Green"
[[3]]
[1] 21 32 11
[[4]]
[1] TRUE
[[5]]
[1] 51.23
[[6]]
[1] 119.1
Matrices
Lists Examples :-
Example
# List of strings
thislist <- list("apple", "banana", "cherry")
Access Lists
You can access the list items by referring to its index number, inside
brackets. The first item has index 1, the second item has index 2, and so
on:
Example
thislist <- list("apple", "banana", "cherry")
thislist[1]
Example
List Length
To find out how many items a list has, use the length() function:
Example
length(thislist)
Output : - true
Example
Example
Note: The search will start at index 2 (included) and end at index 5
(included). Remember that the first item has index 1.
The most common way is to use the c() function, which combines two
elements together:
Example
list3
Arrays: -
Arrays are the R data objects which can store data in more than two
dimensions. For example − If we create an array of dimension (2, 3, 4)
then it creates 4 rectangular matrices each with 2 rows and 3 columns.
Arrays can store only data type.
Example
The following example creates an array of two 3x3 matrices each with 3
rows and 3 columns.
# Create two vectors of different lengths.
vector1 <- c(5,9,3)
vector2 <- c(10,11,12,13,14,15)
1
[1] [2] [3]
[1,] 5 10 13
[2,] 9 11 14
[3,] 3 12 15
2
[1] [2] [3]
[1,] 5 10 13
[2,] 9 11 14
[3,] 3 12 15
We can give names to the rows, columns and matrices in the array by
using the dimensions parameter.
Matrix1
COL1 COL2 COL3
ROW1 5 10 13
ROW2 9 11 14
ROW3 3 12 15
Matrix2
COL1 COL2 COL3
ROW1 5 10 13
ROW2 9 11 14
ROW3 3 12 15
# Print the element in the 1st row and 3rd column of the 1st matrix.
print(result[1,3,1])
For Practice :-
print(result[,,2])
print(result[,2,2])
print(result[1,3,1])
When we execute the above code, it produces the following result −
Factors are the data objects which are used to categorize the data and
store it as levels. They can store both strings and integers. They are useful
in the columns which have a limited number of unique values. Like
"Male, "Female" and True, False etc. They are useful in data analysis for
statistical modelling.
Demography: Male/Female
Music: Rock, Pop, Classic, Jazz
Training: Strength, Stamina
Example: -
Output:-
Levels: Female Male
R Data Frames
Data Frames can have different types of data inside it. While the first
column can be character, the second and third can be numeric or logical.
However, each column should have the same type of data.
Example
# Create a data frame
Data_Frame <- data.frame (
Training = c("Strength", "Stamina", "Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30, 45)
)
# Print the data frame
Data_Frame
Use the summary() function to summarize the data from a Data Frame:
Example
Data_Frame <- data.frame (
Training = c("Strength", "Stamina", "Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30, 45)
)
Data_Frame
Access Items
Example
Data_Frame <- data.frame (
Training = c("Strength", "Stamina", "Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30, 45)
)
Data_Frame[1]
Data_Frame[["Training"]]
Data_Frame$Training
Add Rows
Example
Data_Frame <- data.frame (
Training = c("Strength", "Stamina", "Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30, 45)
)
Example
Data_Frame <- data.frame (
Training = c("Strength", "Stamina", "Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30, 45)
)
# Add a new column
New_col_DF <- cbind(Data_Frame, Steps = c(1000, 6000, 2000))
# Print the new column
New_col_DF
Remove Rows and Columns
Example
Data_Frame <- data.frame (
Training = c("Strength", "Stamina", "Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30, 45)
)
Use the dim() function to find the amount of rows and columns in a Data
Frame:
Example
Data_Frame <- data.frame (
Training = c("Strength", "Stamina", "Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30, 45)
)
dim(Data_Frame)
You can also use the ncol() function to find the number of columns
and nrow() to find the number of rows:
Example
Data_Frame <- data.frame (
Training = c("Strength", "Stamina", "Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30, 45)
)
ncol(Data_Frame)
nrow(Data_Frame)
Data Frame Length
Use the length() function to find the number of columns in a Data Frame
Example
Data_Frame <- data.frame (
Training = c("Strength", "Stamina", "Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30, 45)
)
length(Data_Frame)
Combining Data Frames
Example
Data_Frame1 <- data.frame (
Training = c("Strength", "Stamina", "Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30, 45)
)
And use the cbind() function to combine two or more data frames in R
horizontally:
Example
Data_Frame3 <- data.frame (
Training = c("Strength", "Stamina", "Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30, 45)
)