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

R Arrays and Matrix Operations Guide

The document provides examples of creating and manipulating arrays in R, including one-dimensional and multi-dimensional arrays. It demonstrates how to access specific rows and columns, combine vectors of different lengths into arrays, and perform matrix operations such as addition. Additionally, it shows how to use the apply function to calculate the sum of rows across matrices.

Uploaded by

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

R Arrays and Matrix Operations Guide

The document provides examples of creating and manipulating arrays in R, including one-dimensional and multi-dimensional arrays. It demonstrates how to access specific rows and columns, combine vectors of different lengths into arrays, and perform matrix operations such as addition. Additionally, it shows how to use the apply function to calculate the sum of rows across matrices.

Uploaded by

mdmahadi899
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

# An array with one dimension with values ranging from 1 to 24

thisarray <- c(1:27)

thisarray

# An array with more than one dimension

multiarray <- array(thisarray, dim = c(3, 3, 3))

multiarray

thisarray <- c(1:24)

# Access all the items from the first row from matrix one

multiarray <- array(thisarray, dim = c(4, 3, 2))

multiarray[,1,1]

# Access all the items from the first column from matrix one

multiarray <- array(thisarray, dim = c(4, 3, 2))

multiarray[1,,1]

thisarray <- c(1:24)

multiarray <- array(thisarray, dim = c(4, 3, 2))

5 %in% multiarray
# Create two vectors of different lengths.

vector1 <- c(5,9,3)

vector2 <- c(10,11,12,13,14,15)

# Take these vectors as input to the array.

result <- array(c(vector1,vector2),dim = c(3,3,2))

result

# Create two vectors of different lengths.

vector1 <- c(5,9,3)

vector2 <- c(10,11,12,13,14,15)

[Link] <- c("COL1","COL2","COL3")

[Link] <- c("ROW1","ROW2","ROW3")

[Link] <- c("ROW1","ROW2","ROW3")

[Link] <- c("Matrix1","Matrix2")

# Take these vectors as input to the array.


result <- array(c(vector1,vector2),dim = c(3,3,2),dimnames =
list([Link],[Link],

[Link]))

result

Create two vectors of different lengths.

vector1 <- c(5,9,3)

vector2 <- c(10,11,12,13,14,15)

# Take these vectors as input to the array.

array1 <- array(c(vector1,vector2),dim = c(3,3,2))

# Create two vectors of different lengths.

vector3 <- c(9,1,0)

vector4 <- c(6,0,11,3,14,1,2,6,9)

array2 <- array(c(vector1,vector2),dim = c(3,3,2))

# create matrices from these arrays.

matrix1 <- array1[,,2]

matrix2 <- array2[,,2]


# Add the matrices.

result <- matrix1+matrix2

result

# Create two vectors of different lengths.

vector1 <- c(5,9,3)

vector2 <- c(10,11,12,13,14,15)

# Take these vectors as input to the array.

[Link] <- array(c(vector1,vector2),dim = c(3,3,2))

([Link])

Fee Faysal Ahmed, Dept. Of Mathematics9:55 PM

# Use apply to calculate the sum of the rows across all the
matrices.

result <- apply([Link], 1, sum)

(result)

You might also like