DSR Unit V
DSR Unit V
Vectors: Creating vector, Naming vector, Vector arithmetic's, Vector sub setting
Matrix: Creating matrix, Naming matrix, Matrix sub setting, Array, Class
Array: Creating array, Naming array, Accessing Array, Array sub setting,
Data Frame: Introduction, Creating, Sub setting, Sorting, Extending data frame
List: Introduction, Creating list, Naming list, Accessing list, Manipulate list,
Merging list, List into vector
Factors: Introduction, Factor level, ordered factor, Summarizing factor
Stanley College of Engineering and Technology for Women, Dept of IT, DSR Dr.V.Sidda Reddy
2|14
Vectors Naming: R support in-built function names() naming vector element/values names after
creation:
• Syntax: names(Vname) <- c( V1, V2, …, Vn)
names() R in-built function
Vname is a vector name
Example : names(Vname) <- c(“No1”, “No2”, “No3” )
• Example :
V1 <- c(5, 8, 7)
names(V1) <- c(“No1”, “No2”, “No3”, )
R : Vectors Arithmetic's:
R support perform various arithmetic operation (+, -, *, /) on vectors :
Example :
o V1 <- c(5, 7, 8, 0)
o V2 <- c(9, 3, 0, 5)
V1 + V2
V1 - V2
V1 * V2
V1 / V2
Vector: Sub setting /Indexing: R support various method to sub setting given vector
• Index : vector can be sub setting by using index and subset operator [ ]of the vector
V <- c(10, 20, 30, 40, 50)
V1 <- V[1:3] V1 = { 10, 20, 30}
V2 <- V[3:5] V2 = { 30, 40, 50}
• Positive integer : vector can be subset using positive integer to add/select subset
o Syntax: Sname <- Vname[c( I1, I2, I3) ]
Stanley College of Engineering and Technology for Women, Dept of IT, DSR Dr.V.Sidda Reddy
3|14
Stanley College of Engineering and Technology for Women, Dept of IT, DSR Dr.V.Sidda Reddy
4|14
Naming List and List components: R - support naming list (named list) and list components to access
by using list name and individual components(objects) of the list:
Stanley College of Engineering and Technology for Women, Dept of IT, DSR Dr.V.Sidda Reddy
5|14
Accessing: List components/elements : In R the list components can be accessed by two ways :
1. Index: List components can be accessed by index inside [ ]:
• List create components index in sequence[1, 2, …] : First component index 1, the second
item has index 2, and so on …
o Syntax: Lname[Index]
Lname : List name
[Index]: Component Index
o Example: Emp <- list(101, "Vani", "Software", 35000.00)
To access fist component : Emp[1] 101
To access second component : Emp[2] "Vani"
2.Names: R List components can be accessed by referring to its component name inside single [ ] and
double brackets [[ ]].
List Manipulate: R - List can also be manipulated by using list component index or component name
and replacing them require data/values.
Stanley College of Engineering and Technology for Women, Dept of IT, DSR Dr.V.Sidda Reddy
6|14
Converging List into Vector: R support library function unlist() to convert list into vector
• Syntax : unlist(Lname)
Lname : List name to convert into vector
Example:
Emp<- list(Eno = 101, Ename = "Vani", Esal = 35000.00)
unlist(Emp)
Stanley College of Engineering and Technology for Women, Dept of IT, DSR Dr.V.Sidda Reddy
7|14
Matrix is a two-dimensional homogeneous data structures arrangement of data in rows and columns.
• Creating Matrix: R support inbuilt function matrix( ) to create matrix by sequence or vector
o Syntax : matrix(values, nrow, ncol, byrow, dimnames)
values : Elements of matrix
nrow : no. of rows
ncol : no. of columns
byrow : TRUE to assign elements in row wise
dimnames : Names of rows and columns
o Example 1 : matrix(1:9, nrow = 3, ncol = 3, byrow = TRUE)
o Example 2 : matrix( c(3, 6, 1, 5), nrow = 2, ncol = 2)
o Example 3 : V <- c(3, 6, 1, 5)
matrix( V, nrow = 2, ncol = 2)
Matrix Creation : Vector set: R – matrix data structures can be created by set of atomic vectors:
• Create set of atomic vectors by c( )
o V1 <- c(10, 20, 30)
o V2 <- c(40, 50, 60)
o V3 <- c(70, 80, 80)
Stanley College of Engineering and Technology for Women, Dept of IT, DSR Dr.V.Sidda Reddy
8|14
• mat[2, 2] -8
Stanley College of Engineering and Technology for Women, Dept of IT, DSR Dr.V.Sidda Reddy
9|14
Matrix Sub setting : R support matrix sub setting by using row and column index within [Rid,
Cid ]:
• mat[2, 2] -8
• mat[3, ] 23 9 4
• mat[, 1] 3 5 23
• mat [mat < 5] 3 0 -8 1 4
R class() is inbuilt function that find type of the data structures or object, class() also used to create
various components
Syntax : class(object_name)
mat<- matrix(c(3, 6, 0, 5, -8, 1, 23, 9, 4), nrow = 3, byrow = TRUE)
class(mat) "matrix" "array"
Stanley College of Engineering and Technology for Women, Dept of IT, DSR Dr.V.Sidda Reddy
10 | 1 4
Array Accessing :R support to access array elements by using row, column and matrix index inside [ ]
• Syntax : name[rid, cid, mid]
name: Array name
rid : Row index
cid : Column index
mid: Matrix index
• To access matrix 1 : arr[, , 1]
• To access 1 rows of all matrixes : arr[1, , ]
• To access 1 element of m1 : arr["R1", "C1", "M1" ]
Stanley College of Engineering and Technology for Women, Dept of IT, DSR Dr.V.Sidda Reddy
11 | 1 4
Data Frame is a two dimensional data structure that organize like table
• Data frame is a two dimensional heterogeneous data structures
• It support various kinds of data (integer, numbers, strings, logical)
• Creation : R support data.frame() function to create data frame:
o Syntax : name <- data.frame ( C1= data1, C2 = data2, … Cn = datan)
C1, C2 , …Cn : Names of columns
Stanley College of Engineering and Technology for Women, Dept of IT, DSR Dr.V.Sidda Reddy
12 | 1 4
Sorting Data Frame is a process of reorder the rows based on the values in one or more columns.
• Sorting data frame can be useful for various purposes, such as organizing data for analysis or
presentation.
• R support various methods to sort data frame
o order() : Sorting function sort data increasing and decreasing order
o arrange() : Sorting function from R – dylyr package
o setorder() : Sorting function from table.package
Factors Introduction: R - Factor is a data structure that is used to represent categorical data in level
order:
Factor Summarizing: R support summary() function to generate factor data structure summary data by
categorical wise
• Syntax: summary(name)
o name : Name of the factor
• Example: Summarizing factor
o data<- c("Prof", "HoD", "Asso.Prof", "Asst.Prof", "Prof", "Asso.Prof")
o fdes <- factor(data)
o summary(fdes)
Asso.Prof Asst.Prof HoD Prof
2 1 1 2
Stanley College of Engineering and Technology for Women, Dept of IT, DSR Dr.V.Sidda Reddy