0% found this document useful (0 votes)
62 views5 pages

R Arrays

This document provides information about arrays in R. It discusses how to create arrays using the array() function and specify dimensions. It also covers how to name rows, columns, and matrices of an array. The document shows how to access and manipulate array elements using indexing and mathematical operations. It introduces the apply() function for performing calculations across array elements.

Uploaded by

nasby18
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)
62 views5 pages

R Arrays

This document provides information about arrays in R. It discusses how to create arrays using the array() function and specify dimensions. It also covers how to name rows, columns, and matrices of an array. The document shows how to access and manipulate array elements using indexing and mathematical operations. It introduces the apply() function for performing calculations across array elements.

Uploaded by

nasby18
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
You are on page 1/ 5

LEVEL: BACHELOR OF DATA SCIENCE YEAR 2

MODULE NAME: DATA ANALYSIS WITH R


LECTURER’S NAME: MR MAJALIWA JOHN
NATURE OF ASSIGNMENT: GROUP ASSIGMENT

S/NO NAMES REGISTRATION NO


ASYA HAJI HAJI 1527010162199
HASAM TEMA BAKARI 1517010032199
YASSIR H MOLLEL 1517010272101
MERCY IJAN MVUNGI 1527010052198
R Arrays
In R, arrays are the data objects which allow us to store data in more than two dimensions. In R,
an array is created with the help of the array() function.
This array() function takes a vector as an input and to create an array it uses vectors values in the
dim parameter.
For example-
if we will create an array of dimension (2, 3, 4) then it will create 4 rectangular matrices of 2
row and 3 columns.
R Array Syntax
There is the following syntax of R arrays:
1. array_name <- array(data, dim= (row_size, column_size, matrices, dim_names))  
The data is the first argument in the array() function. It is an input vector which is given to the
array.
matrices
In R, the array consists of multi-dimensional matrices.
row_size
This parameter defines the number of row elements which an array can store.
column_size
This parameter defines the number of columns elements which an array can store.
dim_names
This parameter is used to change the default names of rows and columns.
To create array
In R, array creation is quite simple. We can easily create an array using vector and array()
function. In array, data is stored in the form of the matrix. There are only two steps to create a
matrix which are as follows
1. In the first step, we will create two vectors of different lengths.
2. Once our vectors are created, we take these vectors as inputs to the array.

Example
1. #Creating two vectors of different lengths  
2. vec1 <-c(1,3,5)  
3. vec2 <-c(10,11,12,13,14,15)    
4. #Taking these vectors as input to the array   
5. res <- array (c (vec1, vec2), dim=c (3,3,2))  
6. print(res)  

Naming rows and columns


In R, we can give the names to the rows, columns, and matrices of the array. This is done with
the help of the dim name parameter of the array () function.
Example
1. #Creating two vectors of different lengths  
2. vec1 <-c(1,3,5)  
3. vec2 <-c(10,11,12,13,14,15)    
4. #Initializing names for rows, columns and matrices  
5. col_names <- c("Col1","Col2","Col3")  
6. row_names <- c("Row1","Row2","Row3")  
7. matrix_names <- c("Matrix1","Matrix2")  
8. #Taking the vectors as input to the array   
9. res <- array(c(vec1,vec2),dim=c(3,3,2),dimnames=list(row_names,col_names,matrix_na
mes))  
10. print(res)  
Accessing array elements
we can access the elements of the array with the help of the indexing method. Let see an example
to understand how we can access the elements of the array using the indexing method.
Example

#Array_name[row_possition,column_position,mattrix_level]
res[3,3,2]
res[3,1,1] #accesing individual element
res[1,,1] #accesing all columns in specific row
res[,2,1] #accesing all rows in specific column
res[,,1] #accesing all elements
Manipulation of elements
The array is made up matrices in multiple dimensions so that the operations on elements of an
array are carried out by accessing elements of the matrices.
Example
1. #Creating two vectors of different lengths  
2. vec1 <-c(1,3,5)  
3. vec2 <-c(10,11,12,13,14,15)  
4.   
5. #Taking the vectors as input to the array1   
6. res1 <- array(c(vec1,vec2),dim=c(3,3,2))  
7. print(res1)  
8.   
9. #Creating two vectors of different lengths  
10. vec1 <-c(8,4,7)  
11. vec2 <-c(16,73,48,46,36,73)  
12.   
13. #Taking the vectors as input to the array2   
14. res2 <- array(c(vec1, vec2),dim=c(3,3,2))  
15. print(res2)  
16.   
17. #Creating matrices from these arrays  
18. mat1 <- res1[,,2]  
19. mat2 <- res2[,,2]  
20. res3 <- mat1+mat2 #adding matrices
21. res4<-mat1-mat2 #subtracting matrices
22. res5<- mat1*mat2 #multiplying matrices
23. print(res3)  

Calculations across array elements


For calculation purpose, r provides apply () function. This apply function contains three
parameters i.e., x, margin, and function
apply(X, MARGIN, FUN)
Here:
-x: an array or matrix
-MARGIN: take a value or range between 1 and 2 to define where to apply the function:
-MARGIN=1`: the manipulation is performed on rows
-MARGIN=2`: the manipulation is performed on columns
-MARGIN=c(1,2)` the manipulation is performed on rows and columns
-FUN: tells which function to apply. Built functions like mean, median, sum, min, max and even
user-defined functions can be applied

Example  
#using apply function   
output1<- apply(res,1, sum)
output 2 <- apply(res,1, mean)
output 3 <- apply(res,2, sd)
print(output 1)  
print(output 2)  
print(output 3)  
Note: that if you’d like to find the mean or sum of each row, it faster to use the built-in
rowMeans() or rowSums()
Example
rowMeans(res)
rowSums(res)

You might also like