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
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')))
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)
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
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')))
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)