On Fri, 2008-10-17 at 09:53 +0100, Peter wrote:
> Laurent Gautier wrote:
> >
> > As you noticed, there was something missing in the example I just gave.
> > It should have been:
> >
> > import array
> > import rpy2.robjects as ro
> >
> > d = dict(x = array.array('i', [1,2]), y = array.array('i', [2,3]))
> > dataf = ro.r['data.frame'](**d)
> 
> Hi Laurent,
> 
> You mentioned using rpy2.rlike.container.TaggedList and
> rpy2.robjects.RDataFrame (new in rpy2) as a "safer" approach.

It's safer in the sense that Python and R code for the same thing will
be closer, somehow.
A Python dictionary does not keep the order in which elements were
added, and in R lists can have several elements with the same name.

Example of R code:

> dataf <- data.frame(x=1:2, y=3:4, x=5:6)
> dataf
  x y x.1
1 1 3   5
2 2 4   6
> dataf <- data.frame(x=1:2, y=3:4, x=5:6, check.names=FALSE)
> dataf
  x y x
1 1 3 5
2 2 4 6

Currently, in rpy2, you'll get the first behavior (and an error if
trying to set the parameter check.names).
There is a relatively small number of use cases not yet taken care of
within rpy2.robjects (like if the R constructor 'data.frame' is a
generic function).

>   Could
> you show us how this example would work that way?

Laurent Oget worked it out.

import array
import rpy2.robjects as ro
import rpy2.rlike.container as rlc

x = ro.RVector(array.array('i', [1,2]))
y = ro.RVector(array.array('i', [3,4]))

tl = rlc.TaggedList([x, y], tags = ('x', 'y'))

dataf = ro.RDataFrame(tl)
print(dataf)

x = ro.RVector(array.array('i', [5,6]))
tl.append(x, tag='x')

dataf = ro.RDataFrame(tl)
print(dataf)






>   Given how important
> dataframes are in R, this discussion could become the basis for a
> section of the rpy2 manual.
> 
> Peter
> 
> -------------------------------------------------------------------------
> This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
> Build the coolest Linux based applications with Moblin SDK & win great prizes
> Grand prize is a trip for two to an Open Source event anywhere in the world
> http://moblin-contest.org/redirect.php?banner_id=100&url=/
> _______________________________________________
> rpy-list mailing list
> rpy-list@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/rpy-list


-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
rpy-list mailing list
rpy-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rpy-list

Reply via email to