pythonrrpy2

Add column to DataFrame in rpy2


I'm using R in python with rpy2. I have a dataframe with some columns in it and I want to add additional one.

As far as I understand I, in R I would write something like this:

df$new_colname <- cumsum(df$oldcolname)

But how can I translate this to rpy2?

I tried many variations of this:

ipdb> d.rx2('cum_misses') <- ro.r.cumsum(d.rx2('misses'))
*** Newest frame

But always get this:

ipdb> p d.rx2('cum_misses')
rpy2.rinterface.NULL

Solution

  • So, I found the answer.

    To add a column I have to write following code:

    d = ro.r.cbind(d, cum_misses=ro.r.cumsum(d.rx2('misses')))