0% found this document useful (0 votes)
8 views16 pages

Unit - Iii: R Vectors

The document provides an overview of R programming concepts including vectors, matrices, arrays, factors, and data frames, detailing their creation, manipulation, and arithmetic operations. It explains the differences between functions like paste() and cat(), and introduces object-oriented programming in R through S3, S4, and Reference classes. Additionally, it covers the attributes of factors and the structure of data frames, emphasizing their role in data analysis.

Uploaded by

22bk1a7295
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)
8 views16 pages

Unit - Iii: R Vectors

The document provides an overview of R programming concepts including vectors, matrices, arrays, factors, and data frames, detailing their creation, manipulation, and arithmetic operations. It explains the differences between functions like paste() and cat(), and introduces object-oriented programming in R through S3, S4, and Reference classes. Additionally, it covers the attributes of factors and the structure of data frames, emphasizing their role in data analysis.

Uploaded by

22bk1a7295
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/ 16

UNIT – III

Vectors: Creating and Naming Vectors, Vector Arithmetic, Vector sub setting,
Matrices: Creating and Naming Matrices, Matrix Sub setting, Arrays, Class.
Factors and Data Frames: Introduction to Factors: Factor Levels, summarizing a
Factor, Ordered Factors, Comparing Ordered Factors, Introduction to Data
Frame, sub setting of Data Frames, Extending Data Frames, Sorting Data
Frames. Lists: Introduction, creating a List: Creating a Named List, Accessing
List Elements, Manipulating List Elements, Merging Lists, Converting Lists to
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.

Difference between Paste() and Cat() in R.

paste() – Returns a String


 Creates and returns a string (can be stored in a variable).
 Uses a separator (sep for elements, collapse for vectors).
 Encapsulates output in quotes when printed.
cat() – Prints to Console
 Directly prints output to the console.
 Does NOT return a value (can't be stored in a variable).
 Automatically separates elements with spaces.
 Does NOT enclose output in quotes.

name <- "Alice"


age <- 25
# Using paste()
message <- paste("My name is", name, "and I am", age, "years old.")
print(message)
# Using cat()
cat("My name is", name, "and I am", age, "years old.\n")

Paste output
[1] "My name is Alice and I am 25 years old."

Cat Output
My name is Alice and I am 25 years old.

Type of Vectors
• Numeric vectors
• Characters vectors
• Logical vectors
• Complex Vectors
we use c() function to create a vector. This function returns a one
dimensional array or simply vector.
Example
• vec <- c(10, 20, 30, 40, 50)
• typeof(vec)

• vec <- c(10L, 20L, 30L, 40L, 50L)
• typeof(vec)

• vec <- c(‘geeks’, ‘2’, ‘hello’, 50)
• typeof(vec) #by default numeric values will be converted into char

Creating and naming Vectors


• X=c(61,4,21,67,89,2)
• cat(‘using c function’, X )
• c() is used to combine values into a vector
• cat() prints the value in a single line, separated by spaces
• Seq()= used to generate sequence a number with specific pattern
• Y=seq(1,10, length.out=5)
cat(‘using seq() function’, Y)
Seq()= used to generate sequence a number with specific pattern
• seq(from, to, by, length.out, along.with)
• from – Starting value of the sequence.
• to – Ending value of the sequence.
• by – Step size (increment or decrement).
• length.out – Desired length of the sequence.
• along.with – Generates a sequence the same length as another object.

• seq(1, 10, by = 2)
• Output:- [1] 1 3 5 7 9

• seq(1, 10, length.out = 5)
• Output:- [1] 1.00 3.25 5.50 7.75 10.00

• x <- c(10, 20, 30, 40)
• seq(along.with = x)
• Output:- [1] 1 2 3 4

Length of R vectors

• X= c(1,2,3,4,5)
length(X)
• Output 5
Accessing R Vectors
• X = c(2, 5, 18, 1, 12)
• cat('Using Subscript operator', X[2]) #Output = 5
• Y<- c(4, 8, 2, 1, 17)
• cat('Using combine() function', Y[c(4, 1)]) #Output = 1 4

Modifying R vector
• X= c(2,7,9,7,8,2)
• X[3] = 1
• X[2] = 9
• cat(‘subscription operations’, X) #2,9,1,7,8,2
• X[1:5] = 0
• cat(‘subscription operation’, X) #0,0,0,0,0,2
Vector Arithmetic’s
• The two vectors should be of same length and same type. Or one of the
vectors can be an atomic value of same type.
• If the vectors are not of same length, then Vector Recycling happens
implicitly.
• If two vectors are of unequal length, the shorter one will be
recycled in order to match the longer vector.
• u = c(10, 20, 30)
• v = c(1, 2, 3, 4, 5, 6, 7, 8, 9)
• u+v
• Output 11 22 33 14 25 36 17 28 39

Matrices: creating and Naming matrices


• A matrix is a 2-D data set with columns and rows.
• A matrix can be created with matrix() function.
• matrix(data, nrow, ncol, byrow, dimnames)
• data – values you want to enter
• nrow – no. of rows
• ncol – no. of columns
• byrow – logical clue, if ‘true’ value will be assigned by rows
• dimnames – names of rows and columns
• mat <- matrix(1:9, nrow=3)
• mat
• A<- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol = 2)
• A
• A[1,2]
• Output "cherry"
• [,1] [,2]
• [1,] "apple" "cherry"
• [2,] "banana" "orange"
• A =matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol = 2)
• A[2,]
• A[,2]
• Output
• "banana" "orange“
• "cherry" "orange"

Combine 2 matrices
• Mat1=matrix(c(“A”, “B”, “C”, “G”),nrow=2,ncol=2)
Mat2= matrix(c(“O”, “M", “P”, “W"),nrow=2,ncol=2)
• Matrix_Combined=rbind(Mat1,Mat2)
Matrix_Combined
• Matrix_Combined=cbind(Mat1,Mat2)
Matrix_Combined

Arrays
• In R, Uni-dimensional arrays are called vectors with the length being their
only dimension.
• Two-dimensional arrays are called matrices, consisting of fixed numbers of
rows and columns.
• R Arrays consist of all elements of the same data type.
• Syntax:
• array(data, dim = (nrow, ncol, nmat), dimnames=names)
• arr = array(2:13, dim = c(2, 3, 2))
• print(arr)
• Output
• ,,1
• [,1] [,2] [,3]
• [1,] 2 4 6
• [2,] 3 5 7
• ,,2
• [,1] [,2] [,3]
• [1,] 8 10 12
• [2,] 9 11 13

Class
• A class is like a blueprint for a car. It defines what a car should have (wheels,
engine, color) and what it can do (start, stop, accelerate).
• An object is an actual car made from that blueprint. You can create many
cars (objects) from the same blueprint (class).
• An object is simply a data structure that has some methods and attributes. It
represents the set of properties or methods that are common to all objects of
one type.

• Unlike most other programming languages, R has a three-class system.


These are S3, S4, and Reference Classes.

S3
• S3 is the simplest yet the most popular OOP system and it lacks formal
definition and structure. An object of this type can be created by just
adding an attribute to it.
• # create a list with required components
• movieList <- list(name = "Iron man", leadActor = "Robert Downey
Jr")
• # give a name to your class
• class(movieList) <- "movie"
movieList

S4
Programmers of other languages like C++, Java might find S3 to be very much
different than their normal idea of classes as it lacks the structure that classes
are supposed to provide. S4 is a slight improvement over S3 as its objects have
a proper definition and it gives a proper structure to its objects.

• Code
• library(methods) #it provides the functionality to define work with S4
classes as it more structures
• # definition of S4 class
• setClass("movies", slots=list(name="character", leadActor = "character"))
#used to define new class named movie
• # creating an object using new() by passing class name and slot values
• movieList <- new("movies", name="Iron man", leadActor = "Robert
Downey Jr") #creating object
• movieList #it gives output

Reference class or R5 class


• Reference Class is an improvement over S4 Class. Here the methods
belong to the classes.
• library(methods)
• # setRefClass returns a generator
• movies <- setRefClass("movies", fields = list(name = "character",
• leadActor = "character", rating = "numeric"))

• #now we can use the generator to create objects
• movieList <- movies(name = "Iron Man",
• leadActor = "Robert downey Jr", rating = 7)
• movieList

Difference from S4
• Unlike S4 classes, R5 (Reference) classes use fields instead of slots and
allow modification of object attributes in-place.

Factors
• A factor in R is a data structure used to represent categorical data.
• It is useful for handling variables that take limited, distinct values (like
gender, colors, or status).
• Factors store levels (unique categories) instead of actual character values,
making data analysis more efficient.

Output

Attributes of Factor in R language


• x: It is the vector that needs to be converted into a factor.
• Levels: It is a set of distinct values which are given to the input vector x.
• Labels: It is a character vector corresponding to the number of labels.
• Exclude: This will mention all the values you want to exclude.
• Ordered: This logical attribute decides whether the levels are ordered.
• nmax: It will decide the upper limit for the maximum number of levels

Example

Output

Data Frames
Data frames can also be interpreted as matrices where each column of
a matrix can be of different data types. R DataFrame is made up of three
principal components, the data, rows, and columns.
Example
• friend <- data.frame(
• friend_id = c(1:5),
• friend_name = c("Sachin", "Sourav",
• "Dravid", "Sehwag",
• "Dhoni"),
• stringsAsFactors = FALSE
• )
• # print the data frame
• print(friend)
• Print(str(friend)) #print the data in 1 stream
Print(summary(friend))
output

Extending Data Frames


Here in the data frame we are adding 1 more column to the table.
Output

Accessing the data.frame


Output
Sorting the data frames

Output

You might also like