R Vectors
R Vectors are the same as the arrays in R language which are used to hold multiple data values of the
same type. One major key point is that in R Programming Language the indexing of the vector will
start from ‘1’ and not from ‘0’. We can create numeric vectors and character vectors as well.
Creating a vector
A vector is a basic data structure that represents a one-dimensional array. to create a array we use the
“c” function which the most common method use in R Programming Language.
Types of R vectors
Vectors are of different types which are used in R. Following are some of the types of vectors:
Numeric vectors
Numeric vectors are those which contain numeric values such as integer, float, etc.
R
# R program to create numeric Vectors
# creation of vectors using c() function.
v1<- c(4, 5, 6, 7)
# display type of vector
typeof(v1)
# by using 'L' we can specify that we want integer values.
v2<- c(1L, 4L, 2L, 5L)
# display type of vector
typeof(v2)
Output:
[1] "double"
[1] "integer"
Character vectors
Character vectors in R contain alphanumeric values and special characters.
R
# R program to create Character Vectors
# by default numeric values
# are converted into characters
v1<- c('geeks', '2', 'hello', 57)
# Displaying type of vector
typeof(v1)
Output:
[1] "character"
Logical vectors
Logical vectors in R contain Boolean values such as TRUE, FALSE and NA for Null values.
R
# R program to create Logical Vectors
# Creating logical vector
# using c() function
v1<- c(TRUE, FALSE, TRUE, NA)
# Displaying type of vector
typeof(v1)
Output:
[1] "logical"
Length of R vector
In R, the length of a vector is determined by the number of elements it contains. we can use
the length() function to retrieve the length of a vector.
R
# Create a numeric vector
x <- c(1, 2, 3, 4, 5)
# Find the length of the vector
length(x)
# Create a character vector
y <- c("apple", "banana", "cherry")
# Find the length of the vector
length(y)
# Create a logical vector
z <- c(TRUE, FALSE, TRUE, TRUE)
# Find the length of the vector
length(z)
R – Lists
A list in R programming is a generic object consisting of an ordered collection of objects. Lists
are one-dimensional, heterogeneous data structures.
The list can be a list of vectors, a list of matrices, a list of characters, a list of functions, and so on.
A list is a vector but with heterogeneous data elements. A list in R is created with the use of the list()
function.
R allows accessing elements of an R list with the use of the index value. In R, the indexing of a list
starts with 1 instead of 0.
Creating a List
To create a List in R you need to use the function called “list()“.
In other words, a list is a generic vector containing other objects. To illustrate how a list looks, we
take an example here. We want to build a list of employees with the details. So for this, we want
attributes such as ID, employee name, and the number of employees.
Example:
R
# R program to create a List
# The first attributes is a numeric vector
# containing the employee IDs which is created
# using the command here
empId = c(1, 2, 3, 4)
# The second attribute is the employee name
# which is created using this line of code here
# which is the character vector
empName = c("Debi", "Sandeep", "Subham", "Shiba")
# The third attribute is the number of employees
# which is a single numeric variable.
numberOfEmp = 4
# We can combine all these three different
# data types into a list
# containing the details of employees
# which can be done using a list command
empList = list(empId, empName, numberOfEmp)
print(empList)
Naming List Components
Naming list components make it easier to access them.
Example:
R
# Creating a named list
my_named_list <- list(name = "Sudheer", age = 25, city = "Delhi")
# Printing the named list
print(my_named_list)
Accessing R List Components
We can access components of an R list in two ways.
1. Access components by names:
All the components of a list can be named and we can use those names to access the components of
the R list using the dollar command.
Example:
R
# R program to access
# components of a list
# Creating a list by naming all its components
empId = c(1, 2, 3, 4)
empName = c("Debi", "Sandeep", "Subham", "Shiba")
numberOfEmp = 4
empList = list(
"ID" = empId,
"Names" = empName,
"Total Staff" = numberOfEmp
)
print(empList)
# Accessing components by names
cat("Accessing name components using $ command\n")
print(empList$Names)
2. Access components by indices:
We can also access the components of the R list using indices.
To access the top-level components of a R list we have to use a double slicing operator “[[ ]]” which
is two square brackets and if we want to access the lower or inner-level components of a R list we
have to use another square bracket “[ ]” along with the double slicing operator “[[ ]]“.
Example:
R
# R program to access
# components of a list
# Creating a list by naming all its components
empId = c(1, 2, 3, 4)
empName = c("Debi", "Sandeep", "Subham", "Shiba")
numberOfEmp = 4
empList = list(
"ID" = empId,
"Names" = empName,
"Total Staff" = numberOfEmp
)
print(empList)
# Accessing a top level components by indices
cat("Accessing name components using indices\n")
print(empList[[2]])
# Accessing a inner level components by indices
cat("Accessing Sandeep from name using indices\n")
print(empList[[2]][2])
# Accessing another inner level components by indices
cat("Accessing 4 from ID using indices\n")
print(empList[[1]][4])
Modifying Components of a List
A R list can also be modified by accessing the components and replacing them with the ones which
you want.
Example:
R
# R program to edit
# components of a list
# Creating a list by naming all its components
empId = c(1, 2, 3, 4)
empName = c("Debi", "Sandeep", "Subham", "Shiba")
numberOfEmp = 4
empList = list(
"ID" = empId,
"Names" = empName,
"Total Staff" = numberOfEmp
)
cat("Before modifying the list\n")
print(empList)
# Modifying the top-level component
empList$`Total Staff` = 5
# Modifying inner level component
empList[[1]][5] = 5
empList[[2]][5] = "Kamala"
cat("After modified the list\n")
print(empList)
Concatenation of lists
Two R lists can be concatenated using the concatenation function. So, when we want to concatenate
two lists we have to use the concatenation operator.
Syntax:
list = c(list, list1)
list = the original list
list1 = the new list
Example:
R
# R program to edit
# components of a list
# Creating a list by naming all its components
empId = c(1, 2, 3, 4)
empName = c("Debi", "Sandeep", "Subham", "Shiba")
numberOfEmp = 4
empList = list(
"ID" = empId,
"Names" = empName,
"Total Staff" = numberOfEmp
)
cat("Before concatenation of the new list\n")
print(empList)
# Creating another list
empAge = c(34, 23, 18, 45)
# Concatenation of list using concatenation operator
empList = c(empName, empAge)
cat("After concatenation of the new list\n")
print(empList)
Adding Item to List
To add an item to the end of list, we can use append() function.
R
# creating a list
my_numbers = c(1,5,6,3)
#adding number at the end of list
append(my_numbers, 45)
#printing list
my_numbers
Deleting Components of a List
To delete components of a R list, first of all, we need to access those components and then insert a
negative sign before those components. It indicates that we had to delete that component.
Example:
R
# R program to access
# components of a list
# Creating a list by naming all its components
empId = c(1, 2, 3, 4)
empName = c("Debi", "Sandeep", "Subham", "Shiba")
numberOfEmp = 4
empList = list(
"ID" = empId,
"Names" = empName,
"Total Staff" = numberOfEmp
)
cat("Before deletion the list is\n")
print(empList)
# Deleting a top level components
cat("After Deleting Total staff components\n")
print(empList[-3])
# Deleting a inner level components
cat("After Deleting sandeep from name\n")
print(empList[[2]][-2])
Merging list
We can merge the R list by placing all the lists into a single list.
R
# Create two lists.
lst1 <- list(1,2,3)
lst2 <- list("Sun","Mon","Tue")
# Merge the two lists.
new_list <- c(lst1,lst2)
# Print the merged list.
print(new_list)
Output:
[[1]]
[1] 1
[[2]]
[1] 2
[[3]]
[1] 3
[[4]]
[1] "Sun"
[[5]]
[1] "Mon"
[[6]]
[1] "Tue"
Converting List to Vector
Here we are going to convert the R list to vector, for this we will create a list first and then unlist the
list into the vector.
R
# Create lists.
lst <- list(1:5)
print(lst)
# Convert the lists to vectors.
vec <- unlist(lst)
print(vec)
Output
[[1]]
[1] 1 2 3 4 5
[1] 1 2 3 4 5
R List to matrix
We will create matrices using matrix() function in R programming. Another function that will be used
is unlist() function to convert the lists into a vector.
R
# Defining list
lst1 <- list(list(1, 2, 3),
list(4, 5, 6))
# Print list
cat("The list is:\n")
print(lst1)
cat("Class:", class(lst1), "\n")
# Convert list to matrix
mat <- matrix(unlist(lst1), nrow = 2, byrow = TRUE)
# Print matrix
cat("\nAfter conversion to matrix:\n")
print(mat)
cat("Class:", class(mat), "\n")