Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (1 vote)
58 views

R For Machine Learning Lab Practical Work: Master of Business Administration in Business Analytics

This document contains code examples demonstrating basic operations in R for machine learning. It shows how to create vectors, matrices, data frames, and perform operations like subsetting, coercion and control structures. It also includes an example employee database with sample data to demonstrate working with data frames. The key concepts covered are vectors, matrices, data frames, subsetting, coercion, control structures and an example database.

Uploaded by

Gishnu Raj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
58 views

R For Machine Learning Lab Practical Work: Master of Business Administration in Business Analytics

This document contains code examples demonstrating basic operations in R for machine learning. It shows how to create vectors, matrices, data frames, and perform operations like subsetting, coercion and control structures. It also includes an example employee database with sample data to demonstrate working with data frames. The key concepts covered are vectors, matrices, data frames, subsetting, coercion, control structures and an example database.

Uploaded by

Gishnu Raj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

R for Machine Learning

Lab practical work


Master of Business Administration in Business Analytics

University School of Management and Entrepreneurship,


Delhi Technological University

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

> #elements of list have double brackets around them.


>
> #factors
> x<-factor(c("yes","yes","no","yes","no"))
> x
[1] yes yes no yes no
Levels: no yes
> #table() gives the no of yes and no
> table(x)
x
no yes
2 3
> #stripping out the class of vector
> unclass(x)
[1] 2 2 1 2 1
attr(,"levels")
[1] "no" "yes"
>
> #order of level can be set explicitly stated
> x<-factor(c("yes","yes","no","yes","no"),levels=c("yes","no"))
> x
[1] yes yes no yes no
Levels: yes no
>
> #missing values
> mv <- c(1,2,NA,10,15)
> #is.na()is used to check if there is NA values
> is.na(mv)
[1] FALSE FALSE TRUE FALSE FALSE
> #is.NaN for NaN values, they have no class
> is.nan(mv)
[1] FALSE FALSE FALSE FALSE FALSE
> mv1<-c(1,2,NaN,NA,4)
> is.na(mv1)
[1] FALSE FALSE TRUE TRUE FALSE
> is.nan(mv1)
[1] FALSE FALSE TRUE FALSE FALSE
> #a NaN value is also NA but the converse is not true.
>
> #dataframes
> x<-data.frame(xx=1:4,yy=c(T,T,F,F))
> x
xx yy
1 1 TRUE
2 2 TRUE
3 3 FALSE
4 4 FALSE
> #for getting the number of rows and coloumns.
> nrow(x)
[1] 4
> ncol(x)
[1] 2
>
> #datatypes names.
> x<-1:3
> names(x)<-c("bulbasor" , "charmander" , "squirtel")
> x
bulbasor charmander squirtel
1 2 3
> names(x)
[1] "bulbasor" "charmander" "squirtel"
>
> #list can also have names.
> x<- list(xx=1,yy=2,zz=3)
> x
$xx
[1] 1
$yy
[1] 2
$zz
[1] 3
>
> #matrices can also have names.
> m<-matrix(1:4,2,2)
> dimnames(m)<-list(c('fast','furious'),c('slow','motion'))
> m
slow motion
fast 1 3
furious 2 4
>
> #subsetting basics
> x<-c('a','b','c','d','e')
> x[1]
[1] "a"
> x[1:4]
[1] "a" "b" "c" "d"
> x[x>"a"]
[1] "b" "c" "d" "e"
> y<-x>"a"
> y
[1] FALSE TRUE TRUE TRUE TRUE
> x[y]
[1] "b" "c" "d" "e"
>
> #subsetting list
> x6<-list(qq=1:4,ww=0.6)
> x6[1]
$qq
[1] 1 2 3 4

> 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

The following objects are masked from x (pos = 3):

age, city, gender, name


The following objects are masked from x (pos = 4):

age, city, 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

>

You might also like