Vectors
The most common data structure in R. A vector consists of a series of data elements(ordered) of the same data type. It is a one-D data structure. The data elements in the vector are known as components.
The class() method is used to determine the class of a vector. The typeof() functions are used to determine the data type of a vector.
Example
# R application for displaying Vector
# Vectors(series of same data type)
A = c(“CodingNinjas”)
# Printing all the elements in the console
print(A)
Output:
[1] “CodingNinjas”
Accessing Vector Elements
You can access vector data structures in R programming by the following methods.
Method 1: Using the Index.
The respective indexes of a vector's elements can be used to access each one. To indicate the indexes of the elements to be accessed, use [] brackets.
For example:
x <- c("Java","C","C++","R","Python")
y <- x[c(3,2,1)]
print(y)
Output:
[1] "C++" "C" "Java"
Method 2: Using logical or negative Indexing.
Accessing a vector's elements can also be done via logical indexing, negative indexing, and 0/1.
For example:
x < -c("Java", "C", "C++", "R", "Python")
y < -x[c(TRUE, FALSE, TRUE, FALSE, TRUE)]
z < -x[c(-2, -4)]
w < -x[c(1, 0, 0, 0, 0)]
print(y)
print(z)
print(w)
Output:
[1] "Java" "C++" "Python"
[1] "Java" "C++" "Python"
[1] "Java"
Lists
The data structure stores elements of various types, such as numbers, strings, vectors, or other lists. It also consists of an ordered collection of elements.
Example
#Example of List of characters/strings
CN_Courses_list = list("DSA", "CP", "Java")
# Printing the list
CN_Courses_list
Output:
[[1]]
[1] "DSA"
[[2]]
[1] "CP"
[[3]]
[1] "Java"
Accessing List Elements
We can access the list data structures in R Programming using the respective indices.
For Example:
list2 < -list(matrix(c(3, 8, 5, 1, -2, 6), nrow = 2), c("R", "Ruby", "Java"), list(3, 4, 5))
print(list2[1])
print(list2[2])
print(list2[3])
Output:
[[1]]
[,1] [,2] [,3]
[1,] 3 5 -2
[2,] 8 1 6
[[1]]
[1] "R" "Ruby" "Java"
[[1]]
[[1]][[1]]
[1] 3
[[1]][[2]]
[1] 4
[[1]][[3]]
[1] 5
Matrices
The matrix data structures in R programming are two-D. A matrix is also a group of numbers arranged in a fixed number of rows and columns. Vectors of the same length are used together to form matrices. A matrix's elements must all be the same type (numbers, strings, characters).In R, we can create a memory copy of the matrix using a matrix() function.
Example
# Creating a matrix
CN_matrix = matrix(c(1,3,5,2,4,6), nrow = 3, ncol = 2)
# Printing the matrix
CN_matrix
Output:
[,1] [,2]
[1,] 1 2
[2,] 3 4
[3,] 5 6
Accessing Matrix Element
We can access the element of matrix data structures in R Programming using the respective row and column indices.
For example:
rows = c("row1", "row2", "row3")
cols = c("col1", "col2", "col3")
M < -matrix(c(1: 9), nrow = 3, byrow = TRUE, dimnames = list(rows, cols))
print("Given Matrix")
print(M)
print("Accessing Elements")
print(M[2, 1])
print(M[3, 3])
Output:
[1] "Given Matrix"
col1 col2 col3
row1 1 2 3
row2 4 5 6
row3 7 8 9
[1] "Accessing Elements"
[1] 4
[1] 9
Dataframes
Unlike a matrix, a data frame allows various columns to include various data types (numeric, character, logical, etc.). It combines aspects of rectangular lists and matrices.
The Dataframe has the following features:
- A data frame shouldn't have any empty column names.
- A data frame should have distinct row names.
- A data frame may contain numeric, factor, or character types of data.
- The number of data points in each column should be the same.
Example
# Creating a matrix
Coding Ninjas Studio_library <- data.frame (
Technical_Articles = c("Python", "Java", "C++"),
Sections = c(18, 20, 25),
Ratings = c(5, 5, 5)
)
# Printing the data frame
Coding Ninjas Studio_library
Output:
Technical_Articles Sections Ratings
1 Python 18 5
2 Java 20 5
3 C++ 25 5
Factors
Factors are used as data structures in R programming for storing categorical data. Factors are used to classify the data and store it as levels. You can store both numeric values and strings. They are helpful in statistical modelling and data analysis.
Example
# Creating factor using factor()
print("Example of courses using factor")
courses = factor(c("R", "Java", "Python", "C", "Java", "R", "Python"))
print(courses)
Output:
[1] "Example of courses using factor"
[1] R Java Python C Java R Python
Levels: C Java Python R
Frequently Asked Questions
What are the 4 data structures?
The four commonly used data structures are arrays, linked lists, stacks, and queues. These structures organize and store data in different ways, each suited for specific operations and applications in computer programming.
What are the 4 data types in R?
In R programming, the four fundamental data types are numeric, character, logical, and factor. Numeric data types represent numerical values, character data types store text strings, logical data types represent boolean values (TRUE or FALSE), and factor data types are used for categorical variables with predefined levels.
How to sort data in R?
You can sort your data using the R function in ascending or descending order. Using the hsb2 data frame, you can sort the data. The variable you sort by may be a factor, string, or numeric variable.
Why is R so popular?
In the field of Data Science, R is the most widely used language. It is often used to analyse both structured and unstructured data. R is now the preferred language for doing statistical procedures in Data Science. Making a distinction from other Data Science languages, R supports a number of features.
Conclusion
In this blog, you have learned about the data structures in R programming. In detail, we covered various types of data structures in R programming, such as vectors, lists, matrices, data frames, and factors. We also discussed how to access the elements of various data structures in R programming.
We hope this blog has helped you. We recommend you visit our articles on different topics of Data Science, such as
If you liked our article, do upvote our article and help other ninjas grow. You can refer to our Guided Path to upskill yourself in Data Structures and Algorithms, Competitive Programming, System Design, and many more!
Happy Reading!!