Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Class notes

matrices in r programming

Rating
-
Sold
-
Pages
5
Uploaded on
29-05-2023
Written in
2022/2023

notes for explanation and understanding matrices with theory and examples

Institution
Course

Content preview

2/8/23, 11:01 AM R - Matrices




R - Matrices

Matrices are the R objects in which the elements are arranged in a two-dimensional rectangular
layout. They contain elements of the same atomic types. Though we can create a matrix containing
only characters or only logical values, they are not of much use. We use matrices containing
numeric elements to be used in mathematical calculations.

A Matrix is created using the matrix() function.

Syntax
The basic syntax for creating a matrix in R is −

matrix(data, nrow, ncol, byrow, dimnames)


Following is the description of the parameters used −

data is the input vector which becomes the data elements of the matrix.

nrow is the number of rows to be created.

ncol is the number of columns to be created.
byrow is a logical clue. If TRUE then the input vector elements are arranged by row.

dimname is the names assigned to the rows and columns.


Example
Create a matrix taking a vector of numbers as input.

 Live Demo
# Elements are arranged sequentially by row.
M <- matrix(c(3:14), nrow = 4, byrow = TRUE)
print(M)

# Elements are arranged sequentially by column.
N <- matrix(c(3:14), nrow = 4, byrow = FALSE)
print(N)

# Define the column and row names.
rownames = c("row1", "row2", "row3", "row4")

https://www.tutorialspoint.com/r/r_matrices.htm 1/5

, 2/8/23, 11:01 AM R - Matrices

colnames = c("col1", "col2", "col3")


P <- matrix(c(3:14), nrow = 4, byrow = TRUE, dimnames = list(rownames, colnames))
print(P)




When we execute the above code, it produces the following result −

[,1] [,2] [,3]
[1,] 3 4 5
[2,] 6 7 8
[3,] 9 10 11
[4,] 12 13 14
[,1] [,2] [,3]
[1,] 3 7 11
[2,] 4 8 12
[3,] 5 9 13
[4,] 6 10 14
col1 col2 col3
row1 3 4 5
row2 6 7 8
row3 9 10 11
row4 12 13 14



Accessing Elements of a Matrix
Elements of a matrix can be accessed by using the column and row index of the element. We
consider the matrix P above to find the specific elements below.

 Live Demo
# Define the column and row names.
rownames = c("row1", "row2", "row3", "row4")
colnames = c("col1", "col2", "col3")

# Create the matrix.
P <- matrix(c(3:14), nrow = 4, byrow = TRUE, dimnames = list(rownames, colnames))

# Access the element at 3rd column and 1st row.
print(P[1,3])

# Access the element at 2nd column and 4th row.
print(P[4,2])


# Access only the 2nd row.
https://www.tutorialspoint.com/r/r_matrices.htm 2/5

Written for

Institution
Course

Document information

Uploaded on
May 29, 2023
Number of pages
5
Written in
2022/2023
Type
Class notes
Professor(s)
Zahrir
Contains
All classes

Subjects

$4.79
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
nimairao

Also available in package deal

Get to know the seller

Seller avatar
nimairao Mithibai Institute
Follow You need to be logged in order to follow users or courses
Sold
1
Member since
2 year
Number of followers
1
Documents
34
Last sold
2 year ago
StudyMate

Welcome to StudyMate , your go-to destination for study materials in the fields of USMLE, CFA, data science, and computer science. We offer a carefully curated collection of books, guides, and references to support your academic journey. Whether you're preparing for the USMLE, we have a comprehensive selection of medical textbooks and practice question banks. For aspiring CFA professionals, our store offers finance study guides and exam resources. If you're diving into the world of data science, we have the latest books on machine learning, data analysis, and predictive modeling. And for computer science enthusiasts, we provide programming manuals and algorithm guides for various languages. Our knowledgeable staff is always available to assist you in finding the perfect resources. Visit The Scholar's Bookshop and empower your studies in an inspiring environment.

Read more Read less
0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions