Beruflich Dokumente
Kultur Dokumente
Data Structures
By Shano Solanki
Asst. Prof., CSE,
NITTTR, Chandigarh
Beginning with R Programming
• After installation of R Console or RStudio you
will find a R prompt i.e. >
• We can write commands on R prompt and by
pressing enter output will be displayed after
execution.
• We can quit from R working environment by
typing q() or quit().
Installation of packages
> installed.packages()
This command can be used for seeing the list of
installed packages
> library(package)
This command is used for loading packages as
required
> help(mean)
This command is used for reading help on required
item say any function for ex: help on mean as shown
in above example
> search()
This command shows a list of packages (or objects)
that are loaded and available for use.
Arithmetic Operators in R
Arithmetic Operators in R
Operator Description
+ Addition
– Subtraction
* Multiplication
/ Division
^ Exponent
%% Modulus (Remainder from division)
%/% Integer Division
> x <- 5
<- is an assignment operator for assigning any value to R variable
and this can be used in place of ‘=‘ for doing assignment of
values.
Relational Operators in R
Relational Operators in R
Operator Description
Logical Operators in R
Operator Description
! Logical NOT
| Element-wise logical OR
|| Logical OR
R as an calculator
> 3+9+12-7
[1] 17
> 12+17/2-3/4*2.5
[1] 18.625
> (12+17/2-3/4)*2.5
[1] 49.375
In maths sequence of operations can be
changed using parenthesis as in above
example
Some of the mathematical operations
available in R
> 3+9+12-7
[1] 17
> 12+17/2-3/4*2.5
[1] 18.625
> (12+17/2-3/4)*2.5
[1] 49.375
> pi
[1] 3.141593
> x=2
> y=3
> x^y
[1] 8
Some of the mathematical operations
available in R
> sqrt(x)
[1] 1.414214
> abs(x)
The log() command gives
[1] 2
> factorial(y)
you the natural log as
[1] 6 default unless you
log(x,base=2) specify the base required
[1] 1 as an additional
log10(x) instruction
[1] 0.30103
log2(2)
[1] 1
Storing the results of computation and
performing operations on stored values
> ans1=23+14/2-18+(7*pi/2)
> ans1
[1] 22.99557
> ans2=13+11+(17-4/7)
> ans2
[1] 40.42857
> ans1+ans2
[1] 63.42415
Some of the mathematical operations
available in R
> exp(4)
[1] 54.59815
> cos(x)
[1] -0.4161468
> sin(x)
[1] 0.9092974
> tan(x)
[1] -2.18504
> sin(45*pi/180)
[1] 0.7071068
Use the scan command for making
data
> data3 = scan()
1: 10 20 30
4: 40 50
6:
Read 5 items
> Data3
[1] 10 20 30 40 50
The scan command prompts you to enter your data.
The initial command creates a new data object
called data3 and initiates the data entry process.
Basic Commands in R
>myString <- "Hello, World!"
> print ( myString)
output [1] "Hello, World!“
Vector Homogeneous 1
Matrix Homogeneous 2
List Heterogeneous 1
Array Homogeneous any
Data Frames Heterogeneous 2
Factors Homogeneous -
Vectors
# Create a vector.
capsicum <- c('red','green',"yellow")
print(capsicum)
Warning message:
In x + y : longer object length is not a multiple of shorter
object length
Vector Index
Vector values can be retrieved by using an index inside a single
square bracket "[]" operator. Vector index are 1- based
For example, Use the index position 3 for retrieving the third member.
> s = c("aa", "bb", "cc", "dd", "ee")
> s[3]
[1] "cc"
Negative Index
If the index is negative, it would strip the member whose position has the same
absolute value as the negative index. For example, the following creates a vector
slice with the third member removed.
> s[-3]
[1] "aa" "bb" "dd" "ee"
Out-of-Range Index
If an index is out-of-range, a missing value will be reported via the symbol NA.
> s[10]
[1] NA
Accessing vector elements
>x
[1] 0 2 4 6 8 10
> x[3] # access 3rd element
[1] 4
> x[c(2, 4)] # access 2nd and 4th element
[1] 2 6
> x[-1] # access all but 1st element
[1] 2 4 6 8 10
> x[c(2, -4)] # cannot mix positive and negative integers Error in
x[c(2, -4)] : only 0's may be mixed with negative subscripts
> x[c(2.4, 3.54)] # real numbers are truncated to integers
[1] 2 4
DELETE A VECTOR
>x
[1] -3 -2 -1 0 1 2
> x <- NULL
>x
NULL
> x[4]
NULL
We can delete a vector by simply assigning a null to it.
Some operations using Numeric
Vectors
> x<-c(10,12,3,5,28,14)
> sum(x)
[1] 72
> max(x)
[1] 28
> min(x)
[1] 3
> length(x)
[1] 6
> sort(x,descending=TRUE)
Error in sort.int(x, na.last = na.last, decreasing = decreasing, ...) :
unused argument (descending = TRUE)
> sort(x,decreasing=TRUE)
[1] 28 14 12 10 5 3
Some operations using Numeric
Vectors
> sort(x)
[1] 3 5 10 12 14 28
> sort(x,decreasing=FALSE)
[1] 3 5 10 12 14 28
> mean(x)
[1] 12
> class(x)
[1] "numeric“
> x<-2
> sqrt(x)
> [1] 1.414214
Arithmetic operations using Numeric
Vectors: applied on all elements of an
vector
> x+5
[1] 15 17 8 10 33 19
> x-5
[1] 5 7 -2 0 23 9
> x*5
[1] 50 60 15 25 140 70
> x/2
[1] 5.0 6.0 1.5 2.5 14.0 7.0
Creating a Vector using assign and rep function
> assign("y",c(2,4,5,8,10,13,20))
>y
[1] 2 4 5 8 10 13 20
> rep(1:5,times=2)
[1] 1 2 3 4 5 1 2 3 4 5
> rep(1:5,each=2)
[1] 1 1 2 2 3 3 4 4 5 5
> rep(1:5,each=3)
[1] 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5
> z<-rep(1:5,each=3)
>z
[1] 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5
Creating a Character vector and logical vector
> colours=c("RED","ORANGE","GREEN","BLUE")
> colours
[1] "RED" "ORANGE" "GREEN" "BLUE"
> is.character(colours)
[1] TRUE
> a<-c(TRUE,FALSE,TRUE,NA)
>a
[1] TRUE FALSE TRUE NA
> b<-c(12,-4,3,10,-5)
> b<0
[1] FALSE TRUE FALSE FALSE TRUE
Creating a Vector using seq function
> a<-seq(1,20,by=2)
>a
[1] 1 3 5 7 9 11 13 15 17 19
> a<-seq(from=1,to=10,by=2)
>a
[1] 1 3 5 7 9
Mathematical operations between two numeric vectors
> r<-c(3,6,9,12,15);y<-c(5,4,3,2,1)
> t<-r+y
>t
[1] 8 10 12 14 16
> t<-r-y
>t
[1] -2 2 6 10 14
> t<-r*y
>t
[1] 15 24 27 24 15
Entering text as data
> scan(what='character')
1: shano
2: naveen
3: krishan
4: alpana
5:
Read 4 items
[1] "shano" "naveen" "krishan" "alpana"
Modifying a vector in R
>x
[1] -3 -2 -1 0 1 2
> x[2] <- 0; x # modify 2nd element
[1] -3 0 -1 0 1 2
> x[x<0] <- 5; x # modify elements less than 0
[1] 5 0 5 0 1 2
> x <- x[1:4]; x # truncate x to first 4 elements
[1] 5 0 5 0
Reading data using separator
> names= scan(sep=',',what='character')
1: shano, naveen, krishan, alpana
5:
Read 4 items
> names [1] "shano" " naveen" "krishan"
"alpana"
Matrices
Matrices
• Matrix is a two dimensional data structure an d it can be created by
specifying the number of rows and columns, and specifying the entries
Example
> X <- matrix(c(1,2,3,4,5,6),nrow=2,ncol=3);
>X Columnwise
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
Matrices
Example
> A <- matrix(1:9,nrow=3,ncol=3,byrow=TRUE)
>A
[,1] [,2] [,3]
[1,] 1 2 3 Rowwise
[2,] 4 5 6
[3,] 7 8 9
> A[2,]
Similarly, the entire nth column A can be
[1] 4 5 6
extracted as A[ ,n].
> A[,3]
[1] 3 6 9
> A[ ,c(1,3)]
To extract more than one [,1] [,2]
rows or columns at a time. [1,] 1 3
[2,] 4 6
[3,] 7 9
Matrices
The transpose of a matrix by interchanging its columns and rows
with the function t .
>A
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
> t(A)
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
>g<-c(6,2,9)
> a<- as.matrix(g)
>a
[,1]
[1,] 6
[2,] 2
[3,] 9
Adding/Deleting Elements of Vectors and Matrices
Vectors and matrices are of fixed length and dimensions. However,
they can be reassigned
> g<-c(6,2,9)
> g<-c(g,20)
>g
[1] 6 2 9 20
x<-matrix(c(1:9),nrow=3,ncol=3)
>x A row or column can be
[,1] [,2] [,3] added using rbind() and
[1,] 1 4 7 cbind() function
[2,] 2 5 8
[3,] 3 6 9
respectively.
> cbind(x, c(10,20,30))
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 20
[3,] 3 6 9 30
> x<-matrix(c(1:9), nrow=3,ncol=3)
> x<-rbind(x, c(90,91,92))
>x
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
[4,] 90 91 92
p <- matrix(c(7,4,2), nrow=3, ncol=1)
>p
[,1]
[1,] 7
[2,] 4
[3,] 2
> x <- matrix(c(1:6),nrow=3,ncol=2)
>x
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
> z <-cbind(p,x)
>z
[,1] [,2] [,3]
[1,] 7 1 4
[2,] 4 2 5
[3,] 2 3 6
>z
[,1] [,2] [,3]
[1,] 7 1 4
[2,] 4 2 5
[3,] 2 3 6
> q1
[,1]
[1,] 1
[2,] 2
[3,] 3
> q2=t(q1)
> q2
[,1] [,2] [,3]
[1,] 1 2 3
> hh <- rbind(z,q2)
> hh
[,1] [,2] [,3]
[1,] 7 1 4
[2,] 4 2 5
[3,] 2 3 6
[4,] 1 2 3
Arrays, List, Data Frames using
R
53
INTRODUCTION TO ARRAYS IN R
54
INTRODUCTION TO ARRAYS IN R
55
This array() function creates two matrices each of order 2 by 3 with 12
elements in total starting from 1 till 12. The first argument is for elements
of an array and the second argument dim sets the dimensions of an array.
Now we can see that an array of order 2 by 3 by 2 is created with the given
elements 1 to 12. Note that the product of the dimensions should always
match the number of elements in the array.
We can also assign this array to an object x using assign operator.
56
Finding the type and dimension of an array:
It is possible to find the type of elements in the array along with the type
of array using particular command in R.
To find the type of the array x which we have created earlier, is() function
displays the type of an array and elements which forms the array.
> is(x)
> [1] "array" "structure" "vector"
> dim(x)
> [1] 2 3 2
57
Each element in an array will have unique position. For example
the element present in 1st row, 2nd column of the second
matrix will be having the position [1,2,2,].
59
Creating a list:
Suppose you want to store the information of a person like his name, wife's
name, no. of children and their ages, list is the data structure to be used.
This list() function creates a list with the given components which consists of
numeric vector and strings. A list may also contain a matrix, array etc. and can
be assigned to another variable also
> stu_list <- list(name="rahul",fathers_name="Aneesh
Gupta", num_subjects=3, sub_marks=c(70,80,85))
$name
[1] "rahul"
$fathers_name
[1] "Aneesh Gupta"
$num_subjects
[1] 3
$sub_marks
[1] 70 80 85
60
Assign a list to an object and extract a particular
component from a list :
Now let us assign this created list in an object ‘Lst’ by assign an operator.
Each element in a list will have unique position. To extract a single component
from a list, its position, or name of the component can be used.
Suppose you want to extract a particular component from a list, say “Rahul"
from the above created list, you may have to type either Lst[1] or Lst[['name']] or
Lst$name. We can also extract an particular sub component from an list, like
subject marks of first student in list.
> stu_list <- list(name="rahul",fathers_name="Aneesh Gupta",
num_subjects=3, sub_marks=c(70,80,85)) -> Lst
> Lst[['name']]
[1] "rahul"
> Lst[['fathers_name']]
[1] "Aneesh Gupta“
> Lst[4[1]]
$sub_marks
[1] 70 80 85
61
Data Frames
62
To access an existing dataframe:
Lets say we want to access the existing or in-built dataframe in R called
mtcars.
Just type the name of the dataframe and hit Enter.
Now we can see the dataframe with all the rows and columns. These row
names and column names are predefined in the built‐in dataframe called
mtcars in R.
63
To create a new dataframe :
Here we will show you an example of how to create a dataframe in R.
Suppose we want to create a dataframe with employee details by the vectors are
shown in the R console. data.frame() is the function to be used to create a
dataframe.
64
> name<-c("alpna","krishan",'naveen')
> age<-c(35,38,43)
> location<-
c("gurgaon","noida","faridabad") >
stu_rec<-data.frame(name,age,location)
> stu_rec
name age location
1 alpna 35 gurgaon
2 krishan 38 noida
3 naveen 43 faridabad
65
To get the top few or bottom few observations of a
dataframe:
Consider the inbuilt dataset mtcars we have seen. Now if we want to extract
only top few observations of the dataframe, we can use head() command .
Consider the inbuilt dataset mtcars we have seen. Now if we want to extract
only bottom few observations of the dataframe, we can use tail() command .
66
To edit the dataframe in R:
We can edit the dataframes in R easily using the edit() command.
Consider the dataset mtcars.
> edit(mtcars)
67
Some more commands
> getwd()
[1] "C:/Users/A/Documents“
> dir()
> List.files()
You can look at a directory and see which files
and folders are within it using dir() and list.files()
commands . By default it looks in the current
working directory.
> dir('C:/Users/A/Desktop')
Command to see the files/folders on desktop
Viewing previously loaded named
objects using ls() command
> ls()
[1] "A" "a_data_frame" "ans1" "ans2"
[5] "B" "data3" "i" "mystring“
[9] "names" "our.data" "pp" "V"
[13] "x" "y"
> ls(pattern='a')
[1] "a_data_frame" "ans1" "ans2" "data3" [5] "names" "our.data“
> ls(pattern='ns') [1] "ans1" "ans2"
> ls(pattern='^d')
[1] "data3"
> ls(pattern='^d|^a') [1] "a_data_frame" "ans1" "ans2" "data3“
> ls(pattern='g$') [1] "mystring" > ls(pattern='a.s') [1] "ans1" "ans2"
Viewing previously loaded named objects
containing character a, ns, starting with d
> ls(pattern='a')
[1] "a_data_frame" "ans1" "ans2" "data3"
[5] "names" "our.data"
> ls(pattern='ns')
[1] "ans1" "ans2"
> ls(pattern='^d')
[1] "data3“
Viewing previously loaded named objects
containing character starting with d or a, ending
with g, including a and s both in object names
> ls(pattern='^d|^a')
[1] "a_data_frame" "ans1" "ans2" "data3"
> ls(pattern='g$')
[1] "mystring"
> ls(pattern='a.s')
[1] "ans1" "ans2“
| pipe is used for or condition and ^ is used for
telling pattern starting with some character
Removing objects from R
> rm(list)
> remove(list)
Example of remove command for removing
object named “x”
> remove("x")
> ls()
[1] "A" "a_data_frame" "ans1" "ans2"
[5] "B" "data3" "i" "mystring"
[9] "names" "our.data" "pp" "V"
[13] "y"
Control Structures
These allow you to control the flow of execution of a
script typically inside of a function. Common ones
include:
– if, else
– for
– while
– repeat
– break
– next
– return
We don't use these while working with R interactively but
rather inside functions.
If else statement
if (condition)
{ # do something }
else
{ # do something else }
Example:
x <- 1:15
if (sample(x, 1) <= 10)
{ print("x is less than 10") }
else
{ print("x is greater than 10") }
For loop
• A for loop works on an iterable variable and
assigns successive values till the end of a
sequence.
for (i in 1:10) { >for(i in 1:10){
print(i) + print(i)} [1] 1
} [1] 2
for (i in 1:20) { [1] 3
[1] 4
if (i%%2 == 1) {
[1] 5
next [1] 6
} else { [1] 7
[1] 8
print(i) [1] 9
} [1] 10
}
Another example of for loop
i <- 1
while (i < 10) {
print(i)
i <- i + 1
}
Some useful commands
• Press esc to come back to command prompt of R
if we want to forcefully end some command and
come back to command prompt
• R ignores spaces, so we can skip giving spaces
while writing mathematical expressions
> q() for exiting from R
> ?mean for checking help on mean
> help(mean) alternate method to check help on
mean
Thank You