0% found this document useful (0 votes)
4 views12 pages

R Programming Array Notes

The document provides a comprehensive overview of arrays in R, detailing their creation, manipulation, and access methods. It covers single and multi-dimensional arrays, naming conventions for rows and columns, and various operations such as adding, removing, and updating elements. Examples are included to illustrate each concept clearly.
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)
4 views12 pages

R Programming Array Notes

The document provides a comprehensive overview of arrays in R, detailing their creation, manipulation, and access methods. It covers single and multi-dimensional arrays, naming conventions for rows and columns, and various operations such as adding, removing, and updating elements. Examples are included to illustrate each concept clearly.
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/ 12

Array_R

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.

3) Accessing element of Array: The R arrays can be accessed by using indices


for different dimensions separated by commas. Different components can be
specified by any combination of elements names or positions.
1)Accessing the single dimension array: In this unidimensional array the
element can be accesse by using index of the corresponding elements.
EXAMPLE
Val=c(10,20,30,40,50,60)
print(Val[4])
# If we want to extract more than one element
print(Val[c(2,4,6)])

2)Accessing the Multi dimension array:


a=c(10,20,30,40,50,60,70,80,90,100,110,120)
x=array(a,dim=c(2,3,3))
print(x)

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)

c) Addition of element by using length function:

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

e) Add new element after 3rd index:


EXAMPLE
x<-c(10,20,30,40,50)
y<-array(x)
y<-append(y,c(35,38,39),after=3)
y

2) Removing Elements from Array


Elements can be removed from arrays in R, either one at a time or multiple together.
These elements are specified as indexes to the array, wherein the array values
satisfying the conditions are retained and rest removed. The comparison for removal
is based on array values. Multiple conditions can also be combined together to
remove a range of elements. Another way to remove elements is by using %in
% operator wherein the set of element values belonging to the TRUE values of the
operator are displayed as result and the rest are removed.

a) Remove single value element from the array.


x<-c(10,20,30,40,50,60,70)
y<-array(x)
y=y[y !=50]
y
b) Removing elements based on a conditions like greater than less than and
equal.
EXAMPLE
y<-y[y>20 & y<=60]
x<-c(10,20,30,40,50,60,70)
y<-array(x)
y
z=c(20,40,60)

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

A) Updating a single elements


EXAMPLE
x<-c(10,20,30,40,50,60,70)
y<-array(x)
y[3]<-56
y
b) Upadating sequence of elements
y[2:5]<-1
c) Updating two indices with two different values.

x<-c(10,20,30,40,50,60,70) 89
y<-array(x)
y[c(2,5)]<-c(120,150)
print(y)

d) Adding new element to the array


x<-c(10,20,30,40,50,60,70) 89
y<-array(x)
y[8]<-290
print(y)

You might also like