William
William

Reputation: 805

Rpy2 - List of List of Dataframes

I'm trying to figure out how to use python to do file parsing from XML files into a data structure to pass into R.

What I need to create in R is a List of Lists of Dataframes:

Nodes = data.frame()
Edges = data.frame()

NetworkCompListA = list()
NetworkCompListA[['Nodes']] = Nodes
NetworkCompListA[['Edges']] = Edges

Networks = list()
Networks[['NetA']] = NetworkCompListA
Networks[['NetB']] = NetworkCompListB

I know how to create a dataframe from the examples in the Rpy2 documentation.

import rpy2.rlike.container as rlc
od = rlc.OrdDict([('value', robjects.IntVector((1,2,3))),
                      ('letter', robjects.StrVector(('x', 'y', 'z')))])
df = robjects.DataFrame(od)

How do I insert 'df' into a List and then insert that list into another list in python and then write that out to an rdata file to load into another instance of R?

Thanks!

Upvotes: 2

Views: 941

Answers (1)

lgautier
lgautier

Reputation: 11555

The class ListVector requires an object that implements iteritems() (such as a dict, or a OrderedDict). Note that in R data.frames are just lists with a the (loose) constrain that all elements should be vectors of the same length (or a matrix with the matching number of rows can be accepted), and with row names and column names (list's names being the column names).

from rpy2.robjects.vectors import ListVector, DataFrame

# rpy2's OrdDict was added because there was no ordered dict
# in Python's stdlib. It should be gone by rpy2-2.5
from collections import OrderedDict

od = OrderedDict((('a', 1), ('b', 2)))
df = DataFrame(od)

od_l = OrderedDict((('df', df),))
df_in_list = ListVector(od_l)
df_in_list_in_list = ListVector(OrderedDict((('df_in_list', df_in_list),))

Upvotes: 3

Related Questions