Retrieving an Element of an Array: Difference between revisions

Content added Content deleted
Line 431: Line 431:
aList.pop(0) # Pop first item in a list
aList.pop(0) # Pop first item in a list
'''Note:''' When using the pop() method, the element is removed from the list.
'''Note:''' When using the pop() method, the element is removed from the list.

=={{header|R}}==

An element from a vector can be retrieved with the []; slices can be taken using ranges or another vector of indexes.

<lang R>a <- 1:10 # create a vector made of number from 1 to 10
print(a[1]) # print the first element
print(a[4:6]) # print elements from 4 to 6
print(a[c(1,8, 9)]) # print element 1, 8 and 9</lang>

Comma separated indexes can be used to index matrix, e.g.
<lang R>a <- matrix(c(1,2,3,4), 2, 2)
print(a[2,1])</lang>


=={{header|Ruby}}==
=={{header|Ruby}}==