Download as pdf or txt
Download as pdf or txt
You are on page 1of 33

R Tutorial

SEC 1 (Semester III)


Part III

Dr. Moutushi Chatterjee


Complex Numbers in R
• > sqrt(-17)
[1] NaN
Warning message:
In sqrt(-17) : NaNs produced

• > sqrt(-17+0i)
[1] 0+4.123106i

• > sqrt(as.complex(−1))
[1] 0+1i
sqrt(as.complex(-17))
[1] 0+4.123106i

sqrt(as.complex(17))
[1] 4.123106+0i
• A complex value in R is defined via the pure
imaginary value i.

• > z = 1 + 2i # create a complex number


>z # print the value of z
[1] 1+2i
> class(z) # print the class name of z
[1] "complex"
‘as’ Function in R
• Force An Object To Belong To A Class
– as.numeric
– as.complex
– as. factor
Assignment of values
• To set up a vector named x, say, consisting of
five numbers, namely 10.4, 5.6, 3.1, 6.4 and
21.7, use the R command
x <- c(10.4, 5.6, 3.1, 6.4, 21.7)
x
[1] 10.4 5.6 3.1 6.4 21.7
• This is an assignment statement using the
function c() which in this context can take an
arbitrary number of vector arguments
1/x
[1] 0.09615385 0.17857143 0.32258065
0.15625000 0.04608295
• The reciprocals of the values would be printed at
the terminal

y <- c(x, 0, x)
Y
• would create a vector y with 11 entries consisting
of two copies of x with a zero in the middle-place.
x<-6
y<-4
z<-x+y
> z # OUTPUT OF z
[1] 10

• At any time you can list the objects which have been
created:
• ls()
[1] "x" "y" "z“
Useful when one has to recall all the variables which have
been defined.
rm(x,y) # Removes the variables x and y

x<-6

y<-4

a<-2

rm(x,y)

>x
Error: object 'x' not found
>a
[1] 2
> data1<- c(3, 5, 7, 5, 3, 2, 6, 8, 5, 6, 9)
> data1
[1] 3 5 7 5 3 2 6 8 5 6 9

> data2<- c(data1, 4, 5, 7, 3, 4)


> data2
[1] 3 5 7 5 3 2 6 8 5 6 9 4 5 7 3 4
Generating regular sequences
> s3<- seq(-5, 5, by=.2)
> s3
[1] -5.0 -4.8 -4.6 -4.4 -4.2 -4.0 -3.8 -3.6 -3.4 -3.2 -
3.0 -2.8 -2.6 -2.4 -2.2
[16] -2.0 -1.8 -1.6 -1.4 -1.2 -1.0 -0.8 -0.6 -0.4 -0.2
0.0 0.2 0.4 0.6 0.8
[31] 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0
3.2 3.4 3.6 3.8
[46] 4.0 4.2 4.4 4.6 4.8 5.0
> length(s3)
[1] 51
➢s4 <- seq(length=51, from=-5, by=.2)

➢Generates same vector. Only difference with


the earlier command being, for s3, upper limit
is specified, while for s4, length is specified.

➢In this case, either the step length or the total


length of the sequence (it doesn't make sense
to use both).
• If both of these options are left out, R will
make its own default choice, in this case
assuming a step length of 1. So,

> s7<- seq(-5, 5)


> s7
[1] -5 -4 -3 -2 -1 0 1 2 3 4 5
> 7:11
[1] 7 8 9 10 11
Entering text items as data
> day<-c('Mon', 'Tue', 'Wed', 'Thu')
> day
[1] "Mon" "Tue" "Wed" "Thu“
• > class(day)
• [1] "character"

• Add more data


> day1 <- c(day, 'Fri')
> day1
[1] "Mon" "Tue" "Wed" "Thu" "Fri"
Vector index
> day[3]
[1] "Wed“
• Negative Index
> day[-3] # Removes corresponding index
[1] "Mon" "Tue" "Thu"
• Out of range Index
> day[10]
[1] NA
> day[-10] # Nothing to remove
[1] "Mon" "Tue" "Wed" "Thu"
rep() Function
• A related function is rep() which can be used
for replicating an object in various
complicated ways.
> x<- c(10.4, 5.6, 3.1, 6.4, 21.7)
>x
[1] 10.4 5.6 3.1 6.4 21.7
> rep(0,100)
[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
000000000000
[38] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0000000000000
[75] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
00
> rep(c(1,2),4)
[1] 1 2 1 2 1 2 1 2
> rep(1:3,6)
[1] 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3
> rep(1:3,c(6,6,6))
[1] 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3

> rep(1:3,rep(6,3))
[1] 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3
> s5 <- rep(x, times=5)
> s5
[1] 10.4 5.6 3.1 6.4 21.7 10.4 5.6 3.1 6.4 21.7
10.4 5.6 3.1 6.4 21.7
[16] 10.4 5.6 3.1 6.4 21.7 10.4 5.6 3.1 6.4
21.7

• which will put five copies of x end-to-end in


s5.
• Another useful version is
> s6 <- rep(x, each=5)
> s6
[1] 10.4 10.4 10.4 10.4 10.4 5.6 5.6 5.6 5.6
5.6 3.1 3.1 3.1 3.1 3.1
[16] 6.4 6.4 6.4 6.4 6.4 21.7 21.7 21.7 21.7
21.7
• which repeats each element of x five times
before moving on to the next.
List()
A list is a generic vector containing other objects.
The following variable x is a list containing copies of
three vectors n, s, b, and a numeric value 3.
n = c(2, 3, 5)
s = c("aa", "bb", "cc", "dd", "ee")
b = c(TRUE, FALSE, TRUE, FALSE, FALSE)
x = list(n, s, b, 3) # x contains copies of n, s, b
x
The output is,
[[1]]
[1] 2 3 5

[[2]]
[1] "aa" "bb" "cc" "dd" "ee"

[[3]]
[1] TRUE FALSE TRUE FALSE FALSE

[[4]]
[1] 3
List Slicing
• We retrieve a list slice with the single square
bracket "[]" operator. The following is a slice
containing the second member of x, which is a
copy of s.
• x[2]
[[1]]
[1] "aa" "bb" "cc" "dd" "ee"
• With an index vector, we can retrieve a slice
with multiple members. Here a slice
containing the second and fourth members
of x.
x[c(2, 4)]
[[1]]
[1] "aa" "bb" "cc" "dd" "ee"

[[2]]
[1] 3
Member Reference
• In order to reference a list member directly,
we have to use the double square
bracket "[[]]" operator. The following
object x[[2]] is the second member of x. In
other words, x[[2]] is a copy of s, but is not a
slice containing s or its copy.
x[[2]]
[1] "aa" "bb" "cc" "dd" "ee"
We can modify its content directly.
> x[[2]][1]
[1] "aa"

> x[[2]]
[1] “aa" "bb" "cc" "dd" "ee"
>s
[1] "aa" "bb" "cc" "dd" "ee" # s is unaffected
Interestingly,
x[2][1]
[[1]]
[1] "aa" "bb" "cc" "dd" "ee“

Thus, the output is same as that of x[2]


Inf vs. NaN vs. NA
> pi
[1] 3.141593
> pi/0 # a non-zero number divided by zero
creates infinity
[1] Inf
> 0/0 # the so-called Not a Number NaN
[1] NaN
• which give NaN since the result cannot be
defined sensibly.
• NA (“Not Available”) is generally interpreted as a
missing value
Vector Arithmetic
> a<-c(6,8,9)
> b<-c(1,2,4)
> a+b # Element wise addition
[1] 7 10 13
> a*b
[1] 6 16 36
> a+2
[1] 8 10 11
> sum(a) # Sum of the elements of a vector
[1] 23

> length(a)
[1] 3
> t(a) # Row vector
[,1] [,2] [,3]
[1,] 6 8 9

> a%*%(b) # Vector multiplication


[,1]
[1,] 58

> t(a)%*%b
[,1]
[1,] 58
Matrix as product of two vectors
> a%*%t(b)
[,1] [,2] [,3]
[1,] 6 12 24
[2,] 8 16 32
[3,] 9 18 36
Excersise

You might also like