Next: , Previous: Class conversion, Up: R to Python



4.1.4 Basic conversion

This mode tries to convert a Robj object to a basic Python object. It can convert most of the R types to an adequate Python type; but, sometimes, some information is lost.

The following table shows the conversion of types. When converting lists of objects, the rules are applied recursively.

R object Python object Notes
—— —— ——
NULL None
Integer Plain integer (1)
Logical Plain Integer
Real Float (2)
Complex Complex
String String
Vector list or dictionary (3)
List list or dictionary (3)
Array Array or list (4)
Other (fails)

Notes:

(1)
The R programming language has an integer value represented by NA (not applicable) which is converted to and from Python as the minimum integer (which is the actual value in R). Be careful, because the semantic is completely different:
Python:
NA/100 –> (-sys.maxint-1)/100 != NA
R:
NA/100 –> NA

(2)
The IEEE float values NaN (not a number) and Inf (infinite) are also converted between Python and R.
(3)
Vectors and lists from R may have an attribute names, which are the names of the elements of the sequence. In those cases, the sequence is translated to a Python dictionary whose keys are the names, and the values are the corresponding values of the sequence. When there are no names, the vector or list is translated to a normal Python list.
(4)
When Numeric is installed, a R array is converted to a Numeric array. Otherwise, a list of nested lists is returned.

When converting R arrays, the column and row names are discarded. Also, for R data frames, row names are discarded while column names are kept. And many other R objects with complex attribute structure may loose some of its attributes when converted to Python objects. When it is necessary to keep all the information of an R object, it is better to use the CLASS_CONVERSION mode with proper classes (see Useful examples), or to use the NO_CONVERSION mode (see No conversion).