Ex. No. 6 R Programming Array: UR16BI009

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

17BI2027–LINUX AND R PROGRAMMING LAB UR16BI009

Ex. No. 6 R Programming Array Date of Exercise

AIM:
To implement various matrix, vectors, elements, rows and column to create an array.

DESCRIPTION:
The following command were used to implement various matrix, vectors, elements,
rows and column

OBESERVATION:

1. Write a R program to convert a given matrix to a 1-dimensional array.


m=matrix(1:12,3,4)
print("Original matrix:")
print(m)
a = as.vector(m)
print("1 dimensional array:")
print(a)

OUTPUT:

2. Write a R program to create an array of two 3x3 matrices each with 3 rows and 3 columns
from two given two vectors.
17BI2027–LINUX AND R PROGRAMMING LAB UR16BI009

print("Two vectors of different lengths:")


v1 = c(1,3,4,5)
v2 = c(10,11,12,13,14,15)
print(v1)
print(v2)
result = array(c(v1,v2),dim = c(3,3,2))
print("New array:")
print(result)

OUTPUT:

3. Write a R program to create an array of two 3x3 matrices each with 3 rows and 3 columns from
two given two vectors. Print the second row of the second matrix of the array and the element in
the 3rd row and 3rd column of the 1st matrix.

print("Two vectors of different lengths:")


v1 = c(1,3,4,5)
v2 = c(10,11,12,13,14,15)
print(v1)
print(v2)
result = array(c(v1,v2),dim = c(3,3,2))
print("New array:")
print(result)
print("The second row of the second matrix of the array:")
print(result[2,,2])
print("The element in the 3rd row and 3rd column of the 1st matrix:")
print(result[3,3,1])
17BI2027–LINUX AND R PROGRAMMING LAB UR16BI009

OUTPUTS:
17BI2027–LINUX AND R PROGRAMMING LAB UR16BI009

RESULT:
The following experiment is performed and output is verified

You might also like