找回密码
 注册
查看: 1978|回复: 0

R语言:Extract()函数中文帮助文档(中英文对照)

[复制链接]
发表于 2012-2-16 19:11:33 | 显示全部楼层 |阅读模式
Extract(base)
Extract()所属R语言包:base

                                        Extract or Replace Parts of an Object
                                         提取或更换部件的对象

                                         译者:生物统计家园网 机器人LoveR

描述----------Description----------

Operators acting on vectors, matrices, arrays and lists to extract or replace parts.
向量,矩阵,数组和列表的运营商中提取或更换零件。


用法----------Usage----------


x[i]
x[i, j, ... , drop = TRUE]
x[[i, exact = TRUE]]
x[[i, j, ..., exact = TRUE]]
x$name
getElement(object, name)

x[i] <- value
x[i, j, ...] <- value
x[[i]] <- value
x$i <- value



参数----------Arguments----------

参数:x, object
object from which to extract element(s) or in which to replace element(s).  
从中提取元素(S)或在替换元素(S)的对象。


参数:i, j, ...
indices specifying elements to extract or replace.  Indices are numeric or character vectors or empty (missing) or NULL.  Numeric values are coerced to integer as by as.integer (and hence truncated towards zero). Character vectors will be matched to the names of the object (or for matrices/arrays, the dimnames): see "Character indices" below for further details.  For [-indexing only: i, j, ... can be logical vectors, indicating elements/slices to select.  Such vectors are recycled if necessary to match the corresponding extent. i, j, ... can also be negative integers, indicating elements/slices to leave out of the selection.  When indexing arrays by [ a single argument i can be a matrix with as many columns as there are dimensions of x; the result is then a vector with elements corresponding to the sets of indices in each row of i.  An index value of NULL is treated as if it were integer(0).  
指数指定元素提取或更换。指数是numeric或character向量或空(失踪)或NULL。数值强制转换为整数,如as.integer(从而截断向零)。将匹配字符向量names对象(或矩阵/阵列,dimnames):详见“特征指标”下面。只为[索引:i,j,...可以是逻辑向量,显示元件/片选择。这种向量被回收,如果要匹配相应的程度。 i,j,...也可以是负整数,指示元素/片的选择离开。当索引数组[一个参数i可以是一个矩阵,多列,有x尺寸,其结果是那么相应的指数集的元素的向量在每个i行。 NULL指数值被视为是integer(0)。


参数:name
A literal character string or a name (possibly backtick quoted).  For extraction, this is normally (see under "Environments") partially matched to the names of the object.  
文字字符串或名称(可能是反引号引述)。提取,这是正常(见“环境”下)部分匹配names对象。


参数:drop
For matrices and arrays.  If TRUE the result is coerced to the lowest possible dimension (see the examples).  This only works for extracting elements, not for the replacement.  See drop for further details.  
矩阵和数组。如果TRUE结果被裹挟到可能的最低尺寸(见例子)。这仅适用于提取元素,而不是更换。看到drop作进一步的细节。


参数:exact
Controls possible partial matching of [[ when extracting by a character vector (for most objects, but see under "Environments").  The default is no partial matching.  Value NA allows partial matching but issues a warning when it occurs.  Value FALSE allows partial matching without any warning.
控制[[部分匹配时,由特征向量(对于大多数对象,但根据“环境”)中提取。默认是没有部分匹配。值NA允许部分匹配,但发生时发出警告。值FALSE允许部分匹配,没有任何警告。


参数:value
typically an array-like R object of a similar class as x.
通常是一个类似数组的R作为x同级的对象。


Details

详情----------Details----------

These operators are generic.  You can write methods to handle indexing of specific classes of objects, see InternalMethods as well as [.data.frame and [.factor.  The descriptions here apply only to the default methods.  Note that separate methods are required for the replacement functions [<-, [[<- and $<- for use when indexing occurs on the assignment side of an expression.
这些运营商都是通用的。你可以写的方法来处理特定的类的对象的索引,看到InternalMethods以及[.data.frame和[.factor。这里的描述仅适用于默认方法。需要注意的是单独的方法需要更换功能[<-,[[<-和$<-时使用索引表达式的转让方出现。

The most important distinction between [, [[ and $ is that the [ can select more than one element whereas the other two select a single element.
最重要的区别[,[[和$是[可以选择一个以上的元素,而另外两个选择一个单一的元素。

The default methods work somewhat differently for atomic vectors, matrices/arrays and for recursive (list-like, see is.recursive) objects.  $ is only valid for recursive objects, and is only discussed in the section below on recursive objects.  Its use on non-recursive objects was deprecated in R 2.5.0 and removed in R 2.7.0.
默认的方法有所不同工作原子的向量,矩阵/阵列和递归(列表一样,看到is.recursive)对象。 $递归的对象仅仅是有效的,并且只在递归对象的下面一节讨论。其使用非递归的对象被废弃在R 2.5.0和删除在&#341;2.7.0。

Subsetting (except by an empty index) will drop all attributes except names, dim and dimnames.
子集(空指数除外)将下降除了names,dim和dimnames所有属性。

Indexing can occur on the right-hand-side of an expression for extraction, or on the left-hand-side for replacement.  When an index expression appears on the left side of an assignment (known as subassignment) then that part of x is set to the value of the right hand side of the assignment.  In this case no partial matching of character indices is done, and the left-hand-side is coerced as needed to accept the values.  Attributes are preserved (although names, dim and dimnames will be adjusted suitably).  Subassignment is done sequentially, so if an index is specified more than once the latest assigned value for an index will result.
索引可以出现在右手边的提取,或更换的左手端表达。当一个分配的左侧(subassignment的x设置为的转让右侧的价值部分),然后出现一个索引表达式。在这种情况下没有字符索引部分匹配完成后,左手端被强迫为需要接受的价值观。属性会被保留(虽然names,dim和dimnames将调整适当)。 subassignment依次完成,因此,如果索引指定不止一次的最新分配一个索引值,将导致。

It is an error to apply any of these operators to an object which is not subsettable (e.g. a function).
这是一个错误,适用于任何一个对象,这不是subsettable(例如函数),这些运营商。


原子向量----------Atomic vectors----------

The usual form of indexing is "[".  "[[" can be used to select a single element dropping names, whereas "[" keeps them, e.g., in c(abc = 123)[1].
索引通常的形式是"["。 "[["可用于选择一个单一的元素下降names,而"["让他们,例如,在c(abc = 123)[1]。

The index object i can be numeric, logical, character or empty. Indexing by factors is allowed and is equivalent to indexing by the numeric codes (see factor) and not by the character values which are printed (for which use [as.character(i)]).
索引对象i可以是数字,逻辑,字符或空。索引的因素是允许的,是同等的数字代码索引(见factor),而不是打印的字符值(使用[as.character(i)])。

An empty index selects all values: this is most often used to replace all the entries but keep the attributes.
一个空的索引选择的所有值:这是最常用的,以取代所有的条目,但保持了attributes。


矩阵和数组----------Matrices and arrays----------

Matrices and arrays are vectors with a dimension attribute and so all the vector forms of indexing can be used with a single index.  The result will be an unnamed vector unless x is one-dimensional when it will be a one-dimensional array.
矩阵和数组维度属性的向量和所有索引的矢量形式,可以使用一个单一的指数。其结果将是一位不愿透露姓名的向量除非x是一维时,这将是一个一维数组。

The most common form of indexing a k-dimensional array is to specify k indices to [.  As for vector indexing, the indices can be numeric, logical, character, empty or even factor. An empty index (a comma separated blank) indicates that all entries in that dimension are selected. The argument drop applies to this form of indexing.
最常见的形式是一个k维数组的索引指定k[指数。指数为向量索引,可以是数字,逻辑,性格,甚至空因素。空指数(以逗号分隔的空白)表示,在该维度的所有条目被选中。参数drop适用于这种形式的索引。

A third form of indexing is via a numeric matrix with the one column for each dimension: each row of the index matrix then selects a single element of the array, and the result is a vector.  Negative indices are not allowed in the index matrix.  NA and zero values are allowed: rows of an index matrix containing a zero are ignored, whereas rows containing an NA produce an NA in the result.
一个索引的第三种形式是:通过与每个维度的一列数字矩阵的指数矩阵的每一行,然后选择一个数组的单个元素,其结果是一个向量。中不允许使用索引矩阵负指数。 NA“零值允许索引包含一个零矩阵的行被忽略,而行包含一个NA产生NA的结果。

Indexing via a character matrix with one column per dimensions is also supported if the array has dimension names.  As with numeric matrix indexing, each row of the index matrix selects a single element of the array.  Indices are matched against the appropriate dimension names. NA is allowed and will produce an NA in the result. Unmatched indices as well as the empty string ("") are not allowed and will result in an error.
如果数组维度名称,还支持通过与每列尺寸的字符矩阵索引。至于与数字矩阵索引,指数矩阵的每一行选择一个数组的单个元素。适当的尺寸名称相匹配指数。 NA是允许的,会产生NA结果。无与伦比的指数以及空字符串("")不允许将导致错误。

A vector obtained by matrix indexing will be unnamed unless x is one-dimensional when the row names (if any) will be indexed to provide names for the result.
矩阵索引得到一个向量将无名x除非是一维的行名称(如有)将索引提供结果的名称。


递归(列表)对象----------Recursive (list-like) objects----------

Indexing by [ is similar to atomic vectors and selects a list of the specified element(s).
[索引类似原子的向量,并选择指定的元素的列表(S)。

Both [[ and $ select a single element of the list.  The main difference is that $ does not allow computed indices, whereas [[ does.  x$name is equivalent to x[["name", exact = FALSE]].  Also, the partial matching behavior of [[ can be controlled using the exact argument.
既[[和$选择列表中的单个元素。它们的主要区别是$不计算指数,而[[不。 x$name相当于x[["name", exact = FALSE]]的。此外,部分匹配[[行为可以使用exact参数控制。

getElement(x, name) is a version of x[[name, exact = TRUE]] which for formally classed (S4) objects returns slot(x, name), hence providing access to even more general list-like objects.
getElement(x, name)x[[name, exact = TRUE]](S4),正式归入对象返回slot(x, name),从而提供更一般的列表对象的访问是一个版本。

[ and [[ are sometimes applied to other recursive objects such as calls and expressions.  Pairlists are coerced to lists for extraction by [, but all three operators can be used for replacement.
[和[[有时也适用于其他如电话和表达式的递归对象。被强制到列表pairlists提取[的,但所有三家运营商可以更换。

[[ can be applied recursively to lists, so that if the single index i is a vector of length p, alist[[i]] is equivalent to alist[[i1]]...[[ip]] providing all but the final indexing results in a list.
[[可以递归应用到列表,因此,如果单指数i是一个长度为向量p,alist[[i]]是相当于alist[[i1]]...[[ip]]提供所有的,但索引列表中的最后结果。

Note that in all three kinds of replacement, a value of NULL deletes the corresponding item of the list.  To set entries to NULL, you need x[i] <- list(NULL).
请注意,在所有3种更换,NULL的名单中删除相应的项目的价值。要设置项NULL,你需要x[i] <- list(NULL)。

When $<- is applied to a NULL x, it first coerces x to list().  This is what also happens with [[<- if the replacement value value is of length greater than one: if value has length 1 or 0, x is first coerced to a zero-length vector of the type of value.
当$<-应用NULLx,它首先胁迫xlist()。这是什么还用发生[[<-如果替代值value长度大于一:如果value长度为1或0,x第一次被强迫为零长度的value类型向量。


环境----------Environments----------

Both $ and [[ can be applied to environments.  Only character indices are allowed and no partial matching is done.  The semantics of these operations are those of get(i, env=x,     inherits=FALSE).  If no match is found then NULL is returned.  The replacement versions, $<- and [[<-, can also be used.  Again, only character arguments are allowed.  The semantics in this case are those of assign(i, value, env=x,     inherits=FALSE).  Such an assignment will either create a new binding or change the existing binding in x.
既$和[[可应用于环境。只允许字符指数并没有完成部分匹配。这些操作的语义是那些get(i, env=x,     inherits=FALSE)。如果没有找到匹配的,那么NULL返回。更换版本,$<-和[[<-,也可以使用。再次,只有字符参数是允许的。在这种情况下,语义是那些assign(i, value, env=x,     inherits=FALSE)。这种转让将创建一个新的绑定或改变x的现有约束力。


NAS在索引----------NAs in indexing----------

When extracting, a numerical, logical or character NA index picks an unknown element and so returns NA in the corresponding element of a logical, integer, numeric, complex or character result, and NULL for a list.  (It returns 00 for a raw result.]
提取时,数值,逻辑或字符NA指数挑选一个未知的元素,所以返回NA在相应的元素的逻辑,整数,数字,复杂的或字符的结果,NULL一个列表。 (它返回00为原料的结果。]

When replacing (that is using indexing on the lhs of an assignment) NA does not select any element to be replaced.  As there is ambiguity as to whether an element of the rhs should be used or not, this is only allowed if the rhs value is of length one (so the two interpretations would have the same outcome).
当更换(使用索引转让的LHS)NA不选择任何元素被替换。由于存在歧义rhs的元素是否应使用或不,这仅仅是允许的,如果rhs的值是一个长度(两种解释,将有相同的结果)。


参数匹配----------Argument matching----------

Note that these operations do not match their index arguments in the standard way: argument names are ignored and positional matching only is used.  So m[j=2,i=1] is equivalent to m[2,1] and <STRONG>not</STRONG> to m[1,2].
请注意,这些操作不符合标准的指标参数:参数名称被忽略,并且只用于定位匹配。所以m[j=2,i=1]相当于m[2,1]和<strong> </强>m[1,2]。

This may not be true for methods defined for them; for example it is not true for the data.frame methods described in [.data.frame which warn if i or j is named and have undocumented behaviour in that case.
这可能不是真正为他们定义的方法,例如它是不是真正的data.frame[.data.frame这描述的方法警告如果i或j被命名为在这种情况下无证行为。

To avoid confusion, do not name index arguments (but drop and exact must be named).
为了避免混乱,没有名称的索引参数(但drop和exact必须命名)。


S4方法----------S4 methods----------

These operators are also implicit S4 generics, but as primitives, S4 methods will be dispatched only on S4 objects x.
这些运营商也隐含的S4泛型,但作为原语,S4方法将只派出S4对象x。

The implicit generics for the $ and $<- operators do not have name in their signature because the grammar only allows symbols or string constants for the name argument.
隐含的仿制药$和$<-运营商没有name他们的签名,因为语法只允许符号或字符串常量name参数。


性格指数----------Character indices----------

Character indices can in some circumstances be partially matched (see pmatch) to the names or dimnames of the object being subsetted (but never for subassignment).  Unlike S (Becker et al p. 358)), R has never used partial matching when extracting by [, and as from R 2.7.0 partial matching is not by default used by [[ (see argument exact).
字符索引,可以在某些情况下,部分匹配(见pmatch)子集(但从未subassignment)的对象的名称或dimnames。与小号(贝克尔等。358)),R已从未使用部分匹配时,提取[,从R 2.7.0部分匹配默认情况下是不[[(见参数exact)。

Thus the default behaviour is to use partial matching only when extracting from recursive objects (except environments) by $. Even in that case, warnings can be switched on by options(warnPartialMatchAttr = TRUE).
因此,默认行为是使用部分匹配,只有当递归对象(环境除外)$提取。即使在这种情况下,警告,可以打开options(warnPartialMatchAttr = TRUE)。

Neither empty ("") nor NA indices match any names, not even empty nor missing names.  If any object has no names or appropriate dimnames, they are taken as all "" and so match nothing.
既不为空("")也NA指数匹配任何名字,甚至没有空也没有丢失的名字。如果有任何对象没有名称的或适当dimnames的,他们采取了所有的""等匹配无关。


注意----------Note----------

The documented behaviour of S is that an NA replacement index "goes nowhere" but uses up an element of value (Becker et al p. 359).  However, that has not been true of other implementations.
S的记录行为是一个NA替代指数“死胡同”,而是使用了一个value(Becker等人第359页)的元素。但是,一直没有真正的其他实现。


参考文献----------References----------

The New S Language. Wadsworth &amp; Brooks/Cole.

参见----------See Also----------

names for details of matching to names, and pmatch for partial matching.
names的名称相匹配的细节,和pmatch部分匹配。

list, array, matrix.
list,array,matrix。

[.data.frame and [.factor for the behaviour when applied to data.frame and factors.
[.data.frame和[.factor行为时,适用于数据框和因素。

Syntax for operator precedence, and the R Language reference manual about indexing details.
Syntax运算符优先级和R语言参考手册中关于索引的详细信息。


举例----------Examples----------


x <- 1:12
m <- matrix(1:6, nrow=2, dimnames=list(c("a", "b"), LETTERS[1:3]))
li <- list(pi=pi, e = exp(1))
x[10]                 # the tenth element of x[x的第十个元素]
x &lt;- x[-1]            # delete the 1st element of x[删除第一个元素的x]
m[1,]                 # the first row of matrix m[第一行的矩阵m]
m[1, , drop = FALSE]  # is a 1-row matrix[是一个1行矩阵]
m[,c(TRUE,FALSE,TRUE)]# logical indexing[逻辑索引]
m[cbind(c(1,2,1),3:1)]# matrix numeric index[矩阵数字索引]
ci <- cbind(c("a", "b", "a"), c("A", "C", "B"))
m[ci]                 # matrix character index[矩阵特征指数]
m &lt;- m[,-1]           # delete the first column of m[删除第一列的m]
li[[1]]               # the first element of list li[李列表的第一个元素]
y <- list(1,2,a=4,5)
y[c(3,4)]             # a list containing elements 3 and 4 of y[一个列表,其中包含y的元素3和4]
y$a                   # the element of y named a[y的元素命名]

## non-integer indices are truncated:[非整数的指数将被截断:]
(i &lt;- 3.999999999) # "4" is printed[“4”印]
(1:5)[i]  # 3[3]

## named atomic vectors, compare "[" and "[[" :[#命名原子向量,比较“[”和“[”:]
nx <- c(Abc = 123, pi = pi)
nx[1] ; nx["pi"] # keeps names, whereas "[[" does not:[保持名称,而“[”不]
nx[[1]] ; nx[["pi"]]

## recursive indexing into lists[#递归索引成列表]
z <- list( a=list( b=9, c='hello'), d=1:5)
unlist(z)
z[[c(1, 2)]]
z[[c(1, 2, 1)]]  # both "hello"[双方的“Hello”]
z[[c("a", "b")]] <- "new"
unlist(z)

## check $ and [[ for environments[检查#$ [环境]
e1 <- new.env()
e1$a <- 10
e1[["a"]]
e1[["b"]] <- 20
e1$b
ls(e1)

转载请注明:出自 生物统计家园网(http://www.biostatistic.net)。


注:
注1:为了方便大家学习,本文档为生物统计家园网机器人LoveR翻译而成,仅供个人R语言学习参考使用,生物统计家园保留版权。
注2:由于是机器人自动翻译,难免有不准确之处,使用时仔细对照中、英文内容进行反复理解,可以帮助R语言的学习。
注3:如遇到不准确之处,请在本贴的后面进行回帖,我们会逐渐进行修订。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|生物统计家园 网站价格

GMT+8, 2025-1-24 08:40 , Processed in 0.027457 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表