Expt. No. Basic Math Date
Expt. No. Basic Math Date
…………
BASIC MATH
Date:……………………
# BASIC MATH
2-5
6/3
3+2*5
(3+2)*5
4^3
exp(6)
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
x<-5
y<-3
class(x)
length(x)
x+y
x-y
x^y
sqrt(x)
exp(y)
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
class(u1)
length(u1)
s<-scan()
10
20
30
40
50
s
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')
ct
First,semester,mca,students
ct1
sf
sf1<-seq(10,100,10)
sf1
sf2<-10:20
sf2
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]
# 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
colnames(df)
rownames(df)<-scan(what = 'character')
one
two
three
four
five
six
seven
eight
nine
ten
rownames(df)
df
df1
w<-51:60
df2<-data.frame(x,y,z,w) # insert additional column
df2
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
B<-matrix(c(11,12,13,14,15,16,17,18,19,20), nrow = 5,
byrow = F)
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)
A1
B1<-cbind(B,c(31,32,33,34,35))
B1
# 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())