PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/RdotNET/bin/lib/R/library/base/html/Extract.html

http://rdotnet.googlecode.com/
HTML | 321 lines | 272 code | 49 blank | 0 comment | 0 complexity | 03105f314f96165300520490d1c0e81c MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, LGPL-2.0, BSD-3-Clause, AGPL-3.0
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html><head><title>R: Extract or Replace Parts of an Object</title>
  3. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  4. <link rel="stylesheet" type="text/css" href="../../R.css">
  5. </head><body>
  6. <table width="100%" summary="page for Extract {base}"><tr><td>Extract {base}</td><td align="right">R Documentation</td></tr></table>
  7. <h2>Extract or Replace Parts of an Object</h2>
  8. <h3>Description</h3>
  9. <p>
  10. Operators acting on vectors, matrices, arrays and lists to extract or
  11. replace parts.
  12. </p>
  13. <h3>Usage</h3>
  14. <pre>
  15. x[i]
  16. x[i, j, ... , drop = TRUE]
  17. x[[i]]
  18. x[[i, j, ...]]
  19. x$name
  20. </pre>
  21. <h3>Arguments</h3>
  22. <table summary="R argblock">
  23. <tr valign="top"><td><code>x</code></td>
  24. <td>
  25. object from which to extract element(s) or in which to replace element(s).
  26. </td></tr>
  27. <tr valign="top"><td><code>i, j, ..., name</code></td>
  28. <td>
  29. indices specifying elements to extract or replace. <code>i, j</code> are
  30. <code>numeric</code> or <code>character</code> vectors or empty (missing) or
  31. <code>NULL</code> whereas <code>name</code> must be a character string or an
  32. (unquoted or backtick quoted) name. Numeric values are coerced to
  33. integer as by <code><a href="integer.html">as.integer</a></code>. For extraction with
  34. <code>[[</code> and <code>$</code> character strings are normally (see under
  35. Environments) partially matched to the <code><a href="names.html">names</a></code> of the
  36. object if exact matching does not succeed.
  37. <br>
  38. For <code>[</code>-indexing only: <code>i, j, ...</code> can be logical
  39. vectors, indicating elements/slices to select. Such vectors are
  40. recycled if necessary to match the corresponding extent. <code>i, j,
  41. ...</code> can also be negative integers, indicating elements/slices
  42. to leave out of the selection.
  43. <br>
  44. When indexing arrays by <code>[</code> a single argument <code>i</code> can be a
  45. matrix with as many columns as there are dimensions of <code>x</code>; the
  46. result is then a vector with elements corresponding to the sets of
  47. indices in each row of <code>i</code>.
  48. <br>
  49. An index value of <code>NULL</code> is treated as if it were <code>integer(0)</code>.
  50. </td></tr>
  51. <tr valign="top"><td><code>drop</code></td>
  52. <td>
  53. For matrices and arrays. If <code>TRUE</code> the result is
  54. coerced to the lowest possible dimension (see the examples). This
  55. only works for extracting elements, not for the replacement.</td></tr>
  56. </table>
  57. <h3>Details</h3>
  58. <p>
  59. These operators are generic. You can write methods to handle indexing
  60. of specific classes of objects, see <a href="zMethods.html">InternalMethods</a> as well as
  61. <code><a href="Extract.data.frame.html">[.data.frame</a></code> and <code><a href="Extract.factor.html">[.factor</a></code>. The
  62. descriptions here apply only to the default methods. Note that
  63. separate methods are required for the replacement functions
  64. <code>[&lt;-</code>, <code>[[&lt;-</code> and <code>$&lt;-</code> for use when indexing occurs on
  65. the assignment side of an expression.
  66. </p>
  67. <p>
  68. The most important distinction between <code>[</code>, <code>[[</code> and
  69. <code>$</code> is that the <code>[</code> can select more than one element whereas
  70. the other two select a single element.
  71. </p>
  72. <p>
  73. The default methods work somewhat differently for atomic vectors,
  74. matrices/arrays and for recursive (list-like, see
  75. <code><a href="is.recursive.html">is.recursive</a></code>) objects. <code>$</code> returns <code>NULL</code>
  76. except for recursive objects, and is only discussed in the section
  77. below on recursive objects.
  78. </p>
  79. <p>
  80. Subsetting (except by an empty index) will drop all attributes except
  81. <code>names</code>, <code>dim</code> and <code>dimnames</code>.
  82. </p>
  83. <p>
  84. Indexing can occur on the right-hand-side of an expression for
  85. extraction, or on the left-hand-side for replacement. When an index
  86. expression appears on the left side of an assignment (known as
  87. <EM>subassignment</EM>) then that part of <code>x</code> is set to the value
  88. of the right hand side of the assignment. In this case no partial
  89. matching of indices is done, and the left-hand-side is coerced as
  90. needed to accept the values. Attributes are preserved (although
  91. <code>names</code>, <code>dim</code> and <code>dinmanes</code> will be adjusted
  92. suitably).
  93. </p>
  94. <h3>Atomic vectors</h3>
  95. <p>
  96. The usual form of indexing is <code>"["</code>. <code>"[["</code> can be used to
  97. select a single element, but <code>"["</code> can also do so (but will not
  98. partially match a character index).
  99. </p>
  100. <p>
  101. The index object <code>i</code> can be numeric, logical, character or empty.
  102. Indexing by factors is allowed and is equivalent to indexing by the
  103. numeric codes (see <code><a href="factor.html">factor</a></code>) and not by the character
  104. values which are printed (for which use <code>[as.character(i)]</code>).
  105. </p>
  106. <p>
  107. An empty index selects all values: this is most often used to replace
  108. all the entries but keep the <code><a href="attributes.html">attributes</a></code>.
  109. </p>
  110. <h3>Matrices and arrays</h3>
  111. <p>
  112. Matrices and arrays are vectors with a dimension attribute and so all
  113. the vector forms of indexing can be used with a single index. The
  114. result will be an unnamed vector unless <code>x</code> is one-dimensional
  115. when it will be a one-dimensional array.
  116. </p>
  117. <p>
  118. The most common form of indexing a <i>k</i>-dimensional array is to
  119. specify <i>k</i> indices to <code>[</code>. As for vector indexing, the
  120. indices can be numeric, logical, character, empty or even factor.
  121. An empty index (a comma separated blank) indicates that all entries in
  122. that dimension are selected.
  123. The argument <code>drop</code> applies to this form of indexing.
  124. </p>
  125. <p>
  126. A third form of indexing is via a numeric matrix with the one column
  127. for each dimension: each row of the index matrix then selects a single
  128. element of the array, and the result is a vector. Negative indices are
  129. not allowed in the index matrix. <code>NA</code> and zero values are allowed:
  130. rows of an index matrix containing a zero are ignored, whereas rows
  131. containing an <code>NA</code> produce an <code>NA</code> in the result.
  132. </p>
  133. <p>
  134. A vector obtained by matrix indexing will be unnamed unless <code>x</code>
  135. is one-dimensional when the row names (if any) will be indexed to
  136. provide names for the result.
  137. </p>
  138. <h3>Recursive (list-like) objects</h3>
  139. <p>
  140. Indexing by <code>[</code> is similar to atomic vectors and selects a list
  141. of the specified element(s).
  142. </p>
  143. <p>
  144. Both <code>[[</code> and <code>$</code> select a single element of the list. The
  145. main difference is that <code>$</code> does not allow computed indices,
  146. whereas <code>[[</code> does. <code>x$name</code> is equivalent to
  147. <code>x[["name"]]</code>.
  148. </p>
  149. <p>
  150. <code>[</code> and <code>[[</code> are sometimes applied to other recursive
  151. objects such as <a href="call.html">call</a>s and <a href="expression.html">expression</a>s. Pairlists are
  152. coerced to lists for extraction by <code>[</code>, but all three operators
  153. can be used for replacement.
  154. </p>
  155. <p>
  156. <code>[[</code> can be applied recursively to lists, so that if the single
  157. index <code>i</code> is a vector of length <code>p</code>, <code>alist[[i]]</code> is
  158. equivalent to <code>alist[[i1]]...[[ip]]</code> providing all but the
  159. final indexing results in a list.
  160. </p>
  161. <p>
  162. When <code>$&lt;-</code> is applied to a <code>NULL</code> <code>x</code>, it first coerces
  163. <code>x</code> to <code>list()</code>. This is what also happens with <code>[[&lt;-</code>
  164. if the replacement value <code>value</code> is of length greater than one:
  165. if <code>value</code> has length 1 or 0, <code>x</code> is first coerced to a
  166. zero-length vector of the type of <code>value</code>.
  167. </p>
  168. <h3>Environments</h3>
  169. <p>
  170. Both <code>$</code> and <code>[[</code> can be applied to environments. Only
  171. character arguments are allowed and no partial matching is done. The
  172. semantics of these operations are those of <code>get(i, env=x,
  173. inherits=FALSE)</code>. If no match is found then <code>NULL</code> is
  174. returned. The assignment versions, <code>$&lt;-</code> and <code>[[&lt;-</code>, can
  175. also be used. Again, only character arguments are allowed. The
  176. semantics in this case are those of <code>assign(i, value, env=x,
  177. inherits=FALSE)</code>. Such an assignment will either create a new
  178. binding or change the existing binding in <code>x</code>.
  179. </p>
  180. <h3>NAs in indexing</h3>
  181. <p>
  182. When extracting, a numerical, logical or character <code>NA</code> index picks
  183. an unknown element and so returns <code>NA</code> in the corresponding
  184. element of a logical, integer, numeric, complex or character result,
  185. and <code>NULL</code> for a list. (It returns <code>00</code> for a raw result.]
  186. </p>
  187. <p>
  188. When replacing (that is using indexing on the lhs of an
  189. assignment) <code>NA</code> does not select any element to be replaced. As
  190. there is ambiguity as to whether an element of the rhs should
  191. be used or not, this is only allowed if the rhs value is of length one
  192. (so the two interpretations would have the same outcome).
  193. </p>
  194. <h3>Argument matching</h3>
  195. <p>
  196. Note that these operations do not match their index arguments in the
  197. standard way: argument names are ignored and positional matching only is
  198. used. So <code>m[j=2,i=1]</code> is equivalent to <code>m[2,1]</code> and
  199. <STRONG>not</STRONG> to <code>m[1,2]</code>.
  200. </p>
  201. <p>
  202. This may not be true for methods defined for them; for example it is
  203. not true for the <code>data.frame</code> methods described in
  204. <code><a href="Extract.data.frame.html">[.data.frame</a></code>.
  205. </p>
  206. <p>
  207. To avoid confusion, do not name index arguments (but <code>drop</code> must
  208. be named).
  209. </p>
  210. <h3>Note</h3>
  211. <p>
  212. S uses partial matching when extracting by <code>[</code>
  213. (Becker <EM>et al</EM> p. 358) whereas <font face="Courier New,Courier" color="#666666"><b>R</b></font> does not.
  214. </p>
  215. <p>
  216. The documented behaviour of S is that an <code>NA</code> replacement index
  217. &lsquo;goes nowhere&rsquo; but uses up an element of <code>value</code>
  218. (Becker <EM>et al</EM> p. 359). However, that is not the current
  219. behaviour of S-PLUS.
  220. </p>
  221. <h3>References</h3>
  222. <p>
  223. Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
  224. <EM>The New S Language</EM>.
  225. Wadsworth &amp; Brooks/Cole.
  226. </p>
  227. <h3>See Also</h3>
  228. <p>
  229. <code><a href="list.html">list</a></code>, <code><a href="array.html">array</a></code>, <code><a href="matrix.html">matrix</a></code>.
  230. </p>
  231. <p>
  232. <code><a href="Extract.data.frame.html">[.data.frame</a></code> and <code><a href="Extract.factor.html">[.factor</a></code> for the
  233. behaviour when applied to data.frame and factors.
  234. </p>
  235. <p>
  236. <code><a href="Syntax.html">Syntax</a></code> for operator precedence, and the
  237. <EM>R Language</EM> reference manual about indexing details.
  238. </p>
  239. <h3>Examples</h3>
  240. <pre>
  241. x &lt;- 1:12; m &lt;- matrix(1:6,nr=2); li &lt;- list(pi=pi, e = exp(1))
  242. x[10] # the tenth element of x
  243. x &lt;- x[-1] # delete the 1st element of x
  244. m[1,] # the first row of matrix m
  245. m[1, , drop = FALSE] # is a 1-row matrix
  246. m[,c(TRUE,FALSE,TRUE)]# logical indexing
  247. m[cbind(c(1,2,1),3:1)]# matrix index
  248. m &lt;- m[,-1] # delete the first column of m
  249. li[[1]] # the first element of list li
  250. y &lt;- list(1,2,a=4,5)
  251. y[c(3,4)] # a list containing elements 3 and 4 of y
  252. y$a # the element of y named a
  253. ## non-integer indices are truncated:
  254. (i &lt;- 3.999999999) # "4" is printed
  255. (1:5)[i] # 3
  256. ## recursive indexing into lists
  257. z &lt;- list( a=list( b=9, c='hello'), d=1:5)
  258. unlist(z)
  259. z[[c(1, 2)]]
  260. z[[c(1, 2, 1)]] # both "hello"
  261. z[[c("a", "b")]] &lt;- "new"
  262. unlist(z)
  263. ## check $ and [[ for environments
  264. e1 &lt;- new.env()
  265. e1$a &lt;- 10
  266. e1[["a"]]
  267. e1[["b"]] &lt;- 20
  268. e1$b
  269. ls(e1)
  270. </pre>
  271. <hr><div align="center">[Package <em>base</em> version 2.4.0 <a href="00Index.html">Index]</a></div>
  272. </body></html>