| 42333 |
ripley |
1 |
% File src/library/base/man/Extract.Rd
|
| 68948 |
ripley |
2 |
% Part of the R package, https://www.R-project.org
|
| 70832 |
maechler |
3 |
% Copyright 1995-2016 R Core Team
|
| 61130 |
maechler |
4 |
% Copyright 2002-2012 The R Foundation
|
| 42333 |
ripley |
5 |
% Distributed under GPL 2 or later
|
|
|
6 |
|
| 2 |
r |
7 |
\name{Extract}
|
|
|
8 |
\title{Extract or Replace Parts of an Object}
|
| 56186 |
murdoch |
9 |
\alias{Extract}
|
| 2 |
r |
10 |
\alias{Subscript}
|
|
|
11 |
\alias{[}
|
| 42090 |
maechler |
12 |
\alias{[.listof}
|
|
|
13 |
\alias{[.simple.list}
|
| 65708 |
maechler |
14 |
\alias{[.Dlist}
|
| 2 |
r |
15 |
\alias{[[}
|
|
|
16 |
\alias{$}
|
| 56724 |
maechler |
17 |
\alias{getElement}
|
| 1293 |
ihaka |
18 |
\alias{[<-}
|
|
|
19 |
\alias{[[<-}
|
|
|
20 |
\alias{$<-}
|
| 28509 |
murdoch |
21 |
\concept{delete}
|
| 59927 |
ripley |
22 |
|
| 2 |
r |
23 |
\description{
|
| 34719 |
ripley |
24 |
Operators acting on vectors, matrices, arrays and lists to extract or
|
| 36397 |
ripley |
25 |
replace parts.
|
| 7081 |
pd |
26 |
}
|
| 59927 |
ripley |
27 |
|
| 14506 |
hornik |
28 |
\usage{
|
|
|
29 |
x[i]
|
| 30915 |
ripley |
30 |
x[i, j, \dots , drop = TRUE]
|
| 42981 |
ripley |
31 |
x[[i, exact = TRUE]]
|
|
|
32 |
x[[i, j, \dots, exact = TRUE]]
|
| 14506 |
hornik |
33 |
x$name
|
| 56724 |
maechler |
34 |
getElement(object, name)
|
| 51231 |
maechler |
35 |
|
|
|
36 |
x[i] <- value
|
|
|
37 |
x[i, j, \dots] <- value
|
| 55419 |
maechler |
38 |
x[[i]] <- value
|
| 70832 |
maechler |
39 |
x$name <- value
|
| 14506 |
hornik |
40 |
}
|
| 59927 |
ripley |
41 |
|
| 14426 |
tlumley |
42 |
\arguments{
|
| 56751 |
maechler |
43 |
\item{x, object}{
|
| 36397 |
ripley |
44 |
object from which to extract element(s) or in which to replace element(s).
|
| 32600 |
ripley |
45 |
}
|
| 41476 |
ripley |
46 |
\item{i, j, \dots}{
|
|
|
47 |
indices specifying elements to extract or replace. Indices are
|
| 37890 |
ripley |
48 |
\code{numeric} or \code{character} vectors or empty (missing) or
|
| 41476 |
ripley |
49 |
\code{NULL}. Numeric values are coerced to integer as by
|
| 43377 |
ripley |
50 |
\code{\link{as.integer}} (and hence truncated towards zero).
|
|
|
51 |
Character vectors will be matched to the \code{\link{names}} of the
|
| 43383 |
ripley |
52 |
object (or for matrices/arrays, the \code{\link{dimnames}}):
|
|
|
53 |
see \sQuote{Character indices} below for further details.
|
| 33741 |
maechler |
54 |
|
| 49612 |
hornik |
55 |
For \code{[}-indexing only: \code{i}, \code{j}, \code{\dots} can be
|
|
|
56 |
logical vectors, indicating elements/slices to select. Such vectors
|
|
|
57 |
are recycled if necessary to match the corresponding extent.
|
|
|
58 |
\code{i}, \code{j}, \code{\dots} can also be negative integers,
|
|
|
59 |
indicating elements/slices to leave out of the selection.
|
| 33741 |
maechler |
60 |
|
| 36397 |
ripley |
61 |
When indexing arrays by \code{[} a single argument \code{i} can be a
|
|
|
62 |
matrix with as many columns as there are dimensions of \code{x}; the
|
|
|
63 |
result is then a vector with elements corresponding to the sets of
|
|
|
64 |
indices in each row of \code{i}.
|
| 37890 |
ripley |
65 |
|
|
|
66 |
An index value of \code{NULL} is treated as if it were \code{integer(0)}.
|
| 34719 |
ripley |
67 |
}
|
| 41476 |
ripley |
68 |
\item{name}{
|
|
|
69 |
A literal character string or a \link{name} (possibly \link{backtick}
|
| 43383 |
ripley |
70 |
quoted). For extraction, this is normally (see under
|
|
|
71 |
\sQuote{Environments}) partially matched to the \code{\link{names}}
|
|
|
72 |
of the object.
|
| 41476 |
ripley |
73 |
}
|
| 36397 |
ripley |
74 |
\item{drop}{For matrices and arrays. If \code{TRUE} the result is
|
|
|
75 |
coerced to the lowest possible dimension (see the examples). This
|
| 43878 |
ripley |
76 |
only works for extracting elements, not for the replacement. See
|
|
|
77 |
\code{\link{drop}} for further details.
|
| 41675 |
rgentlem |
78 |
}
|
| 43377 |
ripley |
79 |
\item{exact}{Controls possible partial matching of \code{[[} when
|
|
|
80 |
extracting by a character vector (for most objects, but see under
|
| 43383 |
ripley |
81 |
\sQuote{Environments}). The default is no partial matching. Value
|
|
|
82 |
\code{NA} allows partial matching but issues a warning when it
|
|
|
83 |
occurs. Value \code{FALSE} allows partial matching without any
|
|
|
84 |
warning.}
|
| 51231 |
maechler |
85 |
\item{value}{typically an array-like \R object of a similar class as
|
|
|
86 |
\code{x}.}
|
| 14506 |
hornik |
87 |
}
|
| 59927 |
ripley |
88 |
|
| 7081 |
pd |
89 |
\details{
|
| 36397 |
ripley |
90 |
These operators are generic. You can write methods to handle indexing
|
| 24878 |
ripley |
91 |
of specific classes of objects, see \link{InternalMethods} as well as
|
|
|
92 |
\code{\link{[.data.frame}} and \code{\link{[.factor}}. The
|
| 36374 |
ripley |
93 |
descriptions here apply only to the default methods. Note that
|
|
|
94 |
separate methods are required for the replacement functions
|
|
|
95 |
\code{[<-}, \code{[[<-} and \code{$<-} for use when indexing occurs on
|
|
|
96 |
the assignment side of an expression.
|
| 24878 |
ripley |
97 |
|
|
|
98 |
The most important distinction between \code{[}, \code{[[} and
|
|
|
99 |
\code{$} is that the \code{[} can select more than one element whereas
|
| 34719 |
ripley |
100 |
the other two select a single element.
|
| 33741 |
maechler |
101 |
|
| 34719 |
ripley |
102 |
The default methods work somewhat differently for atomic vectors,
|
|
|
103 |
matrices/arrays and for recursive (list-like, see
|
| 42889 |
ripley |
104 |
\code{\link{is.recursive}}) objects. \code{$} is only valid for
|
|
|
105 |
recursive objects, and is only discussed in the section below on
|
| 57520 |
ripley |
106 |
recursive objects.
|
| 24878 |
ripley |
107 |
|
| 38635 |
ripley |
108 |
Subsetting (except by an empty index) will drop all attributes except
|
| 38872 |
ripley |
109 |
\code{names}, \code{dim} and \code{dimnames}.
|
| 38635 |
ripley |
110 |
|
| 34719 |
ripley |
111 |
Indexing can occur on the right-hand-side of an expression for
|
| 42474 |
ripley |
112 |
extraction, or on the left-hand-side for replacement. When an index
|
| 38635 |
ripley |
113 |
expression appears on the left side of an assignment (known as
|
|
|
114 |
\emph{subassignment}) then that part of \code{x} is set to the value
|
|
|
115 |
of the right hand side of the assignment. In this case no partial
|
| 41697 |
ripley |
116 |
matching of character indices is done, and the left-hand-side is
|
| 63324 |
ripley |
117 |
coerced as needed to accept the values. For vectors, the answer will
|
|
|
118 |
be of the higher of the types of \code{x} and \code{value} in the
|
|
|
119 |
hierarchy raw < logical < integer < double < complex < character <
|
|
|
120 |
list < expression. Attributes are preserved (although \code{names},
|
|
|
121 |
\code{dim} and \code{dimnames} will be adjusted suitably).
|
|
|
122 |
Subassignment is done sequentially, so if an index is specified more
|
|
|
123 |
than once the latest assigned value for an index will result.
|
| 51231 |
maechler |
124 |
|
| 46117 |
ripley |
125 |
It is an error to apply any of these operators to an object which is
|
| 66444 |
hornik |
126 |
not subsettable (e.g., a function).
|
| 34719 |
ripley |
127 |
}
|
| 59927 |
ripley |
128 |
|
| 34719 |
ripley |
129 |
\section{Atomic vectors}{
|
| 62073 |
ripley |
130 |
The usual form of indexing is \code{[}. \code{[[} can be used to
|
| 51231 |
maechler |
131 |
select a single element \emph{dropping} \code{\link{names}}, whereas
|
| 62073 |
ripley |
132 |
\code{[} keeps them, e.g., in \code{c(abc = 123)[1]}.
|
| 2 |
r |
133 |
|
| 34719 |
ripley |
134 |
The index object \code{i} can be numeric, logical, character or empty.
|
| 22865 |
ripley |
135 |
Indexing by factors is allowed and is equivalent to indexing by the
|
|
|
136 |
numeric codes (see \code{\link{factor}}) and not by the character
|
|
|
137 |
values which are printed (for which use \code{[as.character(i)]}).
|
|
|
138 |
|
| 34720 |
ripley |
139 |
An empty index selects all values: this is most often used to replace
|
| 34719 |
ripley |
140 |
all the entries but keep the \code{\link{attributes}}.
|
|
|
141 |
}
|
| 59927 |
ripley |
142 |
|
| 34719 |
ripley |
143 |
\section{Matrices and arrays}{
|
|
|
144 |
Matrices and arrays are vectors with a dimension attribute and so all
|
|
|
145 |
the vector forms of indexing can be used with a single index. The
|
|
|
146 |
result will be an unnamed vector unless \code{x} is one-dimensional
|
|
|
147 |
when it will be a one-dimensional array.
|
| 9075 |
rgentlem |
148 |
|
| 34719 |
ripley |
149 |
The most common form of indexing a \eqn{k}-dimensional array is to
|
|
|
150 |
specify \eqn{k} indices to \code{[}. As for vector indexing, the
|
|
|
151 |
indices can be numeric, logical, character, empty or even factor.
|
| 76148 |
maechler |
152 |
And again, indexing by factors is equivalent to indexing by the
|
|
|
153 |
numeric codes, see \sQuote{Atomic vectors} above.
|
|
|
154 |
|
| 34719 |
ripley |
155 |
An empty index (a comma separated blank) indicates that all entries in
|
|
|
156 |
that dimension are selected.
|
|
|
157 |
The argument \code{drop} applies to this form of indexing.
|
| 39797 |
maechler |
158 |
|
| 34719 |
ripley |
159 |
A third form of indexing is via a numeric matrix with the one column
|
|
|
160 |
for each dimension: each row of the index matrix then selects a single
|
| 36397 |
ripley |
161 |
element of the array, and the result is a vector. Negative indices are
|
|
|
162 |
not allowed in the index matrix. \code{NA} and zero values are allowed:
|
| 34719 |
ripley |
163 |
rows of an index matrix containing a zero are ignored, whereas rows
|
|
|
164 |
containing an \code{NA} produce an \code{NA} in the result.
|
|
|
165 |
|
| 50896 |
falcon |
166 |
Indexing via a character matrix with one column per dimensions is also
|
|
|
167 |
supported if the array has dimension names. As with numeric matrix
|
|
|
168 |
indexing, each row of the index matrix selects a single element of the
|
|
|
169 |
array. Indices are matched against the appropriate dimension names.
|
|
|
170 |
\code{NA} is allowed and will produce an \code{NA} in the result.
|
|
|
171 |
Unmatched indices as well as the empty string (\code{""}) are not
|
|
|
172 |
allowed and will result in an error.
|
|
|
173 |
|
| 36397 |
ripley |
174 |
A vector obtained by matrix indexing will be unnamed unless \code{x}
|
|
|
175 |
is one-dimensional when the row names (if any) will be indexed to
|
|
|
176 |
provide names for the result.
|
| 34719 |
ripley |
177 |
}
|
| 59927 |
ripley |
178 |
|
| 36397 |
ripley |
179 |
\section{Recursive (list-like) objects}{
|
| 34719 |
ripley |
180 |
Indexing by \code{[} is similar to atomic vectors and selects a list
|
|
|
181 |
of the specified element(s).
|
|
|
182 |
|
| 36397 |
ripley |
183 |
Both \code{[[} and \code{$} select a single element of the list. The
|
|
|
184 |
main difference is that \code{$} does not allow computed indices,
|
|
|
185 |
whereas \code{[[} does. \code{x$name} is equivalent to
|
| 41697 |
ripley |
186 |
\code{x[["name", exact = FALSE]]}. Also, the partial matching
|
|
|
187 |
behavior of \code{[[} can be controlled using the \code{exact} argument.
|
| 34719 |
ripley |
188 |
|
| 56724 |
maechler |
189 |
\code{getElement(x, name)} is a version of \code{x[[name, exact = TRUE]]}
|
|
|
190 |
which for formally classed (S4) objects returns \code{\link{slot}(x, name)},
|
|
|
191 |
hence providing access to even more general list-like objects.
|
|
|
192 |
|
| 34719 |
ripley |
193 |
\code{[} and \code{[[} are sometimes applied to other recursive
|
| 34720 |
ripley |
194 |
objects such as \link{call}s and \link{expression}s. Pairlists are
|
|
|
195 |
coerced to lists for extraction by \code{[}, but all three operators
|
|
|
196 |
can be used for replacement.
|
| 34719 |
ripley |
197 |
|
| 36397 |
ripley |
198 |
\code{[[} can be applied recursively to lists, so that if the single
|
|
|
199 |
index \code{i} is a vector of length \code{p}, \code{alist[[i]]} is
|
|
|
200 |
equivalent to \code{alist[[i1]]\dots[[ip]]} providing all but the
|
|
|
201 |
final indexing results in a list.
|
| 22883 |
ripley |
202 |
|
| 55419 |
maechler |
203 |
Note that in all three kinds of replacement, a value of \code{NULL}
|
|
|
204 |
deletes the corresponding item of the list. To set entries to
|
|
|
205 |
\code{NULL}, you need \code{x[i] <- list(NULL)}.
|
| 44435 |
ripley |
206 |
|
| 34719 |
ripley |
207 |
When \code{$<-} is applied to a \code{NULL} \code{x}, it first coerces
|
|
|
208 |
\code{x} to \code{list()}. This is what also happens with \code{[[<-}
|
|
|
209 |
if the replacement value \code{value} is of length greater than one:
|
|
|
210 |
if \code{value} has length 1 or 0, \code{x} is first coerced to a
|
|
|
211 |
zero-length vector of the type of \code{value}.
|
|
|
212 |
}
|
| 59927 |
ripley |
213 |
|
| 34719 |
ripley |
214 |
\section{Environments}{
|
| 36397 |
ripley |
215 |
Both \code{$} and \code{[[} can be applied to environments. Only
|
| 55555 |
ripley |
216 |
character indices are allowed and no partial matching is done. The
|
| 61150 |
ripley |
217 |
semantics of these operations are those of \code{get(i, env = x,
|
|
|
218 |
inherits = FALSE)}. If no match is found then \code{NULL} is
|
| 41697 |
ripley |
219 |
returned. The replacement versions, \code{$<-} and \code{[[<-}, can
|
| 36397 |
ripley |
220 |
also be used. Again, only character arguments are allowed. The
|
| 61150 |
ripley |
221 |
semantics in this case are those of \code{assign(i, value, env = x,
|
|
|
222 |
inherits = FALSE)}. Such an assignment will either create a new
|
| 36397 |
ripley |
223 |
binding or change the existing binding in \code{x}.
|
| 2 |
r |
224 |
}
|
| 59927 |
ripley |
225 |
|
| 31070 |
ripley |
226 |
\section{NAs in indexing}{
|
| 36397 |
ripley |
227 |
When extracting, a numerical, logical or character \code{NA} index picks
|
| 31070 |
ripley |
228 |
an unknown element and so returns \code{NA} in the corresponding
|
|
|
229 |
element of a logical, integer, numeric, complex or character result,
|
| 62064 |
ripley |
230 |
and \code{NULL} for a list. (It returns \code{00} for a raw result.)
|
| 31070 |
ripley |
231 |
|
| 36374 |
ripley |
232 |
When replacing (that is using indexing on the lhs of an
|
| 31070 |
ripley |
233 |
assignment) \code{NA} does not select any element to be replaced. As
|
|
|
234 |
there is ambiguity as to whether an element of the rhs should
|
| 38402 |
ripley |
235 |
be used or not, this is only allowed if the rhs value is of length one
|
|
|
236 |
(so the two interpretations would have the same outcome).
|
| 62066 |
ripley |
237 |
(The documented behaviour of S was that an \code{NA} replacement index
|
|
|
238 |
\sQuote{goes nowhere} but uses up an element of \code{value}:
|
| 65208 |
hornik |
239 |
Becker \emph{et al} p.\sspace{}359. However, that has not been true of
|
| 62066 |
ripley |
240 |
other implementations.)
|
| 31070 |
ripley |
241 |
}
|
| 59927 |
ripley |
242 |
|
| 33232 |
ripley |
243 |
\section{Argument matching}{
|
| 34194 |
ripley |
244 |
Note that these operations do not match their index arguments in the
|
|
|
245 |
standard way: argument names are ignored and positional matching only is
|
| 61150 |
ripley |
246 |
used. So \code{m[j = 2, i = 1]} is equivalent to \code{m[2, 1]} and
|
|
|
247 |
\strong{not} to \code{m[1, 2]}.
|
| 33232 |
ripley |
248 |
|
|
|
249 |
This may not be true for methods defined for them; for example it is
|
| 34719 |
ripley |
250 |
not true for the \code{data.frame} methods described in
|
| 48681 |
ripley |
251 |
\code{\link{[.data.frame}} which warn if \code{i} or \code{j}
|
|
|
252 |
is named and have undocumented behaviour in that case.
|
| 33232 |
ripley |
253 |
|
| 41697 |
ripley |
254 |
To avoid confusion, do not name index arguments (but \code{drop} and
|
|
|
255 |
\code{exact} must be named).
|
| 33232 |
ripley |
256 |
}
|
| 59927 |
ripley |
257 |
|
| 41476 |
ripley |
258 |
\section{S4 methods}{
|
| 50675 |
ripley |
259 |
These operators are also implicit S4 generics, but as primitives, S4
|
|
|
260 |
methods will be dispatched only on S4 objects \code{x}.
|
| 51231 |
maechler |
261 |
|
| 50675 |
ripley |
262 |
% not yet implemented
|
|
|
263 |
% These operators are also implicit S4 generics, but the implementation
|
|
|
264 |
% of the primitive functions will only dispatch S4 methods if one of the
|
|
|
265 |
% arguments is an S4 object (it is no longer true that only the first
|
|
|
266 |
% argument is checked). Method signatures can include any formal
|
|
|
267 |
% argument, but the primitive evaluation will only look for a method if
|
|
|
268 |
% \code{\link{isS4}} would return \code{TRUE} for some argument.
|
| 41476 |
ripley |
269 |
|
| 50675 |
ripley |
270 |
The implicit generics for the \code{$} and \code{$<-} operators do not
|
| 50903 |
hornik |
271 |
have \code{name} in their signature because the grammar only allows
|
| 50675 |
ripley |
272 |
symbols or string constants for the \code{name} argument.
|
| 41476 |
ripley |
273 |
}
|
| 59927 |
ripley |
274 |
|
| 43383 |
ripley |
275 |
\section{Character indices}{
|
|
|
276 |
Character indices can in some circumstances be partially matched (see
|
| 43389 |
ripley |
277 |
\code{\link{pmatch}}) to the names or dimnames of the object being
|
| 43383 |
ripley |
278 |
subsetted (but never for subassignment). Unlike S (Becker \emph{et
|
| 74363 |
maechler |
279 |
al} p.\sspace{}358), \R never uses partial matching when extracting by
|
| 65147 |
ripley |
280 |
\code{[}, and partial matching is not by default used by \code{[[}
|
|
|
281 |
(see argument \code{exact}).
|
| 43383 |
ripley |
282 |
|
|
|
283 |
Thus the default behaviour is to use partial matching only when
|
|
|
284 |
extracting from recursive objects (except environments) by \code{$}.
|
|
|
285 |
Even in that case, warnings can be switched on by
|
| 61130 |
maechler |
286 |
\code{\link{options}(warnPartialMatchDollar = TRUE)}.
|
| 43383 |
ripley |
287 |
|
|
|
288 |
Neither empty (\code{""}) nor \code{NA} indices match any names, not
|
|
|
289 |
even empty nor missing names. If any object has no names or
|
|
|
290 |
appropriate dimnames, they are taken as all \code{""} and so match
|
|
|
291 |
nothing.
|
|
|
292 |
}
|
| 59927 |
ripley |
293 |
|
| 24300 |
ripley |
294 |
\references{
|
|
|
295 |
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
|
|
|
296 |
\emph{The New S Language}.
|
| 47262 |
ripley |
297 |
Wadsworth & Brooks/Cole.
|
| 24300 |
ripley |
298 |
}
|
| 59927 |
ripley |
299 |
|
| 2 |
r |
300 |
\seealso{
|
| 43377 |
ripley |
301 |
\code{\link{names}} for details of matching to names, and
|
|
|
302 |
\code{\link{pmatch}} for partial matching.
|
| 41697 |
ripley |
303 |
|
| 7081 |
pd |
304 |
\code{\link{list}}, \code{\link{array}}, \code{\link{matrix}}.
|
| 18286 |
ripley |
305 |
|
| 24767 |
ripley |
306 |
\code{\link{[.data.frame}} and \code{\link{[.factor}} for the
|
|
|
307 |
behaviour when applied to data.frame and factors.
|
|
|
308 |
|
| 22694 |
ripley |
309 |
\code{\link{Syntax}} for operator precedence, and the
|
| 61823 |
ripley |
310 |
\sQuote{R Language Definition} manual about indexing details.
|
| 61130 |
maechler |
311 |
|
|
|
312 |
|
| 59927 |
ripley |
313 |
\code{\link{NULL}} for details of indexing null objects.
|
| 2 |
r |
314 |
}
|
| 49649 |
ripley |
315 |
%% Fixme: Link (to html in 'help.start()', pdf from 'ref manual',
|
| 74363 |
maechler |
316 |
%% 'info' from ESS), see \url{https://CRAN.R-project.org/manuals.html}.
|
| 51231 |
maechler |
317 |
|
| 2 |
r |
318 |
\examples{
|
| 50896 |
falcon |
319 |
x <- 1:12
|
| 59927 |
ripley |
320 |
m <- matrix(1:6, nrow = 2, dimnames = list(c("a", "b"), LETTERS[1:3]))
|
|
|
321 |
li <- list(pi = pi, e = exp(1))
|
| 7313 |
ripley |
322 |
x[10] # the tenth element of x
|
| 47616 |
ripley |
323 |
x <- x[-1] # delete the 1st element of x
|
| 7313 |
ripley |
324 |
m[1,] # the first row of matrix m
|
| 1293 |
ihaka |
325 |
m[1, , drop = FALSE] # is a 1-row matrix
|
| 26416 |
pd |
326 |
m[,c(TRUE,FALSE,TRUE)]# logical indexing
|
| 50896 |
falcon |
327 |
m[cbind(c(1,2,1),3:1)]# matrix numeric index
|
|
|
328 |
ci <- cbind(c("a", "b", "a"), c("A", "C", "B"))
|
|
|
329 |
m[ci] # matrix character index
|
| 47616 |
ripley |
330 |
m <- m[,-1] # delete the first column of m
|
| 7313 |
ripley |
331 |
li[[1]] # the first element of list li
|
| 59927 |
ripley |
332 |
y <- list(1, 2, a = 4, 5)
|
|
|
333 |
y[c(3, 4)] # a list containing elements 3 and 4 of y
|
| 7313 |
ripley |
334 |
y$a # the element of y named a
|
| 8628 |
pd |
335 |
|
| 22694 |
ripley |
336 |
## non-integer indices are truncated:
|
|
|
337 |
(i <- 3.999999999) # "4" is printed
|
|
|
338 |
(1:5)[i] # 3
|
| 22883 |
ripley |
339 |
|
| 51231 |
maechler |
340 |
## named atomic vectors, compare "[" and "[[" :
|
|
|
341 |
nx <- c(Abc = 123, pi = pi)
|
|
|
342 |
nx[1] ; nx["pi"] # keeps names, whereas "[[" does not:
|
|
|
343 |
nx[[1]] ; nx[["pi"]]
|
|
|
344 |
\dontshow{
|
|
|
345 |
stopifnot(identical(names(nx[1]), "Abc"),
|
|
|
346 |
identical(names(nx["pi"]), "pi"),
|
|
|
347 |
is.null(names(nx[["Abc"]])), is.null(names(nx[[2]])))
|
|
|
348 |
}
|
| 22883 |
ripley |
349 |
## recursive indexing into lists
|
| 61168 |
ripley |
350 |
z <- list(a = list(b = 9, c = "hello"), d = 1:5)
|
| 22883 |
ripley |
351 |
unlist(z)
|
|
|
352 |
z[[c(1, 2)]]
|
|
|
353 |
z[[c(1, 2, 1)]] # both "hello"
|
|
|
354 |
z[[c("a", "b")]] <- "new"
|
|
|
355 |
unlist(z)
|
| 28176 |
rgentlem |
356 |
|
| 31068 |
ripley |
357 |
## check $ and [[ for environments
|
| 28176 |
rgentlem |
358 |
e1 <- new.env()
|
|
|
359 |
e1$a <- 10
|
|
|
360 |
e1[["a"]]
|
|
|
361 |
e1[["b"]] <- 20
|
|
|
362 |
e1$b
|
|
|
363 |
ls(e1)
|
| 61130 |
maechler |
364 |
|
|
|
365 |
## partial matching - possibly with warning :
|
|
|
366 |
stopifnot(identical(li$p, pi))
|
|
|
367 |
op <- options(warnPartialMatchDollar = TRUE)
|
|
|
368 |
stopifnot( identical(li$p, pi), #-- a warning
|
| 61150 |
ripley |
369 |
inherits(tryCatch (li$p, warning = identity), "warning"))
|
| 61130 |
maechler |
370 |
## revert the warning option:
|
|
|
371 |
if(is.null(op[[1]])) op[[1]] <- FALSE; options(op)
|
| 2 |
r |
372 |
}
|
|
|
373 |
\keyword{array}
|
|
|
374 |
\keyword{list}
|