0% found this document useful (0 votes)
4 views5 pages

Data Structures in R Programming

The document provides an overview of various data structures in R, including vectors, matrices, arrays, lists, factors, and data frames, along with examples of how to create and manipulate them. It emphasizes the importance of these structures for data analysis and highlights R's capabilities in handling statistical operations. Additionally, it offers resources for further learning and mastering R programming.

Uploaded by

sabihansari907
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Data Structures in R Programming

The document provides an overview of various data structures in R, including vectors, matrices, arrays, lists, factors, and data frames, along with examples of how to create and manipulate them. It emphasizes the importance of these structures for data analysis and highlights R's capabilities in handling statistical operations. Additionally, it offers resources for further learning and mastering R programming.

Uploaded by

sabihansari907
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

R Programming

Data Structures

1. Vectors in R

Vectors are the simplest form of data


structure in R. They are a collection of
elements of the same type, such as
numeric, character, or logical.

R is designed to work well with vectors


and is specially designed to work with
what are called vectorized operations.
This means you can apply a function to a vector without needing to loop
through its elements explicitly. For example, adding two vectors together
adds corresponding elements in a faster and more concise way than doing so
element-wise through loops.

Here we create three vectors for numerical, character, and logical types
using the function c().
Code:
numeric_vector = c(10, 20, 30)character_vector =
c("apple", "banana", "cherry")logical_vector = c(TRUE,
FALSE, TRUE)

print(numeric_vector) print(character_vector)
print(logical_vector)

You can access the elements of the vector using square brackets.
Code:

# Access the first

elementprint(numeric_vector[1])

# Access multiple elements

print(character_vector[c(1, 3)])
You can also perform mathematical and logical operations with vectors.
# Adding a scalar value to the vector
print(numeric_vector + 2)
# Multiplying elements by a scalar
print(numeric_vector * 10)
# Perform logical operations - Check which elements are greater than 15
print(numeric_vector > 15)
R Programming

Other important operations include summing, finding the mean, and finding
the minimum and maximum values.
# Summation
print(sum(numeric_vector))
# Mean
print(mean(numeric_vector))
# Max and min
print(max(numeric_vector))
print(min(numeric_vector))

Matrices in R
Matrices are two-dimensional arrays that store data of a single type. They
are particularly useful for mathematical computations. In R, you can create a
matrix using the matrix() function.

my_matrix <- matrix(1:9, nrow=3, ncol=3)print(my_matrix)

You can access the elements, rows, columns, or subsets of the matrix using
indices.

# Access the element in the first row and second column


print(my_matrix[1,2])
# Access the second row
print(my_matrix[2,])
# Access the third column
print(my_matrix[,3])
You can also perform mathematical operations on matrices.
another_matrix <- matrix(9:1, nrow=3, ncol=3)
# Element-wise addition
print(my_matrix + another_matrix)
# Element-wise subtraction
print(my_matrix - another_matrix)
# Element-wise multiplication
print(my_matrix * another_matrix)
# Element-wise division
print(my_matrix / another_matrix)
R Programming

Arrays in R
Arrays are an extension to matrices, and can have more than two
dimensions, providing a way to store multidimensional data efficiently. You
can create an array in R using the array()function.

The dim = c(2, 2, 2) argument sets the dimensions of the array. In this case,
the dimensions indicate that the array should have three dimensions (a 3D
array), structured as 2 rows, 2 columns, and 2 layers (or depth). The output
of the code above is shown below. You can explore more on arrays in
our Arrays in R tutorial.
Code: my_array = array(1:8, dim = c(2, 2, 2))
print(my_array)

Lists in R
Lists are versatile data structures in R that can hold a mix of objects of
different types and sizes. You can create a list using the list() function.
Code: my_list <- list(name="DataCamp", year=2024, scores=c(80, 90, 85),
active=TRUE)
print(my_list)

Once you’ve created the list, you can access its elements
using either indices or the name.
print(my_list[[3]])
print(my_list$name)
print(my_list$scores)

Factors in R
Unlike vectors, matrices, or lists, factors do not define a structure of data
storage but rather describe how data should be treated within these
structures. So, factors can be thought of as a data type akin to integer types
or character types.
I included factors in the list because they are crucial for handling categorical
data within R’s ecosystem, especially in scenarios involving statistical
techniques where the distinction between categorical and continuous
variables is significant. This is different than Python which requires
categorical features to be converted to numerical ones through dummy or
one hot encoding.
You can create factors using the factor() function as shown below:
Code: gender <- factor(c("male", "female", "female", "male"))
print(gender)
Factors are also stored as levels and can be ordered or unordered.
R Programming

Code: levels(gender) <- c("Female", "Male")


print(gender)

If you want to look at the distribution of the factor variable, you can use
the summary() function. The output shows that there are two entries each
for Female and Male, respectively.
summary(gender)

Data frames in R
Data frames are the most popular and widely used data structure in R. They
are especially convenient because they can contain different types of data
across different columns. You can consider them to be similar to tables in a
database or CSV files as they store and manage data in a two-dimensional,
square or rectangular format.
You can create a data frame with the [Link]() command.
Code: data_frame <- [Link]( Names = c("Kiran", "Ajey", "Carol"), Age =
c(25, 30, 35), Gender = c("Female", "Male", "Female"))
# Printing the data frame
print(data_frame)

You can access the contents of the data frame in several ways, either by
columns or by rows.
# Prints all names - use the $ sign
print(data_frame$Names)
# Access the first row
print(data_frame[1, ])
# Access the second column
print(data_frame[, 2])

Data frames are super useful in data wrangling and analysis because they
support a range of operations like subsetting and sorting, and they provide
an easy way to print descriptive statistics.
Subsetting a data frame in R
subset_female <- data_frame[data_frame$Gender == 'Female', ]
print(subset_female)
Sorting a data frame in R
# Sorting
data_frame <- data_frame[order(data_frame$Age), ]
print(data_frame)
Summarizing a data frame in R
# Statistical summary
summary(data_frame)
R Programming

You can see from the above outputs how easy it is to perform data frame
operations in R. As you delve deeper into data analysis, you'll find that data
frames are incredibly versatile and powerful for data manipulation, analysis,
and visualization. If you are interested in exploring more functionalities,
check out our Data Frames in R tutorial.

Continuing with R

This tutorial has provided you with insights into the various data structures in
R and their application in real-world data analysis situations. Mastering these
structures will improve your analytical skills, enabling you to effectively
manage and analyze data.

As you continue your journey, you will discover that R is a unique


programming language renowned for its robust statistical capabilities and
extensive libraries. It is a worthwhile investment for anyone curious about
the world of data analysis.

For more R tutorials, please check out the following links:

 Getting started in R

 Introduction to R

 Working with Dataframes

If you are interested in learning more, you can read through our R
Programming Interview Questions & Answers, which is a great resource
if you are preparing for an interview or just learning. The article offers a wide
range of questions and answers that will help you identify any gaps in your
knowledge as you continue your journey.

You might also like