# simple calculation
6*8
## [1] 48
1+(3*2)/log(250)*pi
## [1] 4.413871
# x is not defined, so error comes
# x + 3
# x is defined as 5
x = 5
x + 3
## [1] 8
# redefine the value of x
x = 10
x + 3
## [1] 13
# R will replace the value of x without any notification
# R is a case (capital/small) sensitive but not space sensitive
X = 8 # scalar/ vector with one value
X == x
## [1] FALSE
X*x
## [1] 80
X + x
## [1] 18
# y is a vector or variable or object
y = c(2, 4, 3, 8, 8, 9, 9, 9 , 9, 10, 13, 20, 16, 11, 45)
# if call y: type y and hit ctrl+enter (run)
y
## [1] 2 4 3 8 8 9 9 9 9 10 13 20 16 11 45
# add 2 to each of the elements of y
y + 2
## [1] 4 6 5 10 10 11 11 11 11 12 15 22 18 13 47
# descriptive statistics of y
summary(y)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 2.00 8.00 9.00 11.73 12.00 45.00
sort(y)
## [1] 2 3 4 8 8 9 9 9 9 10 11 13 16 20 45
sort(y, decreasing = TRUE)
## [1] 45 20 16 13 11 10 9 9 9 9 8 8 4 3 2
length(y)
## [1] 15
mean(y)
## [1] 11.73333
hist(y)

boxplot(y, col = 'red', main = 'Fig. Boxplot')

plot(density(y))

sd(y)
## [1] 10.3058
median(y)
## [1] 9
mode(y)
## [1] "numeric"
names(sort(-table(y)))[1]
## [1] "9"
# '=' or '<-' assignment operator
# +, -, *, / mathematical operator
# ==, <, >, <=, >= logical operator
a = 3
b = 5
a > b
## [1] FALSE
a < b
## [1] TRUE
5.12 > 5.2
## [1] FALSE
5.12 < 5.2
## [1] TRUE
# Dividend = Divisor*Quotient + Remainder
17%%4 # remainder
## [1] 1
17%/%4 # quotient
## [1] 4
# convert 75 minutes to hours and minutes
# paste0(), can combine text and commands
75%/%60
## [1] 1
75%%60
## [1] 15
paste0('75 minutes = ', 75%/%6, ' hour and ', 75%/%60, ' minute.' )
## [1] "75 minutes = 12 hour and 1 minute."
# Set working directory in the folder containg this script
# Session > Set Working Directory > To Source File Location
# list can contain different types of vectors
vector1 = c('Rayhan', 'Sumi', 'Ratul', 'Joba', 'Mridul')
print(vector1)
## [1] "Rayhan" "Sumi" "Ratul" "Joba" "Mridul"
vector1
## [1] "Rayhan" "Sumi" "Ratul" "Joba" "Mridul"
# Create vector2 with a sequence of number from 1 to 5
vector2 = c(1:5)
vector2
## [1] 1 2 3 4 5
names = data.frame(vector2, vector1)
names
## vector2 vector1
## 1 1 Rayhan
## 2 2 Sumi
## 3 3 Ratul
## 4 4 Joba
## 5 5 Mridul
# check structure
str(names)
## 'data.frame': 5 obs. of 2 variables:
## $ vector2: int 1 2 3 4 5
## $ vector1: chr "Rayhan" "Sumi" "Ratul" "Joba" ...
# number of rows, column, dimension
nrow(names)
## [1] 5
ncol(names)
## [1] 2
dim(names)
## [1] 5 2
# Names of the variables
names(names)
## [1] "vector2" "vector1"
# see vector1 column (variable)
names$vector2
## [1] 1 2 3 4 5
# save the dataset
write.csv(names, 'name.csv')
# read the dataset
read.csv('name.csv')
## X vector2 vector1
## 1 1 1 Rayhan
## 2 2 2 Sumi
## 3 3 3 Ratul
## 4 4 4 Joba
## 5 5 5 Mridul
vector3 = seq(1, 5, by = 1)
vector3
## [1] 1 2 3 4 5
# sequenc of even numbers from 2:20
vector4 = seq(2, 20, by = 2)
vector4
## [1] 2 4 6 8 10 12 14 16 18 20
# create a sequency of odd numbers from 1 to 12
vector5 = seq(1, 12, by = 2)
vector5
## [1] 1 3 5 7 9 11
# data.frame cannot contain variables with different number of rows
# However, list can contain such differing variables
different = list(vector3, vector4, vector5)
different
## [[1]]
## [1] 1 2 3 4 5
##
## [[2]]
## [1] 2 4 6 8 10 12 14 16 18 20
##
## [[3]]
## [1] 1 3 5 7 9 11
str(different)
## List of 3
## $ : num [1:5] 1 2 3 4 5
## $ : num [1:10] 2 4 6 8 10 12 14 16 18 20
## $ : num [1:6] 1 3 5 7 9 11
# selecting the component of list
# [] for indexing
different[1]
## [[1]]
## [1] 1 2 3 4 5
different[2]
## [[1]]
## [1] 2 4 6 8 10 12 14 16 18 20
different[3]
## [[1]]
## [1] 1 3 5 7 9 11