
正文
python调用r函数 python函数调用函数代码
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
python函数调用
inname = r"C:\Python27\esri.shp"
outname = "outname.cst"
# 在此处调用该函数。函数体定义必须放在调用以前。可以通过import
read_ESRT_……(file = inname, fileOut = outname)
# 这两个参数只是字符串而已python调用r函数,指明python调用r函数你python调用r函数的文件路径。注意在python中python调用r函数,若有 \ 号,则最好使用 \\ 双斜杠,或者如上例,加上前缀 r
相关问答
Q1: python中如何调用自己写的函数
在python中,除了调用下载的扩展库之外,还可以自定义函数,方便自己。把基础模块放在固定文件夹(或相对固定文件夹),使用sys.append(r’自定义的模块路径’)实例如下:
1、在E:pycharm新建hello.py实现基础功能函数(定义一个hello()函数)
2、 调用自定义的函数
3、 运行结果为:
更多Python相关技术文章,请访问Python教程栏目进行学习!以上就是小编分享的关于python中如何调用自己写的函数的详细内容希望对大家有所帮助,更多有关python教程请关注环球青藤其它相关文章!
Q2: python中r'什么意思
Python与R的区别和联系
1、区别
Python与R的区别是显而易见的,因为R是针对统计的,python是给程序员设计的。2012年R是学术界的主流,但是现在Python正在慢慢取代R在学术界的地位。
Python与R相比速度要快。Python可以直接处理上G的数据;R不行,R分析数据时需要先通过数据库把大数据转化为小数据(通过groupby)才能交给R做分析,因此R不可能直接分析行为详单,只能分析统计结果。所以有人说:Python=R+SQL/Hive,并不是没有道理的。
Python的一个最明显的优势在于其胶水语言的特性,很多书里也都会提到这一点,一些底层用C写的算法封装在Python包里后性能非常高效(Python的数据挖掘包Orange canve 中的决策树分析50万用户10秒出结果,用R几个小时也出不来,8G内存全部占满)。但是,凡事都不绝对,如果R矢量化编程做得好的话(有点小难度),会使R的速度和程序的长度都有显著性提升。
R的优势在于有包罗万象的统计函数可以调用,特别是在时间序列分析方面,无论是经典还是前沿的方法都有相应的包直接使用。相比之下,Python之前在这方面贫乏不少。但是,现在Python有了pandas。pandas提供了一组标准的时间序列处理工具和数据算法。因此,可以高效处理非常大的时间序列,轻松地进行切片/切块、聚合、对定期/不定期的时间序列进行重采样等。近年来,由于Python有不断改良的库(主要是pandas),使其成为数据处理任务的一大替代方案。
2、联系
通过R和Python只共享文件,Python把源数据处理干净,生成格式化的文件放在预定的目录下,做个定时器让R去读文件,最终输出统计结果和图表。
让Python直接调用R的函数,R是开源项目,有rpy2之类的模块,可以实现使用python读取R的对象、调用R的方法以及Python与R数据结构转换等。
Q3: Python调用R编程——rpy2
在Python调用Rpython调用r函数,最常见python调用r函数的方式是使用rpy2模块。
The package is made of several sub-packages or modules:
Importing R packages is often the first step when running R code, and rpy2 is providing a function rpy2.robjects.packages.importr() that makes that step very similar to importing Python packages.
We mentioned earlier that rpy2 is running an embedded R. This is may be a little abstract, so there is an object rpy2.robjects.r to make it tangible.
The __getitem__() method of rpy2.robjects.r, gets the R object associated with a given symbol
The object r is also callable , and the string passed in a call is evaluated as R code.
An R object has a string representation that can be used directly into R code to be evaluated.
In R, data are mostly represented by vectors, even when looking like scalars. When looking closely at the R object pi used previously, we can observe that this is in fact a vector of length 1.
Creating R vectors can be achieved simply.
The easiest way to create such objects is to do it through R functions.
Calling R functions is disappointingly similar to calling Python functions.
By default, calling R functions return R objects.
Linear models
Creating an R vector or matrix, and filling its cells using Python code
This module should be the right pick for casual and general use. Its aim is to abstract some of the details and provide an intuitive interface to both Python and R programmers.
The instance can be seen as the entry point to an embedded R process. The elements that would be accessible from an equivalent R environment are accessible as attributes of the instance.
When safety matters most, we recommend using __getitem__() to get a given R object.
Storing the object in a python variable will protect it from garbage collection, even if deleted from the objects visible to an R user.
Just like it is the case with RPy-1.x, on-the-fly evaluation of R code contained in a string can be performed by calling the r instance.
The astute reader will quickly realize that R objects named by python variables can be plugged into code through their R representation.
R environments can be described to the Python user as an hybrid of a dictionary and a scope.
The first of all environments is called the Global Environment, that can also be referred to as the R workspace.
Assigning a value to a symbol in an environment has been made as simple as assigning a value to a key in a Python dictionary.
An environment is also iter-able, returning all the symbols (keys) it contains.
R functions exposed by rpy2's high-level interface can be used:
This is all looking fine and simple until R arguments with names such as na.rm are encountered. By default, this is addressed by having a translation of ‘.’ (dot) in the R argument name into a ‘_’ in the Python argument name.
In Python one can write:
R is capable of introspection, and can return the arguments accepted by a function through the function formals().
The method Function.rcall() is an alternative way to call an underlying R function.
For tasks such as modelling and plotting, an R formula can be a terse, yet readable, way of expressing what is wanted.
The class robjects.Formula is representing an R formula.
Other options are:
This is achieved by the R functions library() and require() (attaching the namespace of the package to the R search path).
Beside functions and environments, most of the objects an R user is interacting with are vector-like. For example, this means that any scalar is in fact a vector of length one.
The class Vector has a constructor:
Creating vectors can be achieved either from R or from Python.
When the vectors are created from R, one should not worry much as they will be exposed as they should by rpy2.robjects.
When one wants to create a vector from Python, either the class Vector or the convenience classes IntVector, FloatVector, BoolVector, StrVector can be used.
Extracting, Python-style
The python __getitem__() method behaves like a Python user would expect it for a vector (and indexing starts at zero).
Extracting, R-style
Access to R-style extracting/subsetting is granted though the two delegators rx and rx2, representing the R functions [ and [[ respectively.
Assigning, Python-style
Since vectors are exposed as Python mutable sequences, the assignment works as for regular Python lists.
In R vectors can be named, that is elements of the vector have a name.
Assigning, R-style
The attributes rx and rx2 used previously can again be used:
For the sake of complete compatibility with R, arguments can be named (and passed as a dict or rpy2.rlike.container.TaggedList).
In S/Splus/R special NA values can be used in a data vector to indicate that fact, and rpy2.robjects makes aliases for those available as data objects NA_Logical, NA_Real, NA_Integer, NA_Character, NA_Complex .
To expose that to Python, a delegating attribute ro is provided for vector-like objects.
R vectors can have a name given to all or some of the elements. The property names can be used to get, or set, those names.
Array
In R, arrays are simply vectors with a dimension attribute. That fact was reflected in the class hierarchy with robjects.Array inheriting from robjects.Vector.
Matrix
A Matrix is a special case of Array. As with arrays, one must remember that this is just a vector with dimension attributes (number of rows, number of columns).
DataFrame
In rpy2.robjects, DataFrame represents the R class data.frame.
Creating a DataFrame can be done by:
The DataFrame constructor accepts either an rinterface.SexpVector (with typeof equal to VECSXP, that is, an R list) or any Python object implementing the method items() (for example dict or rpy2.rlike.container.OrdDict).
To create a DataFrame and be certain of the clumn order order, an ordered dictionary can be used:
Here again, Python’s __getitem__() will work as a Python programmer will expect it to:
The DataFrame is composed of columns, with each column being possibly of a different type:
The approach followed in rpy2 has 2 levels (rinterface and robjects), and conversion functions help moving between them.
R vectors are mapped to Python objects implementing the methods __getitem__() / __setitem__() in the sequence protocol so elements can be accessed easily.
R functions are mapped to Python objects implementing the __call__() so they can be called just as if they were functions.
R environments are mapped to Python objects implementing __getitem__() / __setitem__() in the mapping protocol so elements can be accessed similarly to in a Python dict.
In its high-level interface rpy2 is using a conversion system that has the task of convertion objects between the following 3 representations: - lower-level interface to R (rpy2.rinterface level), - higher-level interface to R (rpy2.robjects level) - other (no rpy2) representations
R vectors or arrays can be converted to numpy arrays using numpy.array() or numpy.asarray().
The activation (and deactivation) of the automatic conversion of numpy objects into rpy2 objects can be made with:
Q4: R语言:DNA序列比对后计算遗传距离(P-distance)
在R语言中找到了计算遗传距离的函数dist.dna();但是不知道在R里面如何利用循环批量处理文件计算遗传距离。想到了利用python来调用R函数的方法,查找相关教程发现需要用到rpy2模块。
easy_install rpy2 报错(看不懂报错内容);
pip install rpy2 报错(提示需要更新pip到pip19.0.3);
利用 python -m pip install --upgrade pip 更新pip报错(看不懂报错内容);
利用 教程安装pip成功更新。
使用 pip install rpy2 安装依旧报错(看不懂报错内容);
尝试教程 安装rpy2,提示 rpy2-2.9.5-cp37-cp37m-win_amd64.whl is not a supported wheel on this platform
在rpy2主页 发现一句话 Releasend source packages are available on PyPi. Installing should be as easy * as
(*:except on Windows)
这意思是在windows系统使用pip安装不太容易吗?
找到了教程 rpy2:在python中调用R函数的一个实例 ;发现其中rpy2的安装使用的是conda,自己也尝试在windows的DOS窗口下使用 conda install rpy2 成功。但是结尾处提示了一句 此时不应有do 不明白是什么意思
在python中加载R包查到可以使用
加载R语言自带的包时没有遇到问题;但是加载需要额外安装的包时遇到了报错
按照教程 使用 conda install -c r r-ggplot2 安装需要用到的包解决问题
一个简便描述序列分歧大小的测度是两条核苷酸序列中不同核苷酸位点的比例 P = nd/n;
nd为检测两条序列间不同核苷酸数;n为配对总数;P成为核苷酸间的p距离
python调用r函数的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于python函数调用函数代码、python调用r函数的信息别忘了在本站进行查找喔。







