Add column to DataFrame in rpy2

Question:

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
Asked By: mcsim

||

Answers:

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')))
Answered By: mcsim

To get an equivalent of $ assignment use it directly as a three-argument function. First import the base package:

from rpy2.robjects.packages import importr

base = importr('base')
set_column = getattr(base, '$<-')

Then to acheive the equivalent of:

df$new_colname <- value

Use:

df = set_column(df, 'new_colname', value)
Answered By: krassowski
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.