Indexing: R vs Python
HTML-код
- Опубликовано: 6 сен 2024
- In this video, we show indexing of R vectors and Python's numpy arrays. Not just one of the languages, but both Python and R.
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.
In python, negative indexing can 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.
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.
#rstats #Python #numpy