Array manipulation - R vs Python

In this blog, I will talk about array manipulation via R and Python. You willsee how to create an array, insert and delete an element from a 1d-array and a2d-array.Creating arraysRdim1 <- c("one...

In this blog, I will talk about array manipulation via R and Python. You will see how to create an array, insert and delete an element from a 1d-array and a 2d-array.

Creating arrays

R

dim1 <- c("one", "two", "three", "four")
dim2 <- c("A", "B", "C")

arr_2d <- array(1:12, dim = c(4, 3), dimnames = list(dim1, dim2))
> arr_2d
      A B  C
one   1 5  9
two   2 6 10
three 3 7 11
four  4 8 12

> class(arr_2d)
[1] "matrix"

> dim(arr_2d)
[1] 4 3

> is.numeric(array_2d)
[1] TRUE

Python

import numpy as np
arr_2d = np.array([[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]])
>>> arr_2d
array([[ 1,  5,  9],
       [ 2,  6, 10],
       [ 3,  7, 11],
       [ 4,  8, 12]])

>>> type(arr_2d)
<class 'numpy.ndarray'>

>>> arr_2d.shape
(4, 3)

>>> arr_2d.dtype
dtype('int64')

Indexing

R

> arr_2d[1, 2]
[1] 5

> arr_2d[, 2]
  one   two three  four 
    5     6     7     8 

> arr_2d[, c("A", "C")]
      A  C
one   1  9
two   2 10
three 3 11
four  4 12

Python

>>> arr_2d[0, 1]
5

>>> arr_2d[0][1]
5

>>> arr_2d[0]
array([1, 5, 9])

>>> arr_2d[:, 1]
array([5, 6, 7, 8])

>>> arr_2d[:, [0,2]]
array([[ 1,  9],
       [ 2, 10],
       [ 3, 11],
       [ 4, 12]])

Inserting an element into 1d-array

R

> arr_1d <- c("k", "j", "w", "d", "v")
> arr_1d
[1] "k" "j" "w" "d" "v"

> arr_1d <- append(arr_1d, "q", after = 3)
> arr_1d
[1] "k" "j" "w" "q" "d" "v"

Python

>>> import numpy as np
>>> arr_1d = np.array([3, 5, 1, 3])
>>> arr_1d
array([3, 5, 1, 3])

>>> arr_1d = np.append(arr_1d, [7])
>>> arr_1d
array([3, 5, 1, 3, 7])

Inserting an element into 2d-array

R

> arr_2d <- matrix(1:9, ncol = 3)
> arr_2d
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

> arr_2d <- rbind(arr_2d, c(7, 77, 777))
> arr_2d
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9
[4,]    7   77  777

Python

>>> import numpy as np
>>> arr_2d  = np.array([[1, 1], [2, 2], [3, 3]])
>>> arr_2d
array([[1, 1],
       [2, 2],
       [3, 3]])

>>> arr_2d = np.insert(arr_2d, 1, 5, axis=1)
>>> arr_2d
array([[1, 5, 1],
       [2, 5, 2],
       [3, 5, 3]])

Removing an element from 1d-array

R

> arr_1d <- arr_1d[-5]
> arr_1d
[1] "k" "j" "w" "q" "v"

Python

>>> arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
>>> arr
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])

>>> arr = np.delete(arr, [1, 3, 5], None)
>>> arr
array([ 1,  3,  5,  7,  8,  9, 10, 11, 12])

Removing an element from 2d-array

R

> arr_2d[, -1]
     [,1] [,2]
[1,]    4    7
[2,]    5    8
[3,]    6    9
[4,]   77  777

Python

>>> arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
>>> arr
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])

>>> arr = np.delete(arr, 1, 0)
>>> arr
array([[ 1,  2,  3,  4],
       [ 9, 10, 11, 12]])