What `R` you? (R lists in python)

Previously, we uncovered what are R vectors in python. In this post, we will convert R lists in python.


A R list is a python …

Like R vectors, it depends. A R list will behave differently in python depending if it is named or not.

Unnamed R list

An unnamed list in R is a python list.

library(tidyverse)
library(reticulate)
conda_list()[[1]] %>% use_condaenv()

Relement_int=2L
Relement_bool=TRUE
Relement_char="banana"

Rlist_nameno<-list(Relement_int, Relement_bool, Relement_char)

class(Rlist_nameno)
## [1] "list"
r_to_py(Rlist_nameno) %>% class()
## [1] "python.builtin.list"   "python.builtin.object"

Special case for tuples

python has a structure similar to a python list, it is known as a tuple. There are no R data structures which are converted to python’s tuple. Nonetheless, you can create a tuple directly in R and call it later in python.

Rtuple<-tuple(66,99)
class(Rtuple)
## [1] "python.builtin.tuple"  "python.builtin.object"

A tuple created in R is still a tuple when it is translated into python.

r_to_py(Rtuple) %>% class()
## [1] "python.builtin.tuple"  "python.builtin.object"

When you print a tuple created in R, it appears as a tuple with the elements sandwiched between ( ).

Rtuple
## (66.0, 99.0)

However, when you create a tuple in python and translate it into R, the tuple gets converted into an unnamed R list.

py_run_string("Ptuple=(66,99)")

py$Ptuple
## [[1]]
## [1] 66
## 
## [[2]]
## [1] 99

Named R list

A named R list is a python dictionary

Rlist_nameyes = list(int= Relement_int, bool=Relement_bool, char=Relement_char)
class(Rlist_nameyes)          
## [1] "list"
r_to_py(Rlist_nameyes) %>% class()
## [1] "python.builtin.dict"   "python.builtin.object"

In a named R list, the names of each element list are similar to the keys in a python dictionary.

names(Rlist_nameyes)
## [1] "int"  "bool" "char"
py_eval("r.Rlist_nameyes.keys()")
## dict_keys(['int', 'bool', 'char'])

The constituent elements of each element list are equivalent to the values in a python dictionary.

Rlist_nameyes
## $int
## [1] 2
## 
## $bool
## [1] TRUE
## 
## $char
## [1] "banana"
py_eval("r.Rlist_nameyes")
## $int
## [1] 2
## 
## $bool
## [1] TRUE
## 
## $char
## [1] "banana"

Creating dictionaries directly

You can create python dictionary directly in R with the dict function.

Rdict<-dict(int= Relement_int, bool=Relement_bool, char=Relement_char)
class(Rdict)
## [1] "python.builtin.dict"   "python.builtin.object"

Let’s check that python recognises the dictionary created by R as legitimate python dictionary structure.

r_to_py(Rdict) %>% class()
## [1] "python.builtin.dict"   "python.builtin.object"

A dictionary created in R prints like dictionary in python where the {} embraces the keys and values, and the keys and values are separated with :.

Rdict
## {'int': 2, 'bool': True, 'char': 'banana'}

Sub setting

In R, the sub setting approach will influence whether the name of element list and the constituent elements will be printed or just the constituent elements will be printed. The former is known as preserving sub setting and the latter is known as simplified sub setting.

  1. Preserving sub setting

The structure of input is preserved in the output. When the input is a list, the output is a list. As the output is a list, it allows both the name of the element list and its constituent elements to be printed out. In R, you wrap the name of the element list between singular square brackets [ ].

Rlist_nameyes["int"]
## $int
## [1] 2

To achieve the same with a python dictionary would mean that given a key, the corresponding key-value pair will be printed. I’m not sure of the most elegant technique to extract specific key-value pair from a python dictionary based on a given key but I found this technique works.

py_run_string("dictfilt = lambda x, y: dict([ (i,x[i]) for i in x if i in set(y)])")

py_eval("dictfilt(r.Rlist_nameyes, ['int'])")
## $int
## [1] 2
  1. Simplified sub setting

This approach “returns the simplest possible data structure that can represent the output”. Simplified sub setting a R list will yield a vector. In other words, sub setting using the name of the element will result in only the constituent elements. The name of element list will not be printed out in the output unlike in persevered sub setting. In R, you either wrap the name of the element list between dual square brackets [[ ]]

Rlist_nameyes[["int"]]
## [1] 2

or use the dollar sign syntax $

Rlist_nameyes$int
## [1] 2

Extracting just the value in a python dictionary is done by wrapping the key between singular square bracket [ ] (notice the difference between R and python when using [ ] to subset).

py_eval("r.Rlist_nameyes['int']")
## [1] 2

python list is a..

python list is a common data structure in python. When we will convert it into R, we get a R list.

py_run_string("p_list=['notast', 0.07]")

py$p_list %>% class()
## [1] "list"

The converted list prints like a typical R list.

py$p_list
## [[1]]
## [1] "notast"
## 
## [[2]]
## [1] 0.07