*****Mathematical operators*****

##Addition 1+2

##Subtraction 5-1

##Multiplication 5*4

##Division 10/2

##Exponentiation 5^2

##Modulo: Returns the reminder after division 4%%2 5%%2

#Assignment of variables

x<-34

##Print the variable x x

##Adding assigned variables y<-6 x+y z<-x+y z

##Types of variables ##Numeric: 4.5 ##Integer 4 ##Logical: Boolean: TRUE. FALSE ##Characters: Test or strings: NSU43

##Check class of variables class(x) z<-3.4 class(z) a<-“NSU” class(a) b<-TRUE class(b)

##Convert class of variable x<-as.integer(x) class(x)

a<-as.character(a) class(a)

#Create a vector

##Numeric vector n<-as.integer(c(1:5)) n1<- c(2,3,7,8,9) n+n1 m<-n+n1 m

##Character vector c<-c(“d”, “g”, “k”)

##Boolean vector b<-c(TRUE, FALSE, TRUE)

##Assign names to vectors names(m)<-c(“Sun”, “Mon”, “Tue”, “Wed”, “Thu”) m

days<-c(“Sat”, “Mon”, “Tue”, “Wed”, “Thu”) names(m)<-days m ##Check first few entries head(m) tail(m)

##Using SUM with vectors total<-sum(n)+sum(n1)

##Selection within vectors, use [] ##Use c when selecting more than 1 n1[4] n1[c(2,4,5)] n1[3:5] m[c(“Mon”, “Tue”)]

#Calculate basic descriptive statistics summary(m) mean(m) median(m) var(m) sd(m) min(m) max(m)

#Comparison operators ##The (logical) comparison operators known to R are: ##< for less than ##> for greater than ##<= for less than or equal to ##>= for greater than or equal to ##== for equal to each other ##!= not equal to each other

z>y c<z

##Comparing numeric to character returns FALSE ##If the two arguments are atomic vectors of different types, one is coerced to the type of the other, the (decreasing) order of precedence being character, complex, numeric, integer, logical and raw.

n1>n ##Each item is compared according to position

n<-c[1:6] n1>n

##Test using logical operator o<-n>2 o

#Matrices ##Byrow determines if the data will be filled by rows or columns

matrix(1:9, byrow=FALSE, nrow=3)

##Combining vectors to form a matrix a<-c(1,2) b<-c(3,4) c<-c(5,6) d<-c(a,b,c) d mat<-matrix(d, byrow=TRUE, nrow=3) mat

mat<-matrix(c(a,b,c), byrow=FALSE, nrow=3) mat

##Naming a matrix colnames(mat)<-c(“US”, “Non US”) rownames(mat)<-c(1, 2, 3) mat

##Calculate row and column totaland mean in matrix row<-rowSums(mat) col<-colSums(mat) rowMeans(mat) colMeans(mat)

##Column bind and row bind functions cbind(mat,row) rbind(mat,col)

mat1<-matrix(9:14, byrow=FALSE, nrow=3) mat2<-rbind(mat, mat1) rownames(mat2)<-c(1,2,3,4,5,6) mat2

##Basic charts in R

plot(m, xlab=“Numbers”, ylab=“Frequency”, main=“Chart”) ##This will create a scatterplot