R语言常用函数(基本)

Python016

R语言常用函数(基本),第1张

vector:向量 numeric:数值型向量 logical:逻辑型向量 character;字符型向量 list:列表 data.frame:数据框

c:连接为向量或列表 sequence:等差序列 rep:重复

length:求长度 subset:求子集 seq,from:to, NA:缺失值 NULL:空对象 sort,order,unique,rev:排序 unlist:展平列表 attr,attributes:对象属性

mode,typeof:对象存储模式与类型 names:对象的名字属性

character:字符型向量 nchar:字符数 substr:取子串 format,formatC:把对象用格式转换为字符串 paste,strsplit:连接或拆分

charmatch,pmatch:字符串匹配 grep,sub,gsub:模式匹配与替换

complex,Re,Im,Mod,Arg,Conj:复数函数

factor:因子 codes:因子的编码 levels:因子的各水平的名字 nlevels:因子的水平个数 cut:把数值型对象分区间转换为因子

table:交叉频数表 split:按因子分组 aggregate:计算各数据子集的概括统计量 tapply:对“不规则”数组应用函数

+, -, *, /, ^, %%, %/%:四则运算 ceiling,floor,round,signif,trunc,zapsmall:舍入 max,min,pmax,pmin:最大最小值

range:最大值和最小值 sum,prod:向量元素和,积 cumsum,cumprod,cummax,cummin:累加、累乘 sort:排序 approx和approx fun:插值 diff:差分 sign:符号函数

abs,sqrt:绝对值,平方根 log, exp, log10, log2:对数与指数函数 sin,cos,tan,asin,acos,atan,atan2:三角函数

sinh,cosh,tanh,asinh,acosh,atanh:双曲函数

beta,lbeta,gamma,lgamma,digamma,trigamma,tetragamma,pentagamma,choose ,lchoose:与贝塔函数、伽玛函数、组合数有关的特殊函数

fft,mvfft,convolve:富利叶变换及卷积 polyroot:多项式求根 poly:正交多项式 spline,splinefun:样条差值

besselI,besselK,besselJ,besselY,gammaCody:Bessel函数 deriv:简单表达式的符号微分或算法微分

array:建立数组 matrix:生成矩阵 data.matrix:把数据框转换为数值型矩阵 lower.tri:矩阵的下三角部分 mat.or.vec:生成矩阵或向量 t:矩阵转置

cbind:把列合并为矩阵 rbind:把行合并为矩阵 diag:矩阵对角元素向量或生成对角矩阵 aperm:数组转置 nrow, ncol:计算数组的行数和列数 dim:对象的维向量

dimnames:对象的维名 row/colnames:行名或列名 %*%:矩阵乘法 crossprod:矩阵交叉乘积(内积) outer:数组外积 kronecker:数组的Kronecker积

apply:对数组的某些维应用函数 tapply:对“不规则”数组应用函数 sweep:计算数组的概括统计量 aggregate:计算数据子集的概括统计量 scale:矩阵标准化

matplot:对矩阵各列绘图 cor:相关阵或协差阵 Contrast:对照矩阵 row:矩阵的行下标集 col:求列下标集

solve:解线性方程组或求逆 eigen:矩阵的特征值分解 svd:矩阵的奇异值分解 backsolve:解上三角或下三角方程组 chol:Choleski分解

qr:矩阵的QR分解 chol2inv:由Choleski分解求逆

<,>,<=,>=,==,!=:比较运算符 !,&,&&,|,||,xor():逻辑运算符 logical:生成逻辑向量 all,any:逻辑向量都为真或存在真

ifelse():二者择一 match,%in%:查找 unique:找出互不相同的元素 which:找到真值下标集合 duplicated:找到重复元素

optimize,uniroot,polyroot:一维优化与求根

if,else,ifelse,switch:分支 for,while,repeat,break,next:循环 apply,lapply,sapply,tapply,sweep:替代循环的函数。

function:函数定义 source:调用文件 call:函数调用 .C,.Fortran:调用C或者Fortran子程序的动态链接库。 Recall:递归调用

browser,debug,trace,traceback:程序调试 options:指定系统参数 missing:判断虚参是否有对应实参 nargs:参数个数 stop:终止函数执行

on.exit:指定退出时执行 eval,expression:表达式计算 system.time:表达式计算计时 invisible:使变量不显示 menu:选择菜单(字符列表菜单)

其它与函数有关的还有:delay,delete.response,deparse,do.call,dput,environment ,,formals,format.info,interactive,

is.finite,is.function,is.language,is.recursive ,match.arg,match.call,match.fun,model.extract,name,parse,substitute,sys.parent ,warning,machine

cat,print:显示对象 sink:输出转向到指定文件 dump,save,dput,write:输出对象 scan,read.table,load,dget:读入

ls,objects:显示对象列表 rm, remove:删除对象 q,quit:退出系统 .First,.Last:初始运行函数与退出运行函数。

options:系统选项 ?,help,help.start,apropos:帮助功能 data:列出数据集分析

每一种分布有四个函数:d――density(密度函数),p――分布函数,q――分位数函数,r――随机数函数。

比如,正态分布的这四个函数为dnorm,pnorm,qnorm,rnorm。下面我们列出各分布后缀,前面加前缀d、p、q或r就构成函数名:

norm:正态,t:t分布,f:F分布,chisq:卡方(包括非中心) unif:均匀,exp:指数,weibull:威布尔,gamma:伽玛,beta:贝塔

lnorm:对数正态,logis:逻辑分布,cauchy:柯西, binom:二项分布,geom:几何分布,hyper:超几何,nbinom:负二项,pois:泊松 signrank:符号秩,

wilcox:秩和,tukey:学生化极差

sum, mean, var, sd, min, max, range, median, IQR(四分位间距)等为统计量,sort,order,rank与排序有关,其它还有ave,fivenum,mad,quantile,stem等。

R中已实现的有chisq.test,prop.test,t.test。

cor,cov.wt,var:协方差阵及相关阵计算 biplot,biplot.princomp:多元数据biplot图 cancor:典则相关 princomp:主成分分析 hclust:谱系聚类

kmeans:k-均值聚类 cmdscale:经典多维标度 其它有dist,mahalanobis,cov.rob。

ts:时间序列对象 diff:计算差分 time:时间序列的采样时间 window:时间窗

lm,glm,aov:线性模型、广义线性模型、方差

在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:

形容词:适合正式场合的;正规的;庄重的;正式的;合乎规矩的

名词:<美>须穿礼服的社交集会;<口>夜礼服

读音:英 [ˈfɔːml]   美 [ˈfɔːrml]

复数: formals

派生词: formally

扩展资料:

词根

form= to form, shape 形成;形状, 形式

aquiformadj. 水状的

aqui〔= aqu 水〕+ form 形成;形状,形式 → 水状的

conformv. 使一致;遵从

con 共同 + form 形成;形状,形式 → 共同的形式 → 使一致

cruciformadj. 十字形的

cruci 十字形 + form 形成;形状,形式 → 十字形的

deformv. 使残废;变形

de 坏 + form 形成;形状,形式 → 破坏形状 → 变形