Anne Ghisla wrote: > Hi list, > > I'm using packaged rpy2 on Debian Squeeze in a python-grass script to > perform kriging and, among other things that rpy2 executes smoothly, I > need tho change the names of two columns in a dataframe. > This edit is necessary because the dataframe coming from previous > as.data.frame command receives arbitrary column names. > > Picking up some sample code: > > d = {'value': robjects.IntVector((1,2,3)), > 'letter': robjects.StrVector(('x', 'y', 'z'))} > dataf = robjects.r['data.frame'](**d) > > For example, I need to change 'letter' column name into 'code' > without modifying its content neither other columns. In R I would have > written: > > names(dataf) = c("value", "code") > > I tried alternatively: > > dataf.names = robjects.r.gsub('letter','code',dataf.names) > dataf.names = robjects.r.c('value','code') > > with no success. The error says that dataf.names doesn't support > assignment. Also I didn't succeed in handling it as Python object, as > the type is RVector. > Gsub works when I assing the result to a copy of dataf.names. > > Any suggestion and link to docs/examples will be appreciated.
One of the shortcomings in rpy2.robjects v2.0.x concerns the rebinding of underlying R objects, needed by the pass-by-value paradigm in R (rnumpy has been working around that by using delegation rather than inheritance). It has been fixed in rpy2-2.1dev. Otherwise, since you want to replace the elements in a vector, the usual __setitem__ should replace them "in-place". suggestion: new_names = robjects.r.gsub('letter','code',dataf.names) for i, val in enumerate(new_names): dataf.names[i] = val link: http://rpy.sourceforge.net/rpy2/doc-dev/html/robjects.html#names HTH, L. > thanks in advance > Anne > > > ------------------------------------------------------------------------ > > ------------------------------------------------------------------------------ > > > ------------------------------------------------------------------------ > > _______________________________________________ > rpy-list mailing list > rpy-list@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/rpy-list ------------------------------------------------------------------------------ _______________________________________________ rpy-list mailing list rpy-list@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/rpy-list