On 18/01/10 16:47, Luca Beltrame wrote:
Hello,

I'm porting over some code from pure R to RPy2.1 (because I'm using R only to
perform the calculations, as I much prefer the data in other structures that
are more efficient) and I stumbled upon a function (which I didn't originally
write) that uses "which".

As far as I can remember, there isn't a perfect equivalent in Python: the
alternative would be using "which" with "robjects.r("expressiongoeshere")" but
as the object is created "outside" I would like to avoid that.

The "offending" code is (in R):

length(which(keys == mydata$label))

(mydata is a R list - hence I would be able to use the .rx2 accessor)

Has anyone got a hint on how to translate this using rpy 2.1.x?


There are many ways. For example:

# translation of the R code into rpy2
from rpy2.robjects.packages import importr
base = importr("base")
eq = base.__dict__["=="]
which = base.which
len(which(eq(keys, mydata.rx("label"))))

# A more "Pythonic" way would be
# (although with an overhead as each element in the
# column "label" will be converted into a Python
# object before comparison)
len([i+1 for i, x in enumerate(mydata.rx("label")) if x == keys])




L.




Thanks!


------------------------------------------------------------------------------
Throughout its 18-year history, RSA Conference consistently attracts the
world's best and brightest in the field, creating opportunities for Conference
attendees to learn about information security's most important issues through
interactions with peers, luminaries and emerging and established companies.
http://p.sf.net/sfu/rsaconf-dev2dev


_______________________________________________
rpy-list mailing list
rpy-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rpy-list

------------------------------------------------------------------------------
Throughout its 18-year history, RSA Conference consistently attracts the
world's best and brightest in the field, creating opportunities for Conference
attendees to learn about information security's most important issues through
interactions with peers, luminaries and emerging and established companies.
http://p.sf.net/sfu/rsaconf-dev2dev
_______________________________________________
rpy-list mailing list
rpy-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rpy-list

Reply via email to