Search This Blog

Tutorial # 4. Matrix

Matrix is a two dimensional datasets created by using "vector" and defined with its number of rows & columns

nrow => number of rows
ncol => number of Column
byrow = TRUE => arranging row-wise
byrow => FALSE => arranging column-wise
dimnames => Dimension Name
 
my_matrix1 = matrix(c('1','2','3','4','5','6','7','8','9'),
nrow=3, 
ncol =3,
byrow = TRUE)
 
print(my_matrix1)

rows = c('R1' , 'R2' , 'R3' )
cols = c('C1' , 'C2' , 'C3' )

my_matrix2 = matrix(c('1','2','3','4','5','6','7','8','9'),
nrow=3, 
ncol =3
byrow = TRUE
dimnames = list(rows , cols))
 
print((my_matrix2))

# Making a senquencial number matrix
seq_mat = matrix(1:9, nrow = 3, ncol = 3, byrow = FALSE, dimnames = list(rows,cols))
print(seq_mat)

# Accessing the elements of a matrix => matrix_name[ row , col ]
print(seq_mat[1,1])
print(seq_mat[ ,1])
print(seq_mat[1, ])
print(seq_mat['R1', ])
print(seq_mat[ , 'C1'])
 

You may also interested in:

An Introduction to language R
Tutorial # 1.Data Type & Variable  Declaration
Tutorial # 2.Operators
Tutorial # 3.Vector  & Element Access
Tutorial # 4.Matrix
Tutorial # 5.Data Frame
Tutorial # 6.Arrays
Tutorial # 7.Lists
Tutorial # 8.Factors
Tutorial # 9.Data import
Tutorial # 10.Machine Learning
Tutorial # 11.Visualization


Post a Comment

0 Comments