R For Machine Learning Lab Practical Work: Master of Business Administration in Business Analytics
R For Machine Learning Lab Practical Work: Master of Business Administration in Business Analytics
Submitted by,
Gishnu Raj
2K18/MBA/909
> #creating a vector
> x1<-c(1,2,3,4,5,6,7,8,9)
> x1
[1] 1 2 3 4 5 6 7 8 9
> #operations on vector
> #multiplication
> x1<-c(1,2,3,4,5,6,7,8,9)
> y1<- x1*3
> y1
[1] 3 6 9 12 15 18 21 24 27
>
> #addition
> x1<-c(1,2,3,4,5,6,7,8,9)
> y2<-x1+c(1,2,3,4,5,6,7,8,9)
> y2
[1] 2 4 6 8 10 12 14 16 18
>
> #explicit coersion
> x2<-0:6
> x2
[1] 0 1 2 3 4 5 6
> class(x2)
[1] "integer"
> as.numeric(x2)
[1] 0 1 2 3 4 5 6
> as.logical(x2)
[1] FALSE TRUE TRUE TRUE TRUE TRUE TRUE
> as.character(x2)
[1] "0" "1" "2" "3" "4" "5" "6"
> as.complex(x2)
[1] 0+0i 1+0i 2+0i 3+0i 4+0i 5+0i 6+0i
>
> #non sensical coersion
> x3<- c('a','b','c')
> as.numeric(x3)
[1] NA NA NA
Warning message:
NAs introduced by coercion
>
> #other operation
> x1<-c(1,2,3,4,5,6,7,8,9)
> #gives mean
> mean(x1)
[1] 5
> #gives the max value
> max(x1)
[1] 9
> #gives the min value
> min(x1)
[1] 1
> #gives the product of values
> prod(x1)
[1] 362880
> #gives the lenght of the vector
> length(x1)
[1] 9
> #gives the square root of each values.
> sqrt(x1)
[1] 1.000000 1.414214 1.732051 2.000000 2.236068 2.449490 2.645751
[8] 2.828427 3.000000
>
> #create a vector fo 10 elements and name them
> x4<-1:10
> names(x4)<-LETTERS[3:12]
> x4
C D E F G H I J K L
1 2 3 4 5 6 7 8 9 10
>
> #read a character vector from the user
> x5<- scan(what="",sep='\n')
1: x5
2:
> #sample
> sample(1:10,5,replace=F)
[1] 1 9 8 7 2
>
> #print MBA/21
> paste(("MBA"),01:09,sep='-')
[1] "MBA-1" "MBA-2" "MBA-3" "MBA-4" "MBA-5" "MBA-6" "MBA-7" "MBA-8"
[9] "MBA-9"
>
> #making matrix
> m<- matrix(1:6,nrow=2,ncol=3)
> m
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
> #dimension of matrix
> dim(m)
[1] 2 3
> #attributes of matrix
> attributes(m)
$dim
[1] 2 3
>
> #creating a matrix from vector by adding dimension attribute
> m1<-1:10
> m1
[1] 1 2 3 4 5 6 7 8 9 10
> dim(m1)<-c(2,5)
> m
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
>
> #making matrix by coloum binding and row binding
> #1. usind cbind()
> x<-1:3
> y<-10:12
> cbind(x,y)
x y
[1,] 1 10
[2,] 2 11
[3,] 3 12
> #2. using rbind()
> rbind(x,y)
[,1] [,2] [,3]
x 1 2 3
y 10 11 12
>
> #list
> l1<- list(1,'a',TRUE,1+4i)
> l1
[[1]]
[1] 1
[[2]]
[1] "a"
[[3]]
[1] TRUE
[[4]]
[1] 1+4i
> x6[[1]]
[1] 1 2 3 4
> x6$ww
[1] 0.6
> x6[["ww"]]
[1] 0.6
>
> #for getting multiple elememts.
> x7<-list(qq=1:4,ww=0.6, zz="highfive")
> x7[c(1:3)]
$qq
[1] 1 2 3 4
$ww
[1] 0.6
$zz
[1] "highfive"
>
> #subsetting matrix
> tom<-matrix(1:6, 2, 3)
> tom[1,2]
[1] 3
> tom[2,3]
[1] 6
>
> #indexing can also be missing
> tom[,2]
[1] 3 4
> #for getting back as vector
> jerry<-matrix(1:6,2,3)
> jerry[1,2,drop=F]
[,1]
[1,] 3
>
> #vectorised operation.
> x<-1:4
> y<-6:9
> x+y
[1] 7 9 11 13
> x>2
[1] FALSE FALSE TRUE TRUE
> y==8
[1] FALSE FALSE TRUE FALSE
> x*y
[1] 6 14 24 36
> x/y
[1] 0.1666667 0.2857143 0.3750000 0.4444444
>
> #vectorised matrix operation
> x<-matrix(1:4,2,2)
> y<-matrix(rep(10,4),2,2)
> yelement wise multiplication
Error: unexpected symbol in "yelement wise"
> x*y
[,1] [,2]
[1,] 10 30
[2,] 20 40
> x/y
[,1] [,2]
[1,] 0.1 0.3
[2,] 0.2 0.4
> #true matrix multiplication
> x%*%y
[,1] [,2]
[1,] 40 40
[2,] 60 60
>
> #dataframe.
> name<-c('a','b','c','d','e')
> age<-c(22,23,24,25,26)
> gender<-c('m','f','m','f','f')
> x<-data.frame(name, age, gender)
> x
name age gender
1 a 22 m
2 b 23 f
3 c 24 m
4 d 25 f
5 e 26 f
> head(x,n=2)
name age gender
1 a 22 m
2 b 23 f
> tail(x)
name age gender
1 a 22 m
2 b 23 f
3 c 24 m
4 d 25 f
5 e 26 f
> nrow(x)
[1] 5
>
> #creating new coloum data frame.
> x$city<-c("goa","banglore","idukki","kochi","delhi")
> x
name age gender city
1 a 22 m goa
2 b 23 f banglore
3 c 24 m idukki
4 d 25 f kochi
5 e 26 f delhi
> #summary
> summary(x)
name age gender city
a:1 Min. :22 f:3 Length:5
b:1 1st Qu.:23 m:2 Class :character
c:1 Median :24 Mode :character
d:1 Mean :24
e:1 3rd Qu.:25
Max. :26
> #attach
> attach(x)
The following objects are masked _by_ .GlobalEnv:
age, gender, name
>
> #subsetting
> sub<-subset(x,gender=='m')
> sub
name age gender city
1 a 22 m goa
3 c 24 m idukki
>
> #control structures.
> #if statement.
> x<-5
> if(x>3){
+ print("hellaluya")
+ }
[1] "hellaluya"
> #for loop
> x<-c(1,2,3,4,5,6,7,8,9)
> for(i in 1:4){
+ print(x[i])
+
+ }
[1] 1
[1] 2
[1] 3
[1] 4
>
> #nested for loop
> for(i in 1:5)
+ {
+ for(j in 1:2)
+ {
+ print(i*j);
+ }
+ }
[1] 1
[1] 2
[1] 2
[1] 4
[1] 3
[1] 6
[1] 4
[1] 8
[1] 5
[1] 10
>
> #while loop
> count<-0
> while(count<10)
+ {
+ print('count')
+ count<-count+1
+ }
[1] "count"
[1] "count"
[1] "count"
[1] "count"
[1] "count"
[1] "count"
[1] "count"
[1] "count"
[1] "count"
[1] "count"
>
> #control structures repeat next break
> x <- 1
> repeat {
+ print(x)
+ x = x+1
+ if (x == 6){
+ break
+ }
+ }
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
>
> #Employee Database
>
> emp_id<- c(121,122,123,124,125,126,127,128,129,130)
> emp_name<- c('a','b','b','d','e','f','g','h','i','j')
> gender<-c('m','m','f','f','f','m','f','m','m','f')
> age<-c(24,28,27,23,24,28,26,27,28,30)
> location<-c('d','k','m','b','d','d','m','k','b','d')
> designation<-c('clerk','manager','analyst','clerk','clerk','analyst','sup
erviser','analyst','clerk','analyst')
> salary<-c(10000,30000,20000,10000,10000,20000,25000,20000,10000,20000)
>
> empdb<-data.frame(emp_id,emp_name,gender,age,location,designation,salary)
> empdb
emp_id emp_name gender age location designation salary
1 121 a m 24 d clerk 10000
2 122 b m 28 k manager 30000
3 123 b f 27 m analyst 20000
4 124 d f 23 b clerk 10000
5 125 e f 24 d clerk 10000
6 126 f m 28 d analyst 20000
7 127 g f 26 m superviser 25000
8 128 h m 27 k analyst 20000
9 129 i m 28 b clerk 10000
10 130 j f 30 d analyst 20000
> sub<- subset(empdb,location=='d')
> sub
emp_id emp_name gender age location designation salary
1 121 a m 24 d clerk 10000
5 125 e f 24 d clerk 10000
6 126 f m 28 d analyst 20000
10 130 j f 30 d analyst 20000
>