0% found this document useful (0 votes)
2 views3 pages

Practical 2

The document provides practical examples of creating, manipulating, and performing operations on matrices and arrays in R. It covers matrix creation, row and column binding, naming conventions, and various arithmetic operations including scalar and matrix operations. Additionally, it demonstrates how to access specific elements and subsets within matrices.

Uploaded by

amisha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Practical 2

The document provides practical examples of creating, manipulating, and performing operations on matrices and arrays in R. It covers matrix creation, row and column binding, naming conventions, and various arithmetic operations including scalar and matrix operations. Additionally, it demonstrates how to access specific elements and subsets within matrices.

Uploaded by

amisha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

PRACTICAL 2 Amisha

22501104
MATRICES AND ARRAYS
Creation
> matrix(1:6, nrow = 2)
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
> matrix(1:6, nrow=2, byrow=TRUE)
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
> matrix(1:3, nrow=2, ncol=3)
[,1] [,2] [,3]
[1,] 1 3 2
[2,] 2 1 3

Row Bind
> rbind(1:3, 2:4)
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 2 3 4
> cbind(1:3, 5:7)
[,1] [,2]
[1,] 1 5
[2,] 2 6
[3,] 3 7
> mat<-matrix(1:12, nrow = 4)
> rbind(mat, 3:5)
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
[5,] 3 4 5
> cbind(mat, 1:4)
[,1] [,2] [,3] [,4]
[1,] 1 5 9 1
[2,] 2 6 10 2
[3,] 3 7 11 3
[4,] 4 8 12 4

Naming
> rownames(mat)<-c("r1","r2","r3","r4")
> mat
[,1] [,2] [,3]
r1 1 5 9
r2 2 6 10
r3 3 7 11
r4 4 8 12
> colnames(mat)<-c("c1","c2","c3")
> mat1<-matrix(1:10, nrow=2, byrow=T, dimnames=list(c("c1","c2"), c("c1","c2","c3","c4
","c5")))
> mat1
c1 c2 c3 c4 c5
c1 1 2 3 4 5
c2 6 7 8 9 10
> num<-matrix(1:8, ncol=2)
> char<-matrix(LETTERS[1:6], nrow=4, ncol=3)
> num
[,1] [,2]
[1,] 1 5
[2,] 2 6
[3,] 3 7
[4,] 4 8
> char
[,1] [,2] [,3]
[1,] "A" "E" "C"
[2,] "B" "F" "D"
[3,] "C" "A" "E"
[4,] "D" "B" "F"
> cbind(num, char)
[,1] [,2] [,3] [,4] [,5]
[1,] "1" "5" "A" "E" "C"
[2,] "2" "6" "B" "F" "D"
[3,] "3" "7" "C" "A" "E"
[4,] "4" "8" "D" "B" "F"

Operations (Scalar and Matrix)


> mat2<-matrix(c(316,343,378,556,584,742), nrow=3, dimnames=list(c("FS","TT","RK"), c(
"US","Non-US")))
> mat2
US Non-US
FS 316 556
TT 343 584
RK 378 742
> mat2+50
US Non-US
FS 366 606
TT 393 634
RK 428 792
> mat2-100
US Non-US
FS 216 456
TT 243 484
RK 278 642
> 5*mat2
US Non-US
FS 1580 2780
TT 1715 2920
RK 1890 3710
> mat/1.5
c1 c2 c3
r1 0.6666667 3.333333 6.000000
r2 1.3333333 4.000000 6.666667
r3 2.0000000 4.666667 7.333333
r4 2.6666667 5.333333 8.000000

Operations (Matrix and Matrix)


> mat3<-matrix(1:6, nrow=3)
> mat2+mat3
US Non-US
FS 317 560
TT 345 589
RK 381 748
> mat2-mat3
US Non-US
FS 315 552
TT 341 579
RK 375 736
> mat2*mat3
US Non-US
FS 316 2224
TT 686 2920
RK 1134 4452
> mat2/mat3
US Non-US
FS 316.0 139.0000
TT 171.5 116.8000
RK 126.0 123.6667
> mat4<-matrix(1:6, nrow=2)
> mat2%*%mat4
[,1] [,2] [,3]
FS 1428 3172 4916
TT 1511 3365 5219
RK 1862 4102 6342

Random Matrix
> mat5<-matrix(sample(1:19, 12), nrow=3)
> mat5
[,1] [,2] [,3] [,4]
[1,] 1 8 18 10
[2,] 2 11 3 5
[3,] 17 15 16 12
> mat5[1,4]
[1] 10
> mat5[2,3]
[1] 3
> mat5[8]
[1] 3
> mat5[3]
[1] 17
> mat5[2, 3:4]
[1] 3 5
> mat5[2, c(2,4)]
[1] 11 5
> mat5[1:2, 2:1]
[,1] [,2]
[1,] 8 1
[2,] 11 2
> mat5[1:2, 1:2]
[,1] [,2]
[1,] 1 8
[2,] 2 11
> mat5[c(1,3),c(1,4)]
[,1] [,2]
[1,] 1 10
[2,] 17 12

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy