Question

I want to do the equivalent of this R code:

m2 <- cbind(1,2)
colnames(m2) <- c("x","Y")

When I do

import rpy2.robjects as R
m2 = R.r['cbind'](1,2)
R.r['colnames'](m2) = R.StrVector(['x','y'])

I get this error:

SyntaxError: can't assign to function call

I tried

>>> m2 = R.r['colnames'](m2, R.StrVector(['x','y']))
>>> print m2

[1] "x1" "y2" 

And

>>> params = {'do.NULL':False}
>>> m2 = R.r['colnames'](R.StrVector(['x','y']), m2, **params)
>>> print m2

[1] "11" "21"

Which both don't give the result that I want. So how can I use colnames to change the column names of a dataframe?

Was it helpful?

Solution

If anyone wants to know, the answer is:

m2.colnames = R.StrVector(['x','y']) 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top