R Programming Array Notes
R Programming Array Notes
Array Data structure: Arrays are essential data storage structures defined by a
fixed number of dimensions. In R arrays are the data objects which allow us to
store data in more then two dimension. It is stored data in the form of matrix.
Arrays are used for the allocation of space at contiguous memory location.
1)How to create array
2)Naming of columns and row
3)Accessing the elements
4)manipulate of elements
****************************************************************
1) How to create array : 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 vector values in the dim parameter.
a)Array in Single Dimension : In single dimension array we use array function
with one parameter.
syntax= array(Data)
EXAMPLE
x=c(10,15,20,25,30,35)
y=array(x)
y
class(y)
b)Array in multi Dimension: In multidimension array we use array function
with two parameters are as following.
1)data
2)Dimension=row,col,dim
syntax: array(data,dim=c(row,col,array))
where,
Data: Data is the first argument in the array() function. It is an input vector
which is given to the array.
Array: In R the array consist of multidimensional matrices.
Row: This parameter define the number of rows elements which an array can
store.
Column: This parameter define the number of Column elements which an
array can store.
EXAMPLE-1
a=c(10,20,30,40,50,60)
x=array(a,dim=c(2,3,3))
x
EXAMPLE-2
b=c(10,20,30,40,50,60,70,80,90,100,110,120)
y=array(b,dim=c(2,3,3))
y
2)Naming of column and row: In R we can give the names to the rows,column
and matrices of the array. This is done with the help of the dim name
parameter of the array() function.
V1=c(1,4,8)
V1
V2=c(10,20,30,40,50,60)
print(V2[3])
V3=array(c(V1,V2),dim=c(3,3,3))
print(V3)
Row_name=c("R1","R2","R3")
Col_name=c("C1","C2")
AR_name=c("AR1","AR2","AR3")
V3=array(c(V1,V2),dim=c(3,2,3),dimnames=list(Row_name,Col_name,AR_nam
e))
V3
Note: It is not necessary to give name to the rows and columns. It is only to
differentiate the row and column for better understanding.
a) Extract specific row: If we want to extract specific row then assign element
in the first position with vector and keep the second position as an empty and
in the third position assign number of array.
EXAMPLE-1
a=c(10,20,30,40,50,60,70,80,90,100,110,120)
x=array(a,dim=c(2,3,3))
print(x[c(1), ,2])
2) Extract specific column: If we want to extract specific column then keep the
first position as an empty and assign element in the second position with
vector and and in the third position assign number of array.
EXAMPLE-2
a=c(10,20,30,40,50,60,70,80,90,100,110,120)
x=array(a,dim=c(2,3,3))
print(x(c[,(2),3]))
3)Extract both rows and column: If we want to extract both rows and column
then assign elements in both the position with vector.
EXAMPLE-3
a=c(10,20,30,40,50,60,70,80,90,100,110,120)
x=array(a,dim=c(2,3,3))
print(x)
print(x[c(1,2),c(2),1])
print(x[c(1),c(1,3),2])
4) Manipulation of Array
1) Addition of element: Elements can be appended at the different positions in the
array. The sequence of elements is retained in order of their addition to the array.
The time complexity required to add new elements is O(n) where n is the length of
the array. The length of the array increases by the number of element additions.
There are various in-built functions available in R to add new values.
a) Addition of element using vector:
EXAMPLE
x<-c(10,20,30,40,50)
y<-array(x)
y<-c(y,60)
y
b)Addition of element using append function
syntax=append(data,value)
Example
x<-c(10,20,30,40,50)
y<-array(x)
y<-append(x,75)
print(y)
x<-c(10,20,30,40,50)
y<-array(x)
LN<-length(y)
y[LN+1]<-78
print(y)
d) Add more than 1 element
EXAMPLE
x<-c(10,20,30,40,50)
y<-array(x)
y<-append(x,c(55,65,79),after=length(y)+3)
y
print(y[!y%in%z])
3) Updating Existing Elements of Array
The elements of the array can be updated with new values by
assignment of the desired index of the array with the modified value. The
changes are retained in the original array. If the index value to be
updated is within the length of the array, then the value is changed,
otherwise, the new element is added at the specified index. Multiple
elements can also be updated at once, either with the same element
value or multiple values in case the new values are specified as a vector.
R
x<-c(10,20,30,40,50,60,70) 89
y<-array(x)
y[c(2,5)]<-c(120,150)
print(y)