# Create matrix with byrow optionmat_g<-matrix(c(1,2,3,4,5,6),nrow=2,ncol=3,byrow=FALSE)mat_g
A matrix: 2 × 3 of type dbl
1
3
5
2
4
6
# Create same matrix with byrow optionmat_f<-matrix(c(1,2,3,4,5,6),nrow=2,ncol=3,byrow=TRUE)mat_f
A matrix: 2 × 3 of type dbl
1
2
3
4
5
6
# Index matrix of first row and first columnmat_f[1,1]# Index matrix of second row and first columnmat_f[2,1]# Index matrix of second row and third columnmat_f[2,3]# Select full first rowmat_f[1,]# Select full first columnmat_f[,1]
# Scalar multiplication with matrixmat_1<-matrix(c(1,2,3,4,5,6),nrow=2,ncol=3)mat_new<-10*mat_1mat_1mat_new
A matrix: 2 × 3 of type dbl
1
3
5
2
4
6
A matrix: 2 × 3 of type dbl
10
30
50
20
40
60
# Broadcast vector of 1x3 to matrix of 2x3vec_1<-c(1,2,3)mat_1<-matrix(c(1,2,3,4,5,6),nrow=2,ncol=3)vec_1+mat_1
A matrix: 2 × 3 of type dbl
2
6
7
4
5
9
# Check for determinant of matrixmat_A<-matrix(c(1,2,3,4),nrow=2,ncol=2)mat_A# Here det != 0, hence inverse existdet_of_mat_A=det(mat_A)cat(det_of_mat_A,'is non-zero hence the inverse exist')
A matrix: 2 × 2 of type dbl
1
3
2
4
-2 is non-zero hence the inverse exist
# Invert of Matrix: mat'mat_A_inv=solve(mat_A)mat_A_inv
A matrix: 2 × 2 of type dbl
-2
1.5
1
-0.5
# I = A'A = AA'mat_A_identity=mat_A_inv%*%mat_Amat_A_identitymat_A_inv%*%mat_Amat_A%*%mat_A_inv
A matrix: 2 × 2 of type dbl
1
0
0
1
A matrix: 2 × 2 of type dbl
1
0
0
1
A matrix: 2 × 2 of type dbl
1
0
0
1
# IA = AI = Amat_Amat_A_identity%*%mat_Amat_A%*%mat_A_identity
# Be careful, you might end up in an infinite loop# if say i = -1i<-0while (i<3){stdout_str=paste('Yet to reach 3, i is: ',i)print(stdout_str)i<-i+1}print('Reached 3!')
[1] "Yet to reach 3, i is: 0"
[1] "Yet to reach 3, i is: 1"
[1] "Yet to reach 3, i is: 2"
[1] "Reached 3!"
# Square rootsqrt(4)# Alternative square root via power yiled same results4**0.5
2
2
# We can square root a complex numbersqrt(-25+0i)# But we cannot square root a real numbercat(sprintf('---------------'))cat(sprintf('This will fail!'))cat(sprintf('---------------'))sqrt(-25)
0+5i
---------------This will fail!---------------
Warning message in sqrt(-25):
“NaNs produced”
NaN
# Print as fractions rather than decimallibrary(MASS)fractions(1-0.25)
# Create listlist_a<-c(1,2,3,4,5,6)# Get standard deviationcat('standard deviation:',sd(list_a))# Get variancecat('\nvariance:',var(list_a))# Get meancat('\nmedian:',mean(list_a))# Get mediancat('\nmean:',median(list_a))# Get summary statisticssummary(list_a)
standard deviation: 1.870829
variance: 3.5
median: 3.5
mean: 3.5
Min. 1st Qu. Median Mean 3rd Qu. Max.
1.00 2.25 3.50 3.50 4.75 6.00
# t-testvar_test=t.test(list_a)var_test
One Sample t-test
data: list_a
t = 4.5826, df = 5, p-value = 0.005934
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
1.536686 5.463314
sample estimates:
mean of x
3.5
# This shows what you can call if we assign it to an objectnames(t.test(list_a))
'statistic'
'parameter'
'p.value'
'conf.int'
'estimate'
'null.value'
'stderr'
'alternative'
'method'
'data.name'
# Get confidence intervalvar_test$conf.int
1.53668569301968
5.46331430698032
# Mid-point of confidence interval is the meanmean(var_test$conf.int)
3.5
# Get p-valuevar_test$p.value
0.00593354451759226
# Plot histogram, hist() doesn't work all the timebarplot(table(list_a))
# Plot dot plotplot(list_a)
# Plot dot plot# type: p for point, l for line, b for both# col: cyan, blue, green, redplot(list_a,xlab='variable x',ylab='variable y',main='X-Y Plot',type='b',col='blue')