On Mon, Mar 23, 2009 at 4:59 AM, Gary Strangman <str...@nmr.mgh.harvard.edu> wrote: > > The rnumpy concept looks great, and addresses some annoyances I've > encountered working at the interface between R and numpy.
Great! > I hope to test > rnumpy this week, but in the meantime ... the text suggests that R data > frames can be manipulated/sliced using rnumpy. Can they be created > directly as well? Is that what is meant by "numpy arrays are mapped > correctly by default"? If so, what's the syntax? For creating data frames, there are few different easy options: 1) If you have a numpy record array, and pass it to any R function or to rnumpy.rcopy, then it will be mapped to an R data frame. So you can just say r.lm("y ~ x", my_recarray) 2) If you have a Python dict, then it will be mapped to an R list, and it's easy to turn an R list into a data frame. So I usually end up building data frames like: d = {"x": [1, 2, 3], "y": [10, 11, 12]} r.as_data_frame(d) (Also, many functions like lm are happy to accept a raw list, so you can make calls like 'r.lm("y ~ x", d)' directly.) 3) You can drop to the lower-level, more-explicit way of calling R functions, ".rcall". It looks like this: r.data_frame.rcall([("x", [1, 2, 3]), ("y", [10, 11, 12])]) (If you check out the definition of RClosure.__call__, then you'll see it's just a thin convenience wrapper around .rcall. This identical to r.data_frame(x=[1, 2,3], y=[10, 11, 12]), except that you control the order of arguments, and the argument names don't undergo underscore conversion.) (1) is super-easy if you have a record array, but I find record arrays a bit cumbersome to construct in the first place. (2) takes the least typing, but you can't control which order your columns end up in. (I find I usually don't care, but sometimes it matters.) (3) Gives you explicit control over the column order, without much extra typing. > My particular use case is > conversion of flexible-type numpy arrays or numpy arrays of type object. I'm not sure what exactly you mean by flexible-type numpy arrays. Numpy arrays of type object are converted by: -- we call the conversion routine recursively to map each element to R -- then we bundle them up in an R list (the only R container type that can hold items that have different types from each other) What sort of object arrays are you working with, and what would you want to happen to them? > Also, can one return a data frame that has been manipulated in R to a > numpy (flexible-type) array? There isn't much explicit support for copying data out of R and into Python, ATM. This lack has been annoying me too; I'm just not sure exactly how it should work. Collecting use cases would be a good step... By "flexible-type" do you mean record array? -- Nathaniel ------------------------------------------------------------------------------ Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are powering Web 2.0 with engaging, cross-platform capabilities. Quickly and easily build your RIAs with Flex Builder, the Eclipse(TM)based development software that enables intelligent coding and step-through debugging. Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com _______________________________________________ rpy-list mailing list rpy-list@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/rpy-list