rlike

Overview

The package proposes R features for a pure Python context, that is without an embedded R running.

Containers

The module contains data collection-type data structures. OrdDict and TaggedList are structures with which contained items/elements can be tagged.

The module can be imported as follows:

>>> import rpy2.rlike.container as rlc

OrdDict

The OrdDict 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.OrdDict(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'

The Python docstring for the class is:

class rpy2.rlike.container.OrdDict(c: Iterable[Tuple[Optional[str], Any]] = [])[source]

Implements the Ordered Dict API defined in PEP 372. When odict becomes part of collections, this class should inherit from it rather than from dict.

This class differs a little from the Ordered Dict proposed in PEP 372 by the fact that: not all elements have to be named. None as a key value means an absence of name for the element.

byindex(i: int) → Any[source]

Fetch a value by index (rank), rather than by key.

get(k[, d]) → OD[k] if k in OD, else d. d defaults to None[source]
index(k: str) → int[source]

Return the index (rank) for the key ‘k’

items() → an iterator over the (key, value) items of D[source]
keys()[source]
reverse()[source]

Reverse the order of the elements in-place (no copy).

TaggedList

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:

class rpy2.rlike.container.TaggedList(seq, tags=None)[source]

A list for which each item has a ‘tag’.

Parameters
  • l – list

  • tag – optional sequence of tags

append(obj, tag=None)[source]

Append an object to the list :param obj: object :param tag: object

extend(iterable)[source]

Extend the list with an iterable object.

Parameters

iterable – iterable object

insert(index, obj, tag=None)[source]

Insert an object in the list

Parameters
  • index – integer

  • obj – object

  • tag – object

items() → an iterator over the (key, value) items of D[source]
iterontag(tag)[source]

iterate on items marked with one given tag.

Parameters

tag – object

itertags()[source]

iterate on tags.

Return type

iterator

pop(index=None)[source]

Pop the item at a given index out of the list

Parameters

index – integer

remove(value)[source]

Remove a given value from the list.

Parameters

value – object

reverse()[source]

Reverse the order of the elements in the list.

settag(i, t)[source]

Set tag ‘t’ for item ‘i’.

Parameters
  • i – integer (index)

  • t – object (tag)

sort(reverse=False)[source]

Sort in place

Tools for working with sequences

Tools for working with objects implementing the sequence protocol can be found here.

rpy2.rlike.functional.tapply(seq, tag, fun)[source]

Apply the function fun to the items in seq, grouped by the tags defined in tag.

Parameters
  • seq – sequence

  • tag – any sequence of tags

  • fun – function

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)]

Indexing

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']
rpy2.rlike.indexing.order(seq, cmp = default_cmp, reverse = False)[source]

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
  • seq – sequence

  • cmp – function

  • reverse – boolean

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']