Ids Unit 3 Final
Ids Unit 3 Final
3.Factors
4.Data frames
5.Lists
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.
Seq()-function
Colon operator- :
# 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.
v1<- c(4, 5, 6, 7)
# display type of vector
typeof(v1)
typeof(v2)
Output:
[1] "double"
[1] "integer"
2.Character vectors
typeof(v1)
Output:
[1] "character"
3.Logical vectors
Logical vectors in R contain Boolean values such as TRUE, FALSE and NA for
Null values.
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
x <- c(1, 2, 3, 4, 5)
length(x)
length(y)
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.
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:
# Creating a vector
X<- c(2, 7, 9, 7, 8, 2)
X[3] <- 1
X[2] <-9
X[1:5]<- 0
# Modify by specifying
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
# Creating a 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
# Creation of Vector
A<- sort(X)
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.
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
"Volleyball", "kabaddi",
"Baseball", "Cricket")
print(sports.players)
Output:
Bridge Polo Basketball Volleyball kabaddi Baseball
Cricket
2 4 5 6 7 9
11
# R program # To access multiple elements
print(V)
print(V[c(5,7)])
Output:
[1] 1 5 9 13 17 21 25 29 33 37
[1] 17 25
Complex Vector
print(v1)
print(typeof(v1))
Output:
[1] 1+2i 0+3i 4-5i -12+6i
[1] "complex"
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)
# Addition
# Subtraction
# Multiplication
# Division
You can concatenate vectors using the c() function to create a new vector.
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
# Subsetting vector
cat("First 5 values of vector: ", x[1:5], "\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.
# No of rows
nrow = 3,
# No of columns
ncol = 3,
# Naming rows
rownames(A) = c("a", "b", "c")
# Naming columns
colnames(A) = c("c", "d", "e")
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("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] ...
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...
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]
Example
thismatrix <-
matrix(c("apple", "banana", "cherry", "orange","grape", "pineapple", "pea
r", "melon", "fig"), nrow = 3, ncol = 3)
Example
thismatrix <-
matrix(c("apple", "banana", "cherry", "orange","grape", "pineapple", "pea
r", "melon", "fig"), nrow = 3, ncol = 3)
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:
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
Try it Yourself »
Example Explained
In the example above we create an array with the values 1 to 24.
Example
thisarray <- c(1:24)
multiarray <- array(thisarray, dim = c(4, 3, 2))
multiarray[2, 3, 2]
Try it Yourself »
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
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"))
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
print(x)
gender <-factor(x)
print(gender)
# Creating a factor with levels defined by programmer
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[3]
Output
[1] male
Levels: female male
Output
[1] male female
Levels: female male
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[2]<-"female"
gender
Output
[1] female female male female
Levels: female male
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
# converting to factor
print(size_factor)
print(ordered.size)
Output:
[1] small large large small medium large medium medium
Levels: large medium small
Example
Change the value of the third item:
music_genre <-
factor(c("Jazz", "Rock", "Classic", "Classic", "Pop", "Jazz", "Rock", "Ja
zz"))
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]
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]
Result:
[1] Opera
Levels: Classic Jazz Pop Rock Opera
Data Frames
Data Frames are data displayed in a format as a table.
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
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 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)
)
# R program to create
print (df)
# Creating a Subset
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.
# R program to create
print (df)
# Creating a Subset
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.
R
# R program to expand
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
# subjects columns
print(data)
Output:
rollno subjects
1 1 java
2 5 python
3 4 php
4 2 sql
5 3 c
LISTS in R
Creating a 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
empId = c(1, 2, 3, 4)
numberOfEmp = 4
print(empList)
Output
[[1]]
[1] 1 2 3 4
[[2]]
[1] "Debi" "Sandeep" "Subham" "Shiba"
[[3]]
[1] 4
print(my_named_list)
Output
$name
[1] "Sudheer"
$age
[1] 25
$city
[1] "Delhi"
Accessing R List Components
# R program to access
# components of a list
empId = c(1, 2, 3, 4)
numberOfEmp = 4
print(empList)
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"
# R program to access
# components of a list
empId = c(1, 2, 3, 4)
numberOfEmp = 4
print(empList)
# Accessing a top level components by indices
print(empList[[2]])
print(empList[[2]][2])
print(empList[[1]][4])
Output
$ID
[1] 1 2 3 4
$Names
[1] "Debi" "Sandeep" "Subham" "Shiba"
$`Total Staff`
[1] 4
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
empId = c(1, 2, 3, 4)
numberOfEmp = 4
print(empList)
empList$`Total Staff` = 5
empList[[1]][5] = 5
empList[[2]][5] = "Kamala"
print(empList)
Output
Before modifying the list
$ID
[1] 1 2 3 4
$Names
[1] "Debi" "Sandeep" "Subham" "Shiba"
$`Total Staff`
[1] 4
$Names
[1] "Debi" "Sandeep" "Subham" ...
Example
Check if "apple" is present in the list:
Merging list
We can merge the R list by placing all the lists into a single list.
R
print(new_list)
Output:
[[1]]
[1] 1
[[2]]
[1] 2
[[3]]
[1] 3
[[4]]
[1] "Sun"
[[5]]
[1] "Mon"
[[6]]
[1] "Tue"
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.
print(lst)
# Convert the lists to vectors.
print(vec)