Ids Unit LLL Jntuh Cse
Ids Unit LLL Jntuh Cse
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, subsetting 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
Vectors
To combine the list of items to a vector, use the c() function and separate
the items by a comma.
Vectors in R are the same as the arrays in C language which are used to
hold multiple data values of the same type. One major key point is that
in R the indexing of the vector will start from ‘1’ and not from ‘0’. We can
create numeric vectors and character vectors as well.
The length is an important property of a vector. A vector length is
basically the number of elements in the vector, and it is calculated with
the help of the length() function.
Types of vectors
Vectors are of different types which are used in R.
1.Numeric vectors
Numeric vectors are those which contain numeric values such as integer,
float, etc.
Ex: 1
v1 <- c(4, 5, 6, 7)
typeof(v1)
typeof(v2)
Character vectors
Character vectors contain alphanumeric values and special characters.
typeof(v1)
Output:
[1] "character"
Ex :3
Logical vectors
Logical vectors contain boolean values such as TRUE, FALSE and NA for
Null values.
Types:
numbers
EX: 2
# Vector with numerical decimals in a sequence
numbers1 <- 1.5:6.5
numbers1
Example:
seq_vec<-seq(1,4,by=0.5)
seq_vec
class(seq_vec)
Output
[1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0
[1] "numeric"
EX: 2
seq_vec<-seq(1,4,length.out=10)
seq_vec
class(seq_vec)
Output
[1] 1.000000 1.333333 1.666667 2.000000 2.333333 2.666667
3.000000 3.333333
[9] 3.666667 4.000000
[1] "numeric"
EX: 3
seq_vec<-seq(1,4,length.out=5)
seq_vec
class(seq_vec)
Output
[1] 1.00 1.75 2.50 3.25 4.00
[1] "numeric"
EX:4
Output
[1] 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0
assign("vec2",c(6,7,8,9,10))
vec2
Output
[1] 6 7 8 9 10
Types:
1. Addition
Addition operator takes two vectors as operands, and returns the result
of sum of two vectors.
a+b
Example
In the following program, we create two integer vectors and add them using
Addition Operator.
Ex:1
a <- c(10, 20, 30, 40, 50)
b <- c(1, 3, 5, 7, 9)
result <- a + b
print(result)
Output
[1] 11 23 35 47 59
2. Subtraction
Subtraction operator takes two vectors as operands, and returns the
result of difference of two vectors.
a-b
Example
In the following program, we create two integer vectors and find their different
using Subtraction Operator.
Ex:1
a <- c(10, 20, 30, 40, 50)
b <- c(1, 3, 5, 7, 9)
result <- a - b
print(result)
Output
[1] 9 17 25 33 41
3.Multiplication
Multiplication operator takes two vectors as operands, and returns the
result of product of two vectors.
a*b
Example
In the following program, we create two integer vectors and find their
product using Multiplication Operator.
Ex:
a <- c(10, 20, 30, 40, 50)
b <- c(1, 3, 5, 7, 9)
result <- a * b
print(result)
Output
[1] 10 60 150 280 450
4.Division
Division operator takes two vectors are operands, and returns the result
of division of two vectors.
a+b
Example
In the following program, we create two integer vectors and divide them using
Division Operator.
Example.R
a <- c(10, 20, 30, 40, 50)
b <- c(1, 3, 5, 7, 9)
result <- a / b
print(result)
Output
[1] 10.000000 6.666667 6.000000 5.714286 5.555556
Accessing elements of vectors
We can access the elements of a vector with the help of vector indexing.
Indexing denotes the position where the value in a vector is stored. Indexing
will be performed with the help of integer, character, or logic.
Example:
1. seq_vec<-seq(1,4,length.out=6)
2. seq_vec
3. seq_vec[2]
Output
Example:
1. char_vec<-c("shubham"=22,"arpita"=23,"vaishali"=25)
2. char_vec
3. char_vec["arpita"]
Output
shubhamarpitavaishali
22 23 25
arpita
23
Example:
1. a<-c(1,2,3,4,5,6)
2. a[c(TRUE,FALSE,TRUE,TRUE,FALSE,TRUE)]
Output
[1] 1 3 4 6
EX
>x[c(1, 3, 4)]
[1] "a""c""c"
Matrices: Creating and Naming Matrices
Matrices are the R objects in which the elements are arranged in a two-
dimensional rectangular layout. They contain elements of the same
atomic types. Though we can create a matrix containing only characters
or only logical values, they are not of much use. We use matrices
containing numeric elements to be used in mathematical calculations.
Syntax
data is the input vector which becomes the data elements of the matrix.
nrow is the number of rows to be created.
ncol is the number of columns to be created.
byrow is a logical clue. If TRUE then the input vector elements are
arranged by row.(byrow is a logical variable. Matrices are by default
column-wise. By setting byrow as TRUE, we can arrange the data row-
wise in the matrix)
dimname is the names assigned to the rows and columns(takes two
character arrays as input for row names and column names).
data
The first argument in matrix function is data. It is the input vector which is the
data elements of the matrix.
nrow
The second argument is the number of rows which we want to create in the
matrix.
ncol
The third argument is the number of columns which we want to create in the
matrix.
byrow
The byrow parameter is a logical clue. If its value is true, then the input vector
elements are arranged by row.
dim_name
The dim_name parameter is the name assigned to the rows and columns.
EX:
print(P)
Output
[,1] [,2] [,3]
[1,] 5 6 7
[2,] 8 9 10
[3,] 11 12 13
[4,] 14 15 16
print(Q)
[,1] [,2] [,3]
[1,] 3 7 11
[2,] 4 8 12
[3,] 5 9 13
[4,] 6 10 14
print(R)
There are three ways to access the elements from the matrix.
1. We can access the element which presents on nth row and mth column.
2. We can access all the elements of the matrix which are present on the
nth row.
3. We can also access all the elements of the matrix which are present on
the mth column.
Example
Output
[1] 12
col1 col2 col3
11 12 13
1. matrix[n, m]<-y
Here, n and m are the rows and columns of the element, respectively. And, y is
the value which we assign to modify our matrix.
Example
R <-
matrix(c(5:16), nrow = 4, byrow = TRUE, dimnames = list(row_names, col_nam
es))
print(R)
Output
Example 1
R <-
matrix(c(5:16), nrow = 4, byrow = TRUE, dimnames = list(row_names, col_nam
es))
print(R)
Output
Example 2
R <-
matrix(c(5:16), nrow = 4, byrow = TRUE, dimnames = list(row_names, col_nam
es))
print(R)
Output
Example 1
R <-
matrix(c(5:16), nrow = 4, byrow = TRUE, dimnames = list(row_names, col_nam
es))
print(R)
#Adding row
rbind(R,c(17,18,19))
#Adding column
cbind(R,c(17,18,19,20))
Output
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,] 5 8 11 14 6 9 12 15 7 10 13 16
Matrix operations
Example 1
#Multiplication
mul<-R*S
print(mul)
#Multiplication by constant
mul1<-R*12
print(mul1)
#Division
div<-R/S
print(div)
Output
Applications of matrix
1. In geology, Matrices takes surveys and plot graphs, statistics, and used
to study in different fields.
2. Matrix is the representation method which helps in plotting common
survey things.
3. In robotics and automation, Matrices have the topmost elements for the
robot movements.
4. Matrices are mainly used in calculating the gross domestic products in
Economics, and it also helps in calculating the capability of goods and
products.
5. In computer-based application, matrices play a crucial role in the
creation of realistic seeming motion.
Subset matrix by row names
# creating matrix
print("Original Matrix")
print(matr)
# getting rows
rows<- c("row1","row3")
Matrix subsetting
A matrix is subset with two arguments within single brackets, [], and
separated by a comma. The first argument specifies the rows, and the
EX:
a
colnames(a) <- c("A", "B", "C")
a[1:2, ]
Output
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
A B C
[1,] 1 4 7
[2,] 2 5 8
Ex: 2
a[1:3, ]
Output
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
A B C
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
Class in R programming
Classes and Objects are basic concepts of Object-Oriented Programming
that revolve around the real-life entities.
Everything in R is an object. An object is simply a data structure that has
some methods and attributes. A class is just a blueprint or a sketch of
these objects.
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.
1.S3 Class in R
S3 class is the most popular class in the R programming
language. Most of the classes that come predefined in R are of
this type.S3 refers to a class system built into R. The system
governs how R handles objects of different classes. Certain R
functions will look up an object's S3 class, and then behave
differently in response
Output
$name
[1] "John"
$age
[1] 21
$GPA
[1] 3.5
attr(,"class")
[1] "student"
In the above example, we have created a list named student1 with three
components. Notice the creation of class,
Here, we have created a class named Student_Info with three slots (member
variables): name , age , and GPA .
Here, inside new() , we have provided the name of the class "Student_Info" and
value for all three slots.
We have successfully created the object named student1 .
Example: S4 Class in R
Output
Slot "GPA":
[1] 3.5
Output
Lacks formal definition Class defined using setClass() Class defined using setRefClass()
Objects are created by Objects are created using Objects are created using generator
setting the class attribute new() functions
R factors
The factor is a data structure which is used for fields which take only predefined
finite number of values.
These are the variable which takes a limited number of different values.
These are the data objects which are used to categorize the data and to store it
on multiple levels.
It can store both integers and strings values, and are useful in the column that
has a limited number of unique values.
Factors have labels which are associated with the unique integers stored in
it. It contains predefined set value known as levels and by default R always
sorts levels in alphabetical order.
Attributes of a factor
There are the following attributes of a factor in R
a. X
It is the input vector which is to be transformed into a factor.
b. levels
It is an input vector that represents a set of unique values which are taken by x.
c. labels
It is a character vector which corresponds to the number of labels.
d. Exclude
It is used to specify the value which we want to be excluded,
e. ordered
It is a logical attribute which determines if the levels are ordered.
f. nmax
It is used to specify the upper bound for the maximum number of level.
How to create a factor?
In R, it is quite simple to create a factor. A factor is created in two steps
R provides factor() function to convert the vector into factor. There is the following
syntax of factor() function
1. factor_data<- factor(vector)
Example
2. data <-
c("Shubham","Nishka","Arpita","Nishka","Shubham","Sumit","Nishka","Shubha
m","Sumit","Arpita","Sumit")
3. print(data)
4. print(is.factor(data))
5. # Applying the factor function.
6. factor_data<- factor(data)
7. print(factor_data)
8. print(is.factor(factor_data))
OUTPUT:
[1] Shubham Nishka Arpita Nishka Shubham Sumit Nishka Shubham Sumit
[1] Nishka
[1] Shubham Nishka Arpita Shubham Sumit Nishka Shubham Sumit Arpita
[10] Sumit
Modification of factor
Like data frames, R allows us to modify the factor. We can modify the
value of a factor by simply re-assigning it.
In R, we cannot choose values outside of its predefined levels means we
cannot insert value if it's level is not present on it.
For this purpose, we have to create a level of that value, and then we can
add it to our factor.
Example
Warning message:
1. gl(n, k, labels)
gen_factor<- gl(3,5,labels=c("BCA","MCA","B.Tech"))
gen_factor
Output
[11] B.TechB.TechB.TechB.TechB.Tech
Factors are data objects used to categorize data and store it as levels.
They can store a string as well as an integer.
They represent columns as they have a limited number of unique values.
Factors in R can be created using factor() function. It takes a vector as
input. c() function is used to create a vector with explicitly provided
values.
Example:
print(x)
print(is.factor(x))
factor_x = factor(x)
levels(factor_x)
Output :
[1] "Pen" "Pencil" "Brush" "Pen" "Brush" "Brush" "Pencil" "Pencil"
[1] FALSE
Example:
# converting to factor
size_factor<- factor(size)
print(size_factor)
print(ordered.size)
Output:
The same can be done using the ordered function. The example for the same is
as shown below:
Example:
"small", "medium"))
print(sizes)
Output:
[1] small large large small medium
Levels: small < medium < large
Comparing Ordered Factors using is.ordered() Function
EX: 1
"small", "medium"))
is.ordered(sizes)
Output:
[1] TRUE
EX: 2
# Creating a vector
is.ordered(gender)
Output:
[1] FALSE
Introduction to Data Frame
R Data Frame
R Data Frame-characteristics
In R, the data frames are created with the help of frame() function of
data. This function contains the vectors of any type such as numeric,
character, or integer. In below example, we create a data frame that
contains employee id (integer vector), employee name(character vector),
salary(numeric vector), and starting date(Date vector).
dataframe1<- data.frame(
first_col =c(val1, val2, ...),
second_col = c(val1, val2, ...),
...
)
Here,
first_col - a vector with values val1, val2, ... of same data type
second_col - another vector with values val1, val2, ... of same data type and so
on
print(dataframe1)
Output
data.frame (
Name = c("Juan", "Alcaraz", "Simantha"),
Age = c(22, 15, 19),
Vote = c(TRUE, FALSE, TRUE)
)
Here, Name, Age, and Vote are column names for vectors
of String, Numeric, and Boolean type respectively.
And finally the datas represented in tabular format are printed.
There are different ways to extract columns from a data frame. We can
use [ ] , [[ ]] , or $ to access specific column of a data frame in R. For
example,
Output
Name
1 Juan
2 Alcaraz
3 Simantha
[1] "Juan" "Alcaraz" "Simantha"
[1] "Juan" "Alcaraz" "Simantha"
If we want to combine two data frames vertically, the column name of the two
data frames must be the same. For example,
Output
Name Age
1 Juan 22
2 Alcaraz 15
3 Yiruma 46
4 Bach 89
Here, we have used the rbind() function to combine the two data
frames: dataframe1 and dataframe2 vertically.
Combine Horizontally Using cbind()
The cbind() function combines two or more data frames horizontally. For
example,
Output
Note: The number of items on each vector of two or more combining data
frames must be equal otherwise we will get an error: arguments imply differing
number of rows or columns .
Length of a Data Frame in R
Output
Total Elements: 3
Here, we have used length() to find the total number of columns in dataframe1.
Since there are 3 columns, the length() function returns 3.
Note: Additional
Example
Output
emp.data.employee_idemp.data.sal
1 1 623.30
2 2 515.20
3 3 611.00
4 4 729.00
5 5 843.25
Example
Output
employee_idemployee_namesalstarting_date
1 1 Shubham 623.3 2012-01-01
employee_idemployee_namesalstarting_date
4 4 Gunjan 729.00 2014-05-11
5 5 Sumit 843.25 2015-03-27
Example
Output
employee_idstarting_date
2 2 2013-09-23
3 3 2014-11-15
We can
1. Add a column by adding a column vector with the help of a new column
name using cbind() function.
2. Add rows by adding new rows in the same structure as the existing data
frame and using rbind() function
Output
employee_idemployee_namesalstarting_date
1 1 Shubham 623.30 2012-01-01
2 2 Arpita 515.20 2013-09-23
3 3 Nishka 611.00 2014-11-15
4 4 Gunjan 729.00 2014-05-11
5 5 Sumit 843.25 2015-03-27
employee_idemployee_namesalstarting_date
1 1 Shubham 623.30 2012-01-01
2 2 Arpita 515.20 2013-09-23
3 3 Nishka 611.00 2014-11-15
4 4 Gunjan 729.00 2014-05-11
5 5 Sumit 843.25 2015-03-27
6 6 Vaishali 547.00 2015-09-01
employee_idemployee_namesalstarting_date Address
1 1 Shubham 623.30 2012-01-01 Moradabad
2 2 Arpita 515.20 2013-09-23 Lucknow
3 3 Nishka 611.00 2014-11-15 Etah
4 4 Gunjan 729.00 2014-05-11 Sambhal
5 5 Sumit 843.25 2015-03-27 Khurja
Output
employee_idemployee_namesalstarting_date
1 1 Shubham623.30 2012-01-01
2 2 Arpita515.20 2013-09-23
3 3 Nishka611.00 2014-11-15
4 4 Gunjan729.00 2014-05-11
5 5 Sumit843.25 2015-03-27
employee_idemployee_namesalstarting_date
2 2 Arpita515.20 2013-09-23
3 3 Nishka611.00 2014-11-15
4 4 Gunjan729.00 2014-05-11
5 5 Sumit843.25 2015-03-27
employee_idemployee_namesal
1 1 Shubham623.30
2 2 Arpita515.20
3 3 Nishka611.00
4 4 Gunjan729.00
5 5 Sumit843.25
Sorting Data Frames
ASCENDING.
where
Coding:
o # subjects columns
o data = data.frame(
o print(data)
subjects ")
rollno ")
rollno subjects
1 1 java
2 5 python
3 4 php
4 2 sql
5 3 c
rollno subjects
4 2 sql
2 5 python
3 4 php
1 1 java
5 3 c
rollno subjects
2 5 python
3 4 php
5 3 c
4 2 sql
1 1 java
Coding:
"gnanesh"),
based on subjects")
o print(data[order(data$subjects, decreasing
= FALSE), ] )
based on rollno")
o print(data[order(data$rollno, decreasing =
FALSE), ] )
based on names")
o print(data[order(data$names,decreasing =
FALSE), ] )
Output:
1 1 sravan java
2 5 bobby python
3 4 pinkey php
4 2 rohith sql
5 3 gnanesh c
5 3 gnanesh c
1 1 sravan java
3 4 pinkey php
2 5 bobby python
4 2 rohith sql
1 1 sravan java
4 2 rohith sql
5 3 gnanesh c
3 4 pinkey php
2 5 bobby python
2 5 bobby python
5 3 gnanesh c
3 4 pinkey php
4 2 rohith sql
1 1 sravan java
Syntax: arrange(dataframe,column)
where
sorted
Syntax: install.packages(“dplyr”)
Code:
library("dplyr")
o "rohith", "gnanesh"),
"sql", "c"))
print(arrange(data, subjects))
Output:
1 3 gnanesh c
2 1 sravan java
3 4 pinkey php
4 5 bobby python
5 2 rohith sql
Code:
library("data.table")
# create dataframe with roll no, names
"rohith", "gnanesh"),
"c"))
print(setorder(data,subjects))
Output:
5 3 gnanesh c
1 1 sravan java
3 4 pinkey php
2 5 bobby python
4 2 rohith sql
Lists
A list in R can contain many different data types inside it. A list is a
collection of data which is ordered and changeable.
A list is a vector but with heterogeneous data elements. A list in R is
created with the use of list() function. R allows accessing elements of a
list with the use of the index value. In R, the indexing of a list starts
with 1 instead of 0 like other programming languages.
In R, lists are the second type of vector. Lists are the objects of R which
contain elements of different types such as number, vectors, string and
another list inside it. It can also contain a function or a matrix as its
elements.
A list is a data structure which has components of mixed data types. We
can say, a list is a generic vector which contains other objects.
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.
Example:
vec<- c(3,4,5,6)
char_vec<-c("shubham","nishka","gunjan","sumit")
logic_vec<-c(TRUE,FALSE,FALSE,TRUE)
out_list<-list(vec,char_vec,logic_vec)
out_list
Output
[[1]]
[1] 3 4 5 6
[[2]]
[1] "shubham" "nishka" "gunjan" "sumit"
[[3]]
[1] TRUE FALSE FALSE TRUE
Lists creation
The process of creating a list is the same as a vector. In R, the vector is
created with the help of c() function. Like c() function, there is another
function, i.e., list() which is used to create a list in R.
A list avoid the drawback of the vector which is data type. We can add
the elements in the list of different data types.
Syntax
1. list()
list_1<-list(1,2,3)
list_2<-list("Shubham","Arpita","Vaishali")
list_3<-list(c(1,2,3))
list_4<-list(TRUE,FALSE,TRUE)
list_1
list_2
list_3
list_4
Output:
[[1]]
[1] 1
[[2]]
[1] 2
[[3]]
[1] 3
[[1]]
[1] "Shubham"
[[2]]
[1] "Arpita"
[[3]]
[1] "Vaishali"
[[1]]
[1] 1 2 3
[[1]]
[1] TRUE
[[2]]
[1] FALSE
[[3]]
[1] TRUE
list_data<-
list("Shubham","Arpita",c(1,2,3,4,5),TRUE,FALSE,
22.5,12L)
print(list_data)
In the above example, the list function will create a list with character, logical,
numeric, and vector element. It will give the following output
Output:
[[1]]
[1] "Shubham"
[[2]]
[1] "Arpita"
[[3]]
[1] 1 2 3 4 5
[[4]]
[1] TRUE
[[5]]
[1] FALSE
[[6]]
[1] 22.5
[[7]]
[1] 12
Giving a name to list elements
R provides a very easy way for accessing elements, i.e., by giving the
name to each element of a list. By assigning names to the elements, we
can access the element easily. There are only three steps to print the list
data corresponding to the name:
Creating a list.
Assign a name to the list elements with the help of names()
function.
Print the list data.
Example
Output:
$Students
[1] "Shubham" "Nishka" "Gunjan"
$Marks
[,1] [,2] [,3]
[1,] 40 60 90
[2,] 80 70 80
$Course
$Course[[1]]
[1] "BCA"
$Course[[2]]
[1] "MCA"
$Course[[3]]
[1] "B. tech."
list_data <-
list(c("Shubham","Arpita","Nishka"), matrix(c(40,80,60,
list("BCA","MCA","B.tech"))
print(list_data[1])
print(list_data[3])
[1]]
[1] "Shubham" "Arpita" "Nishka"
[[1]]
[[1]][[1]]
[1] "BCA"
[[1]][[2]]
[1] "MCA"
[[1]][[3]]
[1] "B.tech"
Example 2: Accessing elements using names
Output:
$Student
[1] "Shubham" "Arpita" "Nishka"
$Student
[1] "Shubham" "Arpita" "Nishka"
$Marks
[,1] [,2] [,3]
[1,] 40 60 90
[2,] 80 70 80
$Course
$Course[[1]]
[1] "BCA"
$Course[[2]]
[1] "MCA"
$Course[[3]]
[1] "B. tech."
Example
list_data <-
list(c("Shubham","Arpita","Nishka"), matrix(c(40,80,60,
list("BCA","MCA","B.tech"))
print(list_data[4])
print(list_data[4])
print(list_data[3])
Output:
[[1]]
[1] "Moradabad"
$<NA>
NULL
$Course
[1] "Masters of computer applications"
# Creating lists.
list1 <- list(10:20)
print(list1)
list2 <-list(5:14)
print(list2)
# Converting the lists to vectors.
v1 <- unlist(list1)
v2 <- unlist(list2)
print(v1)
print(v2)
adding the vectors
result <- v1+v2
print(result)
Output:
[[1]]
[1] 1 2 3 4 5
[[1]]
[1] 10 11 12 13 14
[1] 1 2 3 4 5
[1] 10 11 12 13 14
[1] 11 13 15 17 19
Merging Lists
R allows us to merge one or more lists into one list. Merging is done with
the help of the list() function also.
To merge the lists, we have to pass all the lists into list function as a
parameter, and it returns a list which contains all the elements which
are present in the lists.
Example
print(merged.list)
Output:
[[1]][[1]]
[[1]][[1]]
[1] 2
[[1]][[2]]
[1] 4
[[1]][[3]]
[1] 6
[[1]][[4]]
[1] 8
[[1]][[5]]
[1] 10
[[2]]
[[2]][[1]]
[1] 1
[[2]][[2]]
[1] 3
[[2]][[3]]
[1] 5
[[2]][[4]]
[1] 7
[[2]][[5]]
[1] 9
Note:
R Arrays
In R, arrays are the data objects which allow us to store data
in more than two dimensions. In R, an array is created with
the help of the array() function. This array() function takes a
vector as an input and to create an array it uses vectors values
in the dim parameter.
For example- if we will create an array of dimension (2, 3, 4)
then it will create 4 rectangular matrices of 2 row and 3
columns.
R Array Syntax
1. array_name <-
array(data, dim= (row_size, column_size, matrices, dim_names))
data
The data is the first argument in the array() function. It is an input
vector which is given to the array.
matrices
row_size
column_size
dim_names
Uni-Dimensional Array
A vector is a uni-dimensional array, which is specified by a
single dimension, length. A Vector can be created using ‘c()‘
function. A list of values is passed to the c() function to create
a vector.
EX:
print (vec1)
Multi-Dimensional Array
A two-dimensional matrix is an array specified by a fixed
number of rows and columns, each containing the same data
type. A matrix is created by using array() function to which
the values and the dimensions are passed.
EX:1
print(arr)
Output
,,1
,,2
[,1] [,2] [,3]
[1,] 8 10 12
[2,] 9 11 13
Creation of arrays
Example
vec1 <-c(1,3,5)
vec2 <-c(10,11,12,13,14,15)
print(res)
Output
, , Matrix1
, , Matrix2
Accessing arrays
The arrays can be accessed by using indices for different
dimensions separated by commas. Different components can
be specified by any combination of elements’ names or
positions.
Accessing Uni-Dimensional Array
The elements can be accessed by using indexes of the
corresponding elements.
EX:1
# accessing elements
Output
Vector is : 1 2 3 4 5 6 7 8 9 10Third element of vector is : 3
EX: 1
print(array1)
# access entire elements at 2nd column of 1st matrix
cat("\n2nd Column Elements of 1st matrix:",
array1[,c(2),1])
Output
,,1
,,2
EX: 1
array1
array2
print(result)
Output
,,1
,,2
,,1
,,2
EX: 1
vec1 <-c(1,3,5)
vec2 <-c(10,11,12,13,14,15)
print(res1)
print(result)
Output
,,1
,,2
[1] 48 56 64
Output
,,1
,,2
[1] 18 66 84
EX: 2
vec1 <-c(1,3,5)
vec2 <-c(10,11,12,13,14,15)
print(res1)
print(result)
Output
,,1
,,2
IMPORTANT QUESTIONS
1) Vectors
2) Factors
3) Data Frames
4) Lists
5) Arrays
6) Matrices
7) CLASS