#Mathematical operators
##Session 01

##Addition
1+2
## [1] 3
##Subtraction
9-6
## [1] 3
##Multiplication
5*4
## [1] 20
##Division
10/2
## [1] 5
##Exponentiation
(5^2)
## [1] 25
5**2
## [1] 25
##Modulo: Returns the reminder after division
4%%2
## [1] 0
5%%2
## [1] 1
7%%5
## [1] 2
#Assignment of variables <-

x<-33 #34 is the age of the respondent

##Print the variable x
x
## [1] 33
print(x) ##Prints the variable x
## [1] 33
##Adding assigned variables
y<- -6

x+y
## [1] 27
z<-x+y

z
## [1] 27
##Types of variables

##Numeric/float : 4.5
##Integer 4
##Logical: Boolean: TRUE. FALSE
##Characters: Test or strings: NSU43
c<- '5.8'
class(c)
## [1] "character"
##Check class of variables
x<-FALSE
class(x)
## [1] "logical"
z<-3.4
class(z)
## [1] "numeric"
a<-'NSU456'
class(a)
## [1] "character"
b<-TRUE
class(b)
## [1] "logical"
##########

##Convert class of variable
x=5.67
x<-as.integer(x)
y<-as.integer(y)
x<-as.numeric(x)
class(y)
## [1] "integer"
a<-43
a<-as.character(a)
class(a)
## [1] "character"
a<-'bangla'
a<-as.numeric(a)
## Warning: NAs introduced by coercion
e<-'English'
class(e)
## [1] "character"
e<-as.integer(e)
## Warning: NAs introduced by coercion
f<-TRUE
class(f)
## [1] "logical"
f<-as.integer(f)
f<-as.numeric(f)

a<-FALSE
class(a)
## [1] "logical"
a<-as.numeric(a)

a<-as.character(a)
class(a)
## [1] "character"
d<-4.32
class(d)
## [1] "numeric"
d<-as.character(d)

x<-4.5678
class(x)
## [1] "numeric"
x<-as.integer(x)

x<-as.numeric(x)
x
## [1] 4
#Create a vector

##Numeric vector
x<-5.6
class(x)
## [1] "numeric"
y<-c(3,4,6,7,8)
y
## [1] 3 4 6 7 8
y[5]
## [1] 8
w<-c(4.8,3.2,4.5)
t<-c(2,4,6,8,10,12)
s<-c(3,4,5,6,7,8)
z<-c(3.4, 7.8, 6.7)
z
## [1] 3.4 7.8 6.7
1:10
##  [1]  1  2  3  4  5  6  7  8  9 10
n<-(c(1:5))
n
## [1] 1 2 3 4 5
n<- c(2,3,7,8,9)
n1<-c(6,7,4,5,2)
n/n1
## [1] 0.3333333 0.4285714 1.7500000 1.6000000 4.5000000
m<-n+n1
m
## [1]  8 10 11 13 11
##Character vector
c<-c('d', 'g', 'k')

c<-c('d,g,k')
c
## [1] "d,g,k"
d<-c('d', 'p', 'w', 'l')

s<-c(c,d)
s
## [1] "d,g,k" "d"     "p"     "w"     "l"
##Boolean vector
b<-c(TRUE, FALSE, TRUE)

b
## [1]  TRUE FALSE  TRUE
d<-c(4, 5, 6)
z<-c(d,b)
z
## [1] 4 5 6 1 0 1
g<-c("TON","FON")
h<-c(1,2,3,4)
j<-c(g,h)
j
## [1] "TON" "FON" "1"   "2"   "3"   "4"
##Assign names to vectors

names(m)<-c("Sun", "Mon", "Tue", "Wed","Thu")
m
## Sun Mon Tue Wed Thu 
##   8  10  11  13  11
days<-c("Sun", "Mon", "Tue", "Wed")
#d<-c(7,8,9,4,5)
names(m)<-days
m
##  Sun  Mon  Tue  Wed <NA> 
##    8   10   11   13   11
days
## [1] "Sun" "Mon" "Tue" "Wed"
names(h)<-days
h
## Sun Mon Tue Wed 
##   1   2   3   4
##Check first few entries
head(m)
##  Sun  Mon  Tue  Wed <NA> 
##    8   10   11   13   11
tail(m)
##  Sun  Mon  Tue  Wed <NA> 
##    8   10   11   13   11
##Using SUM with vectors
total<-sum(n)+sum(n1)
total
## [1] 53
n<-c(4,5,6,8)
n+n1
## Warning in n + n1: longer object length is not a multiple of shorter object
## length
## [1] 10 12 10 13  6
total<-sum(n)+sum(n1)
total
## [1] 47
##Selection within vectors, use []
##Use c when selecting more than 1

n1[4]
## [1] 5
n1[c(2,4,5)]
## [1] 7 5 2
n1[3:5]
## [1] 4 5 2
m
##  Sun  Mon  Tue  Wed <NA> 
##    8   10   11   13   11
m[c("Mon", "Tue")]
## Mon Tue 
##  10  11
m[c("Sun")]
## Sun 
##   8
m[c("Tue","Thu")]
##  Tue <NA> 
##   11   NA
m[c("8","11")]
## <NA> <NA> 
##   NA   NA
#Calculate basic descriptive statistics
summary(m)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     8.0    10.0    11.0    10.6    11.0    13.0
mean(m)
## [1] 10.6
median(m)
## [1] 11
var(m)
## [1] 3.3
sd(m)
## [1] 1.81659
min(m)
## [1] 8
max(m)
## [1] 13
#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

x>=2
## [1] TRUE
z>=y
## Warning in z >= y: longer object length is not a multiple of shorter object
## length
## [1]  TRUE  TRUE  TRUE FALSE FALSE FALSE
z<-3.4

class(z)
## [1] "numeric"
c<-'nsu42'
c>=z
## [1] TRUE
##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
## Warning in n1 > n: longer object length is not a multiple of shorter object
## length
## [1]  TRUE  TRUE FALSE FALSE FALSE
##Each item is compared according to position

n<-c[1:6]
n1>n
## Warning in n1 > n: longer object length is not a multiple of shorter object
## length
## [1] FALSE    NA    NA    NA    NA    NA
##Test using logical operator
o<-n1>2
o
## [1]  TRUE  TRUE  TRUE  TRUE FALSE
#Matrices
##Byrow determines if the data will be filled by rows or columns


matrix(1:9, byrow=FALSE, nrow=3)
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9
##Combining vectors to form a matrix
a<-c(1,2)
b<-c(3,4)
c<-c(5,6)
d<-c(a,b,c)
d
## [1] 1 2 3 4 5 6
mat<-matrix(d, byrow=TRUE, nrow=3)
mat
##      [,1] [,2]
## [1,]    1    2
## [2,]    3    4
## [3,]    5    6
mat<-matrix(c(a,b,c), byrow=FALSE, nrow=3)
mat
##      [,1] [,2]
## [1,]    1    4
## [2,]    2    5
## [3,]    3    6
##Naming a matrix
colnames(mat)<-c("US", "Non US")
rownames(mat)<-c(1, 2, 3)
mat
##   US Non US
## 1  1      4
## 2  2      5
## 3  3      6
##Calculate row and column totaland mean in matrix
row<-rowSums(mat)
col<-colSums(mat)
rowMeans(mat)
##   1   2   3 
## 2.5 3.5 4.5
colMeans(mat)
##     US Non US 
##      2      5
##Column bind and row bind functions
cbind(mat,row)
##   US Non US row
## 1  1      4   5
## 2  2      5   7
## 3  3      6   9
rbind(mat,col)
##     US Non US
## 1    1      4
## 2    2      5
## 3    3      6
## col  6     15
mat 
##   US Non US
## 1  1      4
## 2  2      5
## 3  3      6
mat1<-matrix(9:14, byrow=FALSE, nrow=3)
mat2<-rbind(mat, mat1)
rownames(mat2)<-c(1,2,3,4,5,6)
mat2
##   US Non US
## 1  1      4
## 2  2      5
## 3  3      6
## 4  9     12
## 5 10     13
## 6 11     14
##Basic charts in R

plot(m, xlab="Numbers", ylab="Frequency", main="Chart")

##This will create a scatterplot

barplot(m, xlab="No", ylab="Frequency", main="Chart")

##This creates a barchart

##Plot function arguments
##"p" - points
##"l" - lines
##"b" - both points and lines
##"c" - empty points joined by lines
##"o" - overplotted points and lines
##"s" and "S" - stair steps
##"h" - histogram-like vertical lines
##"n" - does not produce any points or lines

x <- seq(-pi,pi,0.1)
plot(x, sin(x),
     main="The Sine Function",
     ylab="sin(x)",
     type="h",
     col="darkgreen")

##List of colors in R: http://www.stat.columbia.edu/~tzheng/files/Rcolor.pdf