Explore the various matrix operations using R.

927    Asked by GayatriJaiteley in Data Science , Asked on Jan 7, 2020
Answered by Nitin Solanki

A matrix is a representation of an array of numbers in n dimensional format that are again represented as rows and columns. From a matrix, we can perform any type of operations such as indexing, slicing, changing the data types, etc.

Let us create a vector for matrix operation

v <- 1:10

v

1 2 3 4 5 6 7 8 9 10

We can create a matrix() function to create a matrix

matrix(v)

1

2

3

4

5

6

7

8

9

10

Let us create a matrix by specifying the number of rows by using nrow function

matrix(v,nrow=2)

1

3

5

7

9

2

4

6

8

10

 Let us create a matrix from vectors

# not real prices

goog <- c(450,451,452,445,468)

msft <- c(230,231,232,236,228)

stocks <- c(goog,msft)

stock.matrix <- matrix(stocks,byrow=TRUE,nrow=2)

stock.matrix

450

451

452

445

468

230

231

232

236

228



Your Answer

Interviews

Parent Categories