Indexing and slicing R vectors and Python arrays

Indexing allows you to select a subset of elements in a given object containing a sequence of data elements. You can index a vector in R and a numpy array in Python by using square brackets [] and specifying the indices of the elements you want to extract.

While square brackets are used for both Python and R indexing, how you specify index values within square brackets differs between the two languages. For example, x[1] accesses the first element of an R vector x; and y[0] accesses the first element of a numpy array y in Python.

R: Creating an R vector and accessing one element

Let’s create an R vector x using the concatenate function c().

#R
> x <- c(1, 2, 3, 4, 5)

You can extract the first element of the vector by using the index 1. R language indexing starts from 1 (i.e., R is 1-indexed language).

#R
> x[1]
[1] 1

You extract the 3rd element of the vector by using the index 3.

#R
> x[3]
[1] 3

The last element of the vector can be accessed using

#R
> x[length(x)]

The length of the vector x we created is 5. The length() function tells you the number of entries. The last element of an R vector is indexed by an integer value equal to the length of the vector, which in this example is 5.

#R
> print(x)
[1] 1 2 3 4 5

> length(x)
[1] 5

> x[5] ## last element
[1] 5

> x[length(x)] ## you also can directly use length(x) in the square brackets
[1] 5

Python: Creating a numpy array and accessing one element

Let’s create a one dimensional numpy array in python. We first need to import the numpy library as np and then use the function array() that comes with the library to create an array containing 5 elements. Here is the code:

#Python
>>> import numpy as np
>>> y = np.array([1, 2, 3, 4, 5])
>>> print(y)
[1 2 3 4 5]

The first element of the array y can be accessed using an index value of 0 inside the square brackets [] following the name of the array y.

#Python
>>> y[0]
1

Note the difference between R and Python: Python is a 0-indexed programming language while R is 1-indexed.

The 2nd element of the array is indexed by 1, and the 3rd element is indexed by 2 and so on. To access the 3rd element, you use

#Python
>>> y[2]
3

The last element of the array is indexed by 4 since the length of y which we can find using the function len() is 5.

#Python
>>> print(y)
[1 2 3 4 5]
>>> len(y)
5
>>> y[4]
5

Negative indexing can also be used to access the elements of a numpy array from the end. For example, the last element has an index of -1, the second last element has an index of -2, and so on.

#Python
>>> print(y)
[1 2 3 4 5]
>>> y[-1]
5
>>> y[-2]
4

Negative indexing has a different behavior in R. It has the effect of excluding elements instead of the reverse indexing behavior in Python. For example x[-1] excluded the first element of x and accesses the rest, and x[-2] returns all elements of vector x except the 2nd entry.

#R
> print(x)
[1] 1 2 3 4 5
> x[-1]
[1] 2 3 4 5
> x[-2]
[1] 1 3 4 5

The next lesson in this series will be on slicing R vectors and numpy arrays. That is extracting multiple elements of an R vector and numpy arrays at once.

Bereket Kindo, PhD
Bereket Kindo, PhD

Bereket Kindo’s interests include Bayesian analysis and machine learning.