Platforms: Unix, Windows
The package proposes R features for a pure Python context, that is without an embedded R running.
The module contains data collection-type data structures. ArgsDict and TaggedList are structures with which containeed items/elements can be tagged.
The module can be imported as follows:
>>> import rpy2.rlike.container as rlc
The ArgsDict proposes an implementation of what is sometimes referred to in Python as an ordered dictionnary, with a particularity: a key None means that, although an item has a rank and can be retrieved from that rank, it has no “name”.
In the hope of simplifying its usage, the API for an ordered dictionnary in PEP 372 was implemented. An example of usage is:
>>> x = (('a', 123), ('b', 456), ('c', 789))
>>> nl = rlc.ArgsDict(x)
>>> nl['a']
123
>>> nl.index('a')
0
Not all elements have to be named, and specifying a key value equal to None indicates a value for which no name is associated.
>>> nl[None] = 'no name'
A TaggedList is a Python list in which each item has an associated tag. This is similar to named vectors in R.
>>> tl = rlc.TaggedList([1,2,3])
>>> tl
[1, 2, 3]
>>> tl.tags()
(None, None, None)
>>> tl.settag(0, 'a')
>>> tl.tags()
('a', None, None)
>>> tl = rlc.TaggedList([1,2,3], tags=('a', 'b', 'c'))
>>> tl
[1, 2, 3]
>>> tl.tags()
('a', 'b', 'c')
>>> tl.settag(2, 'a')
>>> tl.tags()
('a', 'b', 'a')
>>> it = tl.iterontag('a')
>>> [x for x in it]
[1, 3]
>>> [(t, sum([i for i in tl.iterontag(t)])) for t in set(tl.itertags())]
[('a', 4), ('b', 2)]
The Python docstring for the class is:
A list for which each item has a ‘tag’.
Parameters: |
|
---|
Append an object to the list :param obj: object :param tag: object
Extend the list with an iterable object.
Parameters: | iterable – iterable object |
---|
Insert an object in the list
Parameters: |
|
---|
Return a tuple of all pairs (tag, item).
Return type: | tuple of 2-element tuples (tag, item) |
---|
iterate on items marked with one given tag.
Parameters: | tag – object |
---|
iterate on tags.
Return type: | iterator |
---|
Pop the item at a given index out of the list
Parameters: | index – integer |
---|
Remove a given value from the list.
Parameters: | value – object |
---|
Reverse the order of the elements in the list.
Set tag ‘t’ for item ‘i’.
Parameters: |
|
---|
Sort in place
Return a tuple of all tags
Return type: | tuple |
---|
Tools for working with objects implementing the the sequence protocol can be found here.
Apply the function fun to the items in seq, grouped by the tags defined in tag.
Parameters: |
|
---|---|
Return type: | list |
>>> import rpy2.rlike.functional as rlf
>>> rlf.tapply((1,2,3), ('a', 'b', 'a'), sum)
[('a', 4), ('b', 2)]
TaggedList objects can be used with their tags (although more flexibility can be achieved using their method iterontags()):
>>> import rpy2.rlike.container as rlc
>>> tl = rlc.TaggedList([1, 2, 3], tags = ('a', 'b', 'a'))
>>> rlf.tapply(tl, tl.tags(), sum)
[('a', 4), ('b', 2)]
Much of the R-style indexing can be achieved with Python’s list comprehension:
>>> l = ('a', 'b', 'c')
>>> l_i = (0, 2)
>>> [l[i] for i in l_i]
['a', 'c']
In R, negative indexes mean that values should be excluded. Again, list comprehension can be used (although this is not the most efficient way):
>>> l = ('a', 'b', 'c')
>>> l_i = (-1, -2)
>>> [x for i, x in enumerate(l) if -i not in l_i]
['a']
Give the order in which to take the items in the sequence seq and have them sorted. The optional function cmp should return +1, -1, or 0.
Parameters: |
|
---|---|
Return type: | list of integers |
>>> import rpy2.rlike.indexing as rli
>>> x = ('a', 'c', 'b')
>>> o = rli.order(x)
>>> o
[0, 2, 1]
>>> [x[i] for i in o]
['a', 'b', 'c']