You are on page 1of 24

Expt. No.

…………
BASIC MATH
Date:……………………

# BASIC MATH

2-5

6/3

3+2*5

(3+2)*5

4^3

exp(6)

log(2.742) # natural log

log10(1000)

pi/4

sqrt(100)

abs(-4)

factorial(5)

sin(45)

cos(45)

tan(45)

sinpi(1/4)

cospi(1/4)

tanpi(1/4)
Expt. No.…………
BASIC MATH
Date:……………………
> 2-5

[1] -3
> 6/3
[1] 2
> 3+2*5
[1] 13
> (3+2)*5
[1] 25
> 4^3
[1] 64
> exp(6)
[1] 403.4288
> log(2.742) # natural log
[1] 1.008688
> log10(1000)
[1] 3
> pi/4
[1] 0.7853982
> sqrt(100)
[1] 10
> abs(-4)
[1] 4
> factorial(5)
[1] 120
> sin(45)
[1] 0.8509035
> cos(45)
[1] 0.525322
> tan(45)
[1] 1.619775
> sinpi(1/4)
[1] 0.7071068
> cospi(1/4)
[1] 0.7071068
> tanpi(1/4)
[1] 1

Expt. No.…………
Vector/variable
Date:……………………

# Varible \ vector

# Data type - Numeric data

x<-5

y<-3

class(x)

length(x)

x+y

x-y

x^y

sqrt(x)

exp(y)

# Data type - Character (text) data

t<-"data"

m<-'programming'

class(t)

nchar(m)
# Vector

# the c function

v<-c(2,5,7,6,4,9,1,7,7,7)

u<-c(11,17,16,18,14,13,12,19,11,11)

class(u)

length(u)

sort(u)

sort(u, decreasing = T)

v+2

v^2

u+v

u-2*v

u1<-c('one', 'two', 'three', 'four')

class(u1)

length(u1)

# The scan function

s<-scan()

10

20

30

40

50
s

#for character/text data

st<-scan(what = 'character')

Jan

Feb

Mar

apr

May

Jun

st

# just copy data from word or other files using scan fn.
the nos are separated by space

cn<-scan()

1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9

cn

# just copy data from word or other files using scan fn.
the nos are separated by comma

cn1<-scan(sep = ',')

1,2,3,7,8,9,4,5,6,14,12 1,2,3,7,8,9,4,5,6,14,12
cn1

ct<-scan(what = 'character')

Jyothis Thomas professor dept of mathematics

ct

ct1<-scan(what = 'character', sep = ',')

First,semester,mca,students

ct1

# the seq function

sf<-seq(from=1, to=10, by=2)

sf

sf1<-seq(10,100,10)

sf1

sf2<-10:20

sf2

# Displaying individual elements or parts of a vector

q<-sample(1:50,30, replace = F)

sort(q)

q[4]

q[11]
q[1:10]

q[c(1,11,21)]

q[q<15]

q[q>40]

q[q>10 & q<40]

length(q[q>10 & q<40]) # this command can be used to form


the frequency distribution
> # Varible \ vector
> # Data type - Numeric data
> x<-5
> x
[1] 5
> y<-3
> y
[1] 3
> class(x)
[1] "numeric"
> length(x)
[1] 1
> x+y
[1] 8
> x-y
[1] 2
> x^y
[1] 125
> sqrt(x)
[1] 2.236068
> exp(y)
[1] 20.08554
>
> # Data type - Character (text) data
> t<-"data"
> t
[1] "data"
> m<-'programming'
> m
[1] "programming"
> class(t)
[1] "character"
> nchar(m)
[1] 11
>
> # Vector
> # the c function
> v<-c(2,5,7,6,4,9,1,7,7,7)
> v
[1] 2 5 7 6 4 9 1 7 7 7
> u<-c(11,17,16,18,14,13,12,19,11,11)
> u
[1] 11 17 16 18 14 13 12 19 11 11
> class(u)
[1] "numeric"
> length(u)
[1] 10
> sort(u)
[1] 11 11 11 12 13 14 16 17 18 19
> sort(u, decreasing = T)
[1] 19 18 17 16 14 13 12 11 11 11
> v+2
[1] 4 7 9 8 6 11 3 9 9 9
> v^2
[1] 4 25 49 36 16 81 1 49 49 49
> u+v
[1] 13 22 23 24 18 22 13 26 18 18
> u-2*v
[1] 7 7 2 6 6 -5 10 5 -3 -3
>
> u1<-c('one', 'two', 'three', 'four')
> class(u1)
[1] "character"
> length(u1)
[1] 4
>
>
> # The scan function
> s<-scan()
1: 10
2: 20
3: 30
4: 40
5: 50
6:
Read 5 items
>
> s
[1] 10 20 30 40 50
> #for character/text data
> st<-scan(what = 'character')
1: Jan
2: Feb
3: Mar
4: apr
5: May
6: Jun
7:
Read 6 items
>
> st
[1] "Jan" "Feb" "Mar" "apr" "May" "Jun"
> # just copy data from word or other files using scan fn
. the nos are separated by space
> cn<-scan()
1: 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9
28:
Read 27 items
>
> cn
[1] 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8
9
> # just copy data from word or other files using scan fn
. the nos are separated by comma
> cn1<-scan(sep = ',')
1: 1,2,3,7,8,9,4,5,6,14,12 1,2,3,7,8,9,4,5,6,14,12
22:
Read 21 items
>
> cn1
[1] 1 2 3 7 8 9 4 5 6 14 121 2 3
7 8 9 4 5 6 14 12
> ct<-scan(what = 'character')
1: Jyothis Thomas professor dept of mathematics
7:
Read 6 items
>
> ct
[1] "Jyothis" "Thomas" "professor" "dept"
"of" "mathematics"
> ct1<-scan(what = 'character', sep = ',')
1: First,semester,mca,students
5:
Read 4 items
>
> ct1
[1] "First" "semester" "mca" "students"
>
> # the seq function
> sf<-seq(from=1, to=10, by=2)
> sf
[1] 1 3 5 7 9
> sf1<-seq(10,100,10)
> sf1
[1] 10 20 30 40 50 60 70 80 90 100
> sf2<-10:20
> sf2
[1] 10 11 12 13 14 15 16 17 18 19 20
>
>
> # Displaying individual elements or parts of a vector
> q<-sample(1:50,30, replace = F)
> q
[1] 21 20 48 25 32 43 45 44 24 50 35 37 3 4 38 10 17
7 31 40 1 36 13 49 19 11 8 47
[29] 42 33
> sort(q)
[1] 1 3 4 7 8 10 11 13 17 19 20 21 24 25 31 32 33 3
5 36 37 38 40 42 43 44 45 47 48
[29] 49 50
> q[4]
[1] 25
> q[11]
[1] 35
> q[1:10]
[1] 21 20 48 25 32 43 45 44 24 50
> q[c(1,11,21)]
[1] 21 35 1
> q[q<15]
[1] 3 4 10 7 1 13 11 8
> q[q>40]
[1] 48 43 45 44 50 49 47 42
> q[q>10 & q<40]
[1] 21 20 25 32 24 35 37 38 17 31 36 13 19 11 33
> length(q[q>10 & q<40]) # this command can be used to
form the frequency distribution
[1] 15
Expt. No.…………
DATA FRAME
Date:……………………

# data.frame

x<-1:10

y<--4:5

z<-scan(what = 'character')

jan

feb

mar

apr

may

jun

jul

aug

sep

oct

df<-data.frame(x,y,z)

df

nrow(df) # to find the no of rows

ncol(df) # to find the no of columns

dim(df) # find the dimension

class(df) # which category

rownames(df) # display row names


colnames(df) # display column names

colnames(df)<-c('FIRST', 'SECOND', 'THIRD') # to rename


column names

colnames(df)

rownames(df)<-scan(what = 'character')

one

two

three

four

five

six

seven

eight

nine

ten

rownames(df)

df

df$FIRST # to display the first column only

df[ ,"FIRST"] # to display the first column only

df[3:7,2:3] # display selected portion

df1<-cbind.data.frame(df, 41:50) # insert additional


column

colnames(df1)<-c('FIRST', 'SECOND', 'THIRD','FOURTH')


# to rename the fourth column

df1

w<-51:60
df2<-data.frame(x,y,z,w) # insert additional column

df2

df2$w<-NULL # remove column w

df2
> x<-1:10
> y<--4:5
> z<-scan(what = 'character')
1: jan
2: feb
3: mar
4: apr
5: may
6: jun
7: jul
8: aug
9: sep
10: oct
11:
Read 10 items
>
> df<-data.frame(x,y,z)
> df
x y z
1 1 -4 jan
2 2 -3 feb
3 3 -2 mar
4 4 -1 apr
5 5 0 may
6 6 1 jun
7 7 2 jul
8 8 3 aug
9 9 4 sep
10 10 5 oct
> nrow(df) #to find the no of rows
[1] 10
> ncol(df) # to find the no of columns
[1] 3
> dim(df) # find the dimension
[1] 10 3
> class(df) # which category
[1] "data.frame"
> rownames(df) # display row names
[1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10"
> colnames(df) # display column names
[1] "x" "y" "z"
> colnames(df)<-c('FIRST', 'SECOND', 'THIRD') # to rename
column names
> colnames(df)
[1] "FIRST" "SECOND" "THIRD"
> rownames(df)<-scan(what = 'character')
1: one
2: two
3: three
4: four
5: five
6: six
7: seven
8: eight
9: nine
10: ten
11:
Read 10 items
>
> rownames(df)
[1] "one" "two" "three" "four" "five" "six" "sev
en" "eight" "nine" "ten"
> df
FIRST SECOND THIRD
one 1 -4 jan
two 2 -3 feb
three 3 -2 mar
four 4 -1 apr
five 5 0 may
six 6 1 jun
seven 7 2 jul
eight 8 3 aug
nine 9 4 sep
ten 10 5 oct
> df$FIRST # to display the first column only
[1] 1 2 3 4 5 6 7 8 9 10
> df[ ,"FIRST"] # to display the first column only
[1] 1 2 3 4 5 6 7 8 9 10
> df[3:7,2:3] # display selected portion
SECOND THIRD
three -2 mar
four -1 apr
five 0 may
six 1 jun
seven 2 jul
> df1<-cbind.data.frame(df, 41:50) # insert additional co
lumn
> colnames(df1)<-c('FIRST', 'SECOND', 'THIRD','FOURTH') #
to rename the fourth column
> df1
FIRST SECOND THIRD FOURTH
one 1 -4 jan 41
two 2 -3 feb 42
three 3 -2 mar 43
four 4 -1 apr 44
five 5 0 may 45
six 6 1 jun 46
seven 7 2 jul 47
eight 8 3 aug 48
nine 9 4 sep 49
ten 10 5 oct 50
> w<-51:60
> df2<-data.frame(x,y,z,w) # insert additional column
> df2
x y z w
1 1 -4 jan 51
2 2 -3 feb 52
3 3 -2 mar 53
4 4 -1 apr 54
5 5 0 may 55
6 6 1 jun 56
7 7 2 jul 57
8 8 3 aug 58
9 9 4 sep 59
10 10 5 oct 60
> df2$w<-NULL # remove column w
> df2
x y z
1 1 -4 jan
2 2 -3 feb
3 3 -2 mar
4 4 -1 apr
5 5 0 may
6 6 1 jun
7 7 2 jul
8 8 3 aug
9 9 4 sep
10 10 5 oct
Expt. No.…………
MATRIX
Date:……………………

# matrix function

A<-matrix(1:10, nrow = 5, ncol = 2, byrow = T)

B<-matrix(c(11,12,13,14,15,16,17,18,19,20), nrow = 5,
byrow = F)

D<-matrix(1:9, nrow = 3, byrow = T)

A+B

A-B

A*B

D^(-1) # inverse of D

det(D) # determinant of D

t(A) # transpose of A

rownames(D)<-c('one','two', 'three')

colnames(D)<-c("First", "second",'third')

D^(-1)

dim(D)

class(D)

# inserting rows and columns


A1<-rbind(A, c(22,23))

A1

B1<-cbind(B,c(31,32,33,34,35))

B1

# inserting large files from excel or word or csv file

# first save the excel file as .csv files

# R<- read.csv(file.choose())
> # matrix function
> A<-matrix(1:10, nrow = 5, ncol = 2, byrow = T)
> A
[,1] [,2]
[1,] 1 2
[2,] 3 4
[3,] 5 6
[4,] 7 8
[5,] 9 10
> B<-matrix(c(11,12,13,14,15,16,17,18,19,20), nrow = 5, b
yrow = F)
> B
[,1] [,2]
[1,] 11 16
[2,] 12 17
[3,] 13 18
[4,] 14 19
[5,] 15 20
> D<-matrix(1:9, nrow = 3, byrow = T)
> D
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
> A+B
[,1] [,2]
[1,] 12 18
[2,] 15 21
[3,] 18 24
[4,] 21 27
[5,] 24 30
> A-B
[,1] [,2]
[1,] -10 -14
[2,] -9 -13
[3,] -8 -12
[4,] -7 -11
[5,] -6 -10
> A*B
[,1] [,2]
[1,] 11 32
[2,] 36 68
[3,] 65 108
[4,] 98 152
[5,] 135 200
> D^(-1)
[,1] [,2] [,3]
[1,] 1.0000000 0.500 0.3333333
[2,] 0.2500000 0.200 0.1666667
[3,] 0.1428571 0.125 0.1111111
> det(D)
[1] 6.661338e-16
> t(A)
[,1] [,2] [,3] [,4] [,5]
[1,] 1 3 5 7 9
[2,] 2 4 6 8 10
> rownames(D)<-c('one','two', 'three')
> colnames(D)<-c("First", "second",'third')
> D
First second third
one 1 2 3
two 4 5 6
three 7 8 9
> D^(-1)
First second third
one 1.0000000 0.500 0.3333333
two 0.2500000 0.200 0.1666667
three 0.1428571 0.125 0.1111111
> dim(D)
[1] 3 3
> class(D)
[1] "matrix"
>
>
> # inserting rows and columns
> A1<-rbind(A, c(22,23))
> A1
[,1] [,2]
[1,] 1 2
[2,] 3 4
[3,] 5 6
[4,] 7 8
[5,] 9 10
[6,] 22 23
> B1<-cbind(B,c(31,32,33,34,35))
> B1
[,1] [,2] [,3]
[1,] 11 16 31
[2,] 12 17 32
[3,] 13 18 33
[4,] 14 19 34
[5,] 15 20 35
>
> # inserting large files from excel or word or csv file
> # first save the excel file as .csv files
> # R<- read.csv(file.choose())

You might also like