Accessing functions with a dot in their name (eg. “as.vector”) using rpy2

南笙酒味 提交于 2019-11-30 09:25:18

问题


I am trying to access the "as.vector" R function from within Python, using rpy2. Let's say, for the sake of simplicity, that I want to do something as simple as this using rpy2 (R code):

x <- as.vector(c(1, 2, 3))

Since "as.vector" contains a dot in its name, it is not directly available as a member of rpy2.robjects.r

According to the documentation, rpy2 replaces dots by underscores for named function parameters, but it doesn't seem to work for the function name itself. I tried eg. "as_vector", "asvector" to no avail.

Any ideas?


回答1:


Get a reference to the function using the rpy2.robjects.r interface.

So, you could do something like:

as_vector = robjects.r("as.vector")
vect = as_vector(r_vect)



回答2:


According to the documentation, rpy2 does not replace dots by underscores when using 'rpy2.robjects.r'.

You may want to consider the use of 'importr()'



来源:https://stackoverflow.com/questions/6032060/accessing-functions-with-a-dot-in-their-name-eg-as-vector-using-rpy2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!