0% found this document useful (0 votes)
27 views42 pages

Ids Unit 3 Final

Uploaded by

pnithishreddy14
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)
27 views42 pages

Ids Unit 3 Final

Uploaded by

pnithishreddy14
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
You are on page 1/ 42

INTRODUCTION TO DATA SCIENCE(IDS)

Prepared by N.Pandu Ranga Reddy


UNIT-3
Data structures in R-Program
1.Vectors

2.Matrices and Arrays

3.Factors

4.Data frames

5.Lists

in R program comment line starts with #

e.g: # R program to create Vectors---comment line

Vectors
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.

# R program to create Vectors---comment line

# we can use the c function

# to combine the values as a vector.

# By default the type will be double

X<- c(61, 4, 21, 67, 89, 2)

cat('using c function', X, '\n')

Seq()-function

# seq() function for creating


# a sequence of continuous values.

# length.out defines the length of vector.

Y<- seq(1, 10, length.out = 5)

cat('using seq() function', Y, '\n')

Colon operator- :

# use':' to create a vector

# of continuous values.

Z<- 2:7

cat('using colon', Z)

Output:
using c function 61 4 21 67 89 2
using seq() function 1 3.25 5.5 7.75 10
using colon 2 3 4 5 6 7
Types of R vectors
Vectors are of different types which are used in R. Following are some of the types
of vectors:
1.Numeric vectors
Numeric vectors are those which contain numeric values such as integer, float,
etc.

# 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"

2.Character vectors

Character vectors in R contain alphanumeric values and special characters.


# 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"
3.Logical vectors
Logical vectors in R contain Boolean values such as TRUE, FALSE and NA for
Null values.

# 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)

Output:
> length(x)
[1] 5

> length(y)
[1] 3

> length(z)
[1] 4
Accessing R vector elements
Accessing elements in a vector is the process of performing operation on an
individual element of a vector. There are many ways through which we can access
the elements of the vector. The most common is using the ‘[]’, symbol.
Note: Vectors in R are 1 based indexing unlike the normal C, python, etc format.

# R program to access elements of a Vector

# accessing elements with an index number.

X<- c(2, 5, 18, 1, 12)


cat('Using Subscript operator', X[2], '\n')

# by passing a range of values

# inside the vector index.

Y<- c(4, 8, 2, 1, 17)

cat('Using combine() function', Y[c(4, 1)], '\n')

Output:
Using Subscript operator 5
Using combine() function 1 4
Modifying a R vector
Modification of a Vector is the process of applying some operation on an individual
element of a vector to change its value in the vector. There are different ways
through which we can modify a vector:

# R program to modify elements of a Vector

# Creating a vector

X<- c(2, 7, 9, 7, 8, 2)

# modify a specific element

X[3] <- 1

X[2] <-9

cat('subscript operator', X, '\n')


# Modify using different logics.

X[1:5]<- 0

cat('Logical indexing', X, '\n')

# Modify by specifying

# the position or elements.

X<- X[c(3, 2, 1)]

cat('combine() function', X)

Output:
subscript operator 2 9 1 7 8 2
Logical indexing 0 0 0 0 0 2
combine() function 0 0 0
Deleting a R vector
Deletion of a Vector is the process of deleting all of the elements of the vector. This
can be done by assigning it to a NULL value.
 R

# R program to delete a Vector

# Creating a Vector

M<- c(8, 10, 2, 5)

# set NULL to the vector

M<- NULL

cat('Output vector', M)

Output:
Output vector NULL
Sorting elements of a R Vector
sort() function is used with the help of which we can sort the values in ascending
or descending order.
 R

# R program to sort elements of a Vector

# Creation of Vector

X<- c(8, 2, 7, 1, 11, 2)

# Sort in ascending order

A<- sort(X)

cat('ascending order', A, '\n')

# sort in descending order

# by setting decreasing as TRUE

B<- sort(X, decreasing = TRUE)

cat('descending order', B)

Output:
ascending order 1 2 2 7 8 11
descending order 11 8 7 2 2 1

Vectors are one of the most basic data structure in R. They contain data of same
type.

 In R, array is a vector of one or more dimensions and every single object


created is stored in the form of a vector. The members of a vector are termed as
components

Assigning Named Vectors in R


It’s also possible to create named vectors in R such that every value has a name
assigned with it. R provides the names() function in order to create named vectors.

Example:

Suppose one wants to create a named vector with the number of players in each
sport. To do so, first, he will create a numeric vector containing the number of
players. Now, he can use the names() function to assign the name of the sports to
the number of players.

# R program to illustrate

# Assigning named vectors

# Creating a numeric vector

# with the number of players

sports.players = c(2, 4, 5, 6, 7, 9, 11)

# Assigning sports name to the numeric vector

names(sports.players) = c("Bridge", "Polo", "Basketball",

"Volleyball", "kabaddi",

"Baseball", "Cricket")

# Displaying the named vector

print(sports.players)

Output:
Bridge Polo Basketball Volleyball kabaddi Baseball
Cricket

2 4 5 6 7 9
11
# R program # To access multiple elements

# Creating a vector by seq() function

V = seq(1, 40, by= 4)

# Printing the vector

print(V)

# Printing the fifth and seventh element of the vector

print(V[c(5,7)])

Output:
[1] 1 5 9 13 17 21 25 29 33 37
[1] 17 25

Complex Vector

The complex data type is to store numbers with an imaginary component.


Examples of complex values are 1+2i, 3i, 4-5i, -12+6i, etc.
Example:
 R

# R program to create complex Vectors

# create complex vector

v1 <- c(1+2i, 3i, 4-5i, -12+6i)


# print vector

print(v1)

# display type of vector

print(typeof(v1))

Output:
[1] 1+2i 0+3i 4-5i -12+6i
[1] "complex"

Vectors contain a sequence of homogeneous types of data. There are


mainly two types of vectors in R. The complete classification vectors in R
are given below.
1. Atomic Vector
 Integer
 Double
 Logical
 Character
 Complex
 Raw
2. Recursive Vector
 List
The main difference between atomic vectors and recursive vector(list) is
that atomic vectors are homogeneous, whereas the recursive
vector(list) can be heterogeneous. Vectors have three common
properties:
1. Type, typeof(), what it is.
2. Length, length(), how many elements it contains.
Attributes, attributes(), additional arbitrary metadata

Vector Arithmetic
Arithmetic Operations:
You can perform basic arithmetic operations like addition, subtraction, multiplication, and division
on vectors. The operations are carried out element by element.
vector1 <- c(1, 2, 3, 4, 5)

vector2 <- c(5, 4, 3, 2, 1)

# Addition

result_addition <- vector1 + vector2 # Element-wise addition

# Subtraction

result_subtraction <- vector1 – vector2 # Element-wise subtraction

# Multiplication

result_multiplication <- vector1 * vector2 # Element-wise multiplication

# Division

result_division <- vector1 / vector2 # Element-wise division

Vector Concatenation or combining vectors

You can concatenate vectors using the c() function to create a new vector.

vector1 <- c(1, 2, 3)

vector2 <- c(4, 5, 6)

concatenated_vector <- c(vector1, vector2) # Concatenates the two vectors

Vector sub setting,


subsetting allows the user to access elements from an object. It takes out a portion
from the object based on the condition provided

R – subsetting
Subsetting in R Using [ ] Operator

Using the ‘[ ]’ operator, elements of vectors and observations from data frames can be
accessed. To neglect some indexes, ‘-‘ is used to access all other indexes of vector or
data frame.
Example 1:
In this example, let us create a vector and perform subsetting using the [ ] operator.

 R
# Create vector

x <- 1:15

# Print vector

cat("Original vector: ", x, "\n")

# Subsetting vector
cat("First 5 values of vector: ", x[1:5], "\n")

cat("Without values present at index 1, 2 and 3: ",

x[-c(1, 2, 3)], "\n")

Output:
Original vector: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
First 5 values of vector: 1 2 3 4 5
Without values present at index 1, 2 and 3: 4 5 6 7 8 9 10 11
12 13 14 15

MATRICES

Matrices
A matrix is a two dimensional data set with columns and rows.

A column is a vertical representation of data, while a row is a horizontal representation


of data.

A matrix can be created with the matrix() function. Specify


the nrow and ncol parameters to get the amount of rows and columns:
# R program to create a matrix

A = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), byrow = TRUE)

# No of rows
nrow = 3,

# No of columns
ncol = 3,

# By default matrices are in column-wise order


# So this parameter decides how to arrange the matrix

# Naming rows
rownames(A) = c("a", "b", "c")

# Naming columns
colnames(A) = c("c", "d", "e")

cat("The 3x3 matrix:\n")


print(A)

Output
The 3x3 matrix:
c d e
a 1 2 3
b 4 5 6
c 7 8 9
.
Matrix Metrics

Matrix metrics tell you about the Matrix you created. You might want to know the
number of rows, number of columns, dimensions of a Matrix.
Below Example will help you in answering following questions:
 How can you know the dimension of the matrix?
 How can you know how many rows are there in the matrix?
 How many columns are in the matrix?
 How many elements are there in the matrix?

Example:
R
# R program to illustrate
# matrix metrics
# Create a 3x3 matrix
A = matrix(
c(1, 2, 3, 4, 5, 6, 7, 8, 9),
nrow = 3,
ncol = 3,
byrow = TRUE
)
cat("The 3x3 matrix:\n")
print(A)

cat("Dimension of the matrix:\n")


print(dim(A))

cat("Number of rows:\n")
print(nrow(A))

cat("Number of columns:\n")
print(ncol(A))

cat("Number of elements:\n")
print(length(A))
# OR
print(prod(dim(A)))

Output
The 3x3 matrix:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
Dimension of the matrix:
[1] 3 3
Number of rows:
[1] 3
Number of columns:
[1] 3
Number of elements:
[1] ...

Accessing Submatrices in R:(or) Matrix Sub setting


We can access the submatrix in a matrix using the colon(:) operator.
R
# R program to illustrate
# access submatrices in a matrix

# Create a 3x3 matrix


A = matrix(
c(1, 2, 3, 4, 5, 6, 7, 8, 9),
nrow = 3,
ncol = 3,
byrow = TRUE
)
cat("The 3x3 matrix:\n")
print(A)

cat("Accessing the first three rows and the first two columns\n")
print(A[1:3, 1:2])

Output
The 3x3 matrix:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
Accessing the first three rows and the first two columns
[,1] [,2]
[1,] 1 2
[2,] 4 5
[3...

You can also create a matrix with strings:

Example
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2,
ncol = 2)

print(thismatrix)
Access Matrix Items
You can access the items by using [ ] brackets. The first number "1" in the bracket
specifies the row-position, while the second number "2" specifies the column-position:

Example
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2,
ncol = 2)

thismatrix[1, 2]

Add Rows and Columns


Use the cbind() function to add additional columns in a Matrix:

Example
thismatrix <-
matrix(c("apple", "banana", "cherry", "orange","grape", "pineapple", "pea
r", "melon", "fig"), nrow = 3, ncol = 3)

newmatrix <- cbind(thismatrix, c("strawberry", "blueberry", "raspberry"))

# Print the new matrix


newmatrix
Try it Yourself »
Note: The cells in the new column must be of the same length as the existing matrix.

Use the rbind() function to add additional rows in a Matrix:

Example
thismatrix <-
matrix(c("apple", "banana", "cherry", "orange","grape", "pineapple", "pea
r", "melon", "fig"), nrow = 3, ncol = 3)

newmatrix <- rbind(thismatrix, c("strawberry", "blueberry", "raspberry"))

# Print the new matrix


newmatrix
Try it Yourself »
Note: The cells in the new row must be of the same length as the existing matrix.

Try it Yourself »
Check if an Item Exists
To find out if a specified item is present in a matrix, use the %in% operator:

Example
Check if "apple" is present in the matrix:

thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2,


ncol = 2)

"apple" %in% thismatrix

Arrays
Compared to matrices, arrays can have more than two dimensions.

We can use the array() function to create an array, and the dim parameter to specify
the dimensions:

Example
# An array with one dimension with values ranging from 1 to 24
thisarray <- c(1:24)
thisarray

# An array with more than one dimension


multiarray <- array(thisarray, dim = c(4, 3, 2))
multiarray

Try it Yourself »

Example Explained
In the example above we create an array with the values 1 to 24.

How does dim=c(4,3,2) work?


The first and second number in the bracket specifies the amount of rows and columns.
The last number in the bracket specifies how many dimensions we want.

Note: Arrays can only have one data type.


Access Array Items
You can access the array elements by referring to the index position. You can use
the [] brackets to access the desired elements from an array:

Example
thisarray <- c(1:24)
multiarray <- array(thisarray, dim = c(4, 3, 2))

multiarray[2, 3, 2]

Try it Yourself »

The syntax is as follow: array[row position, column position, matrix level]

You can also access the whole row or column from a matrix in an array, by using
the c() function:

Example
thisarray <- c(1:24)

# Access all the items from the first row from matrix one
multiarray <- array(thisarray, dim = c(4, 3, 2))
multiarray[c(1),,1]

# Access all the items from the first column from matrix one
multiarray <- array(thisarray, dim = c(4, 3, 2))
multiarray[,c(1),1]

Try it Yourself »

A comma (,) before c() means that we want to access the column.

A comma (,) after c() means that we want to access the row.

R Factors
Factors in R Programming Language are data structures that are
implemented to categorize the data or represent categorical data and
store it on multiple levels.
Factors are used to categorize data. Examples of factors are:

 Demography: Male/Female
 Music: Rock, Pop, Classic, Jazz
 Training: Strength, Stamina

The command used to create or modify a factor in R language is – factor() with a


vector as input.

The two steps to creating an R factor :

 Creating a vector
 Converting the vector created into a factor using function factor()

 # Create a factor
music_genre <-
factor(c("Jazz", "Rock", "Classic", "Classic", "Pop", "Jazz", "Rock"
, "Jazz"))

# Print the factor


music_genre

 Result:
 [1] Jazz Rock Classic Classic Pop Jazz Rock Jazz
 Levels: Classic Jazz Pop Rock


Examples: Let us create a factor gender with levels female, male and
transgender.

# Creating a vector

x <-c("female", "male", "male", "female")

print(x)

# Converting the vector x into a factor


# named gender

gender <-factor(x)

print(gender)

Levels can also be predefined by the programmer.


# Creating a factor with levels defined by programmer

gender <- factor(c("female", "male", "male", "female"),

levels = c("female", "transgender", "male"));

gender

Output
[1] female male male female
Levels: female transgender male

Further one can check the levels of a factor by using function levels().
Accessing elements of a Factor in R
Like we access elements of a vector, the same way we access the elements of a
factor. If gender is a factor then gender[i] would mean accessing an ith element in
the factor.
Example
 R

gender <- factor(c("female", "male", "male", "female"));

gender[3]

Output
[1] male
Levels: female male

More than one element can be accessed at a time

gender <- factor(c("female", "male", "male", "female"));


gender[c(2, 4)]

Output
[1] male female
Levels: female male

Subtract one element at a time.


Example

gender <- factor(c("female", "male", "male", "female" ));

gender[-3]

Output
[1] female male female
Levels: female male

Modification of a Factor in R
After a factor is formed, its components can be modified but the new values which
need to be assigned must be at the predefined level.
Example
 R

gender <- factor(c("female", "male", "male", "female" ));

gender[2]<-"female"

gender

Output
[1] female female male female
Levels: female male

Ordering Factor Levels


Ordered factors levels are an extension of factors.
It arranges the levels in increasing order.

We use two functions: factor() and argument ordered().

Syntax: factor(data, levels =c(“”), ordered =TRUE)

Parameter:
 data: input vector with explicitly defined values.
 levels(): Mention the list of levels in c function.
 ordered: It is set true for enabling ordering.

Example:
 R

# creating size vector

size = c("small", "large", "large", "small",

"medium", "large", "medium", "medium")

# converting to factor

size_factor <- factor(size)

print(size_factor)

# ordering the levels

ordered.size <- factor(size, levels = c(


"small", "medium", "large"), ordered = TRUE)

print(ordered.size)

Output:
[1] small large large small medium large medium medium
Levels: large medium small

Change Item Value


To change the value of a specific item, refer to the index number:

Example
Change the value of the third item:

music_genre <-
factor(c("Jazz", "Rock", "Classic", "Classic", "Pop", "Jazz", "Rock", "Ja
zz"))

music_genre[3] <- "Pop"

music_genre[3]

Result:

[1] Pop
Levels: Classic Jazz Pop Rock
Try it Yourself »

Note that you cannot change the value of a specific item if it is not already
specified in the factor. The following example will produce an error:

Example
Trying to change the value of the third item ("Classic") to an item that does not
exist/not predefined ("Opera"):

music_genre <-
factor(c("Jazz", "Rock", "Classic", "Classic", "Pop", "Jazz", "Rock", "Ja
zz"))

music_genre[3] <- "Opera" # not exist in vector

music_genre[3]

Result:

Warning message:
In `[<-.factor`(`*tmp*`, 3, value = "Opera") :
invalid factor level, NA generated
Try it Yourself »

However, if you have already specified it inside the levels argument, it will work:

Example
Change the value of the third item:

music_genre <-
factor(c("Jazz", "Rock", "Classic", "Classic", "Pop", "Jazz", "Rock", "Ja
zz"), levels = c("Classic", "Jazz", "Pop", "Rock", "Opera"))

music_genre[3] <- "Opera"

music_genre[3]

Result:

[1] Opera
Levels: Classic Jazz Pop Rock Opera

Data Frames
Data Frames are data displayed in a format as a table.

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.

Use the data.frame() function to create a data frame:

# 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

Summarize the Data


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

summary(Data_Frame)

Access Items
We can use single brackets [ ], double brackets [[ ]] or $ to access columns 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[1]

Data_Frame[["Training"]]

Data_Frame$Training

Try it Yourself »
Add Rows
Use the rbind() function to add new rows in a Data Frame:

Example
Data_Frame <- data.frame (
Training = c("Strength", "Stamina", "Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30, 45)
)

# Add a new row


New_row_DF <- rbind(Data_Frame, c("Strength", 110, 110))

# Print the new row


New_row_DF

Add Columns
Use the cbind() function to add new 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)
)

# Add a new column


New_col_DF <- cbind(Data_Frame, Steps = c(1000, 6000, 2000))

# Print the new column


New_col_DF

SUBSETTING OF DATA FRAMES


subset() function in R Programming Language is used to create subsets of a
Data frame.
This can also be used to drop columns from a data frame.

Syntax: subset(df, expr)


Parameters:
 df: Data frame used
 expr: Condition for subset

Create Subsets of Data Frames

Here we will make subsets of dataframe using subset() methods in R language.

Example 1: Basic example of R – subset() Function


 R

# R program to create

# subset of a data frame

# Creating a Data Frame

df<-data.frame(row1 = 0:2, row2 = 3:5, row3 = 6:8)

print ("Original Data Frame")

print (df)

# Creating a Subset

df1<-subset(df, select = row2) # row 2 selected only

print("Modified Data Frame")

print(df1)

Output:
[1] "Original Data Frame"
row1 row2 row3
1 0 3 6
2 1 4 7
3 2 5 8
[1] "Modified Data Frame"
row2
1 3
2 4
3 5

Here, in the above code, the original data frame remains intact while another
subset of data frame is created which holds a selected row from the original data
frame.

Example 2: Create Subsets of Data frame in R Language


 R

# R program to create

# subset of a data frame

# Creating a Data Frame

df<-data.frame(row1 = 0:2, row2 = 3:5, row3 = 6:8)

print ("Original Data Frame")

print (df)

# Creating a Subset

df<-subset(df, select = -c(row2, row3))# row 2 & 3 not selected

print("Modified Data Frame")

print(df)

Output:
[1] "Original Data Frame"
row1 row2 row3
1 0 3 6
2 1 4 7
3 2 5 8
[1] "Modified Data Frame"
row1
1 0
2 1
3 2
Here, in the above code, the rows are permanently deleted from the original data
frame.

Expand Data Frame


A data frame in R can be expanded by adding new columns and rows to the
already existing R data frame.

 R

# R program to expand

# the data frame

# creating a data frame

friend.data <- data.frame(friend_id = c(1:5),friend_name = c("Sachin",


"Sourav", "Dravid", "Sehwag", "Dhoni"), stringsAsFactors = FALSE)

# Expanding data frame

friend.data$location <- c("Kolkata", "Delhi", "Bangalore", "Hyderabad",

"Chennai")# location is added as extra item

resultant <- friend.data

# print the modified data frame

print(resultant)

Output:
friend_id friend_name location
1 1 Sachin Kolkata
2 2 Sourav Delhi
3 3 Dravid Bangalore
4 4 Sehwag Hyderabad
5 5 Dhoni Chennai
Methods to sort a dataframe:
1. order() function (increasing and decreasing order)
2. arrange() function from dplyr package
3. setorder() function from data.table package

Method 1: Using order() function


This function is used to sort the dataframe based on the particular column in the
dataframe
Syntax: order(dataframe$column_name,decreasing = TRUE))
where
 dataframe is the input dataframe
 Column name is the column in the dataframe such that dataframe is sorted
based on this column
 Decreasing parameter specifies the type of sorting order
If it is TRUE dataframe is sorted in descending order. Otherwise, in increasing
order
return type: Index positions of the elements

Example 1: R program to create a dataframe with 2 columns and order based on


particular columns in decreasing order. Displayed the Sorted dataframe based on
subjects in decreasing order, displayed the Sorted dataframe based on roll no in
decreasing order
 R

# create dataframe with roll no and

# subjects columns

data = data.frame(rollno = c(1, 5, 4, 2, 3),subjects = c("java", "python",


"php", "sql", "c"))

print(data)

print("sort the data in decreasing order based on subjects ")

print(data[order(data$subjects, decreasing = TRUE), ] )


print("sort the data in decreasing order based on rollno ")

print(data[order(data$rollno, decreasing = TRUE), ] )

Output:
rollno subjects
1 1 java
2 5 python
3 4 php
4 2 sql
5 3 c

[1] "sort the data in decreasing order based on subjects "


rollno subjects
4 2 sql
2 5 python
3 4 php
1 1 java
5 3 c

[1] "sort the data in decreasing order based on rollno "


rollno subjects
2 5 python
3 4 php
5 3 c
4 2 sql
1 1 java

LISTS in R
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)

Output
[[1]]
[1] 1 2 3 4
[[2]]
[1] "Debi" "Sandeep" "Subham" "Shiba"

[[3]]
[1] 4

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)

Output
$name
[1] "Sudheer"

$age
[1] 25

$city
[1] "Delhi"
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)

Output
$ID
[1] 1 2 3 4
$Names
[1] "Debi" "Sandeep" "Subham" "Shiba"

$`Total Staff`
[1] 4
Accessing name components using $ command
[1] "Debi" "Sandeep" "Subham" "Shiba"

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])

Output
$ID
[1] 1 2 3 4

$Names
[1] "Debi" "Sandeep" "Subham" "Shiba"

$`Total Staff`
[1] 4

Accessing name components using indices


[1] "Debi" "Sandeep" "Subham" "Shiba"
Accessing Sandeep from na...
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)

Output
Before modifying the list
$ID
[1] 1 2 3 4

$Names
[1] "Debi" "Sandeep" "Subham" "Shiba"

$`Total Staff`
[1] 4

After modified the list


$ID
[1] 1 2 3 4 5

$Names
[1] "Debi" "Sandeep" "Subham" ...

Check if Item Exists


To find out if a specified item is present in a list, use the %in% operator:

Example
Check if "apple" is present in the list:

thislist <- list("apple", "banana", "cherry")

"apple" %in% thislist

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)

You might also like