2473 lines
200 KiB
HTML
2473 lines
200 KiB
HTML
|
<!DOCTYPE html>
|
|||
|
|
|||
|
<html>
|
|||
|
|
|||
|
<head>
|
|||
|
|
|||
|
<meta charset="utf-8" />
|
|||
|
<meta name="generator" content="pandoc" />
|
|||
|
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
|
|||
|
|
|||
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<title>Invariants: Comparing behavior with data frames</title>
|
|||
|
|
|||
|
<script>// Pandoc 2.9 adds attributes on both header and div. We remove the former (to
|
|||
|
// be compatible with the behavior of Pandoc < 2.8).
|
|||
|
document.addEventListener('DOMContentLoaded', function(e) {
|
|||
|
var hs = document.querySelectorAll("div.section[class*='level'] > :first-child");
|
|||
|
var i, h, a;
|
|||
|
for (i = 0; i < hs.length; i++) {
|
|||
|
h = hs[i];
|
|||
|
if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6
|
|||
|
a = h.attributes;
|
|||
|
while (a.length > 0) h.removeAttribute(a[0].name);
|
|||
|
}
|
|||
|
});
|
|||
|
</script>
|
|||
|
|
|||
|
<style type="text/css">
|
|||
|
code{white-space: pre-wrap;}
|
|||
|
span.smallcaps{font-variant: small-caps;}
|
|||
|
span.underline{text-decoration: underline;}
|
|||
|
div.column{display: inline-block; vertical-align: top; width: 50%;}
|
|||
|
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
|
|||
|
ul.task-list{list-style: none;}
|
|||
|
</style>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<style type="text/css">
|
|||
|
code {
|
|||
|
white-space: pre;
|
|||
|
}
|
|||
|
.sourceCode {
|
|||
|
overflow: visible;
|
|||
|
}
|
|||
|
</style>
|
|||
|
<style type="text/css" data-origin="pandoc">
|
|||
|
pre > code.sourceCode { white-space: pre; position: relative; }
|
|||
|
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
|
|||
|
pre > code.sourceCode > span:empty { height: 1.2em; }
|
|||
|
.sourceCode { overflow: visible; }
|
|||
|
code.sourceCode > span { color: inherit; text-decoration: inherit; }
|
|||
|
div.sourceCode { margin: 1em 0; }
|
|||
|
pre.sourceCode { margin: 0; }
|
|||
|
@media screen {
|
|||
|
div.sourceCode { overflow: auto; }
|
|||
|
}
|
|||
|
@media print {
|
|||
|
pre > code.sourceCode { white-space: pre-wrap; }
|
|||
|
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
|
|||
|
}
|
|||
|
pre.numberSource code
|
|||
|
{ counter-reset: source-line 0; }
|
|||
|
pre.numberSource code > span
|
|||
|
{ position: relative; left: -4em; counter-increment: source-line; }
|
|||
|
pre.numberSource code > span > a:first-child::before
|
|||
|
{ content: counter(source-line);
|
|||
|
position: relative; left: -1em; text-align: right; vertical-align: baseline;
|
|||
|
border: none; display: inline-block;
|
|||
|
-webkit-touch-callout: none; -webkit-user-select: none;
|
|||
|
-khtml-user-select: none; -moz-user-select: none;
|
|||
|
-ms-user-select: none; user-select: none;
|
|||
|
padding: 0 4px; width: 4em;
|
|||
|
color: #aaaaaa;
|
|||
|
}
|
|||
|
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
|
|||
|
div.sourceCode
|
|||
|
{ }
|
|||
|
@media screen {
|
|||
|
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
|
|||
|
}
|
|||
|
code span.al { color: #ff0000; font-weight: bold; }
|
|||
|
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; }
|
|||
|
code span.at { color: #7d9029; }
|
|||
|
code span.bn { color: #40a070; }
|
|||
|
code span.bu { color: #008000; }
|
|||
|
code span.cf { color: #007020; font-weight: bold; }
|
|||
|
code span.ch { color: #4070a0; }
|
|||
|
code span.cn { color: #880000; }
|
|||
|
code span.co { color: #60a0b0; font-style: italic; }
|
|||
|
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; }
|
|||
|
code span.do { color: #ba2121; font-style: italic; }
|
|||
|
code span.dt { color: #902000; }
|
|||
|
code span.dv { color: #40a070; }
|
|||
|
code span.er { color: #ff0000; font-weight: bold; }
|
|||
|
code span.ex { }
|
|||
|
code span.fl { color: #40a070; }
|
|||
|
code span.fu { color: #06287e; }
|
|||
|
code span.im { color: #008000; font-weight: bold; }
|
|||
|
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; }
|
|||
|
code span.kw { color: #007020; font-weight: bold; }
|
|||
|
code span.op { color: #666666; }
|
|||
|
code span.ot { color: #007020; }
|
|||
|
code span.pp { color: #bc7a00; }
|
|||
|
code span.sc { color: #4070a0; }
|
|||
|
code span.ss { color: #bb6688; }
|
|||
|
code span.st { color: #4070a0; }
|
|||
|
code span.va { color: #19177c; }
|
|||
|
code span.vs { color: #4070a0; }
|
|||
|
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; }
|
|||
|
</style>
|
|||
|
<script>
|
|||
|
// apply pandoc div.sourceCode style to pre.sourceCode instead
|
|||
|
(function() {
|
|||
|
var sheets = document.styleSheets;
|
|||
|
for (var i = 0; i < sheets.length; i++) {
|
|||
|
if (sheets[i].ownerNode.dataset["origin"] !== "pandoc") continue;
|
|||
|
try { var rules = sheets[i].cssRules; } catch (e) { continue; }
|
|||
|
var j = 0;
|
|||
|
while (j < rules.length) {
|
|||
|
var rule = rules[j];
|
|||
|
// check if there is a div.sourceCode rule
|
|||
|
if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") {
|
|||
|
j++;
|
|||
|
continue;
|
|||
|
}
|
|||
|
var style = rule.style.cssText;
|
|||
|
// check if color or background-color is set
|
|||
|
if (rule.style.color === '' && rule.style.backgroundColor === '') {
|
|||
|
j++;
|
|||
|
continue;
|
|||
|
}
|
|||
|
// replace div.sourceCode by a pre.sourceCode rule
|
|||
|
sheets[i].deleteRule(j);
|
|||
|
sheets[i].insertRule('pre.sourceCode{' + style + '}', j);
|
|||
|
}
|
|||
|
}
|
|||
|
})();
|
|||
|
</script>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<style type="text/css">body {
|
|||
|
background-color: #fff;
|
|||
|
margin: 1em auto;
|
|||
|
max-width: 700px;
|
|||
|
overflow: visible;
|
|||
|
padding-left: 2em;
|
|||
|
padding-right: 2em;
|
|||
|
font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
|
|||
|
font-size: 14px;
|
|||
|
line-height: 1.35;
|
|||
|
}
|
|||
|
#TOC {
|
|||
|
clear: both;
|
|||
|
margin: 0 0 10px 10px;
|
|||
|
padding: 4px;
|
|||
|
width: 400px;
|
|||
|
border: 1px solid #CCCCCC;
|
|||
|
border-radius: 5px;
|
|||
|
background-color: #f6f6f6;
|
|||
|
font-size: 13px;
|
|||
|
line-height: 1.3;
|
|||
|
}
|
|||
|
#TOC .toctitle {
|
|||
|
font-weight: bold;
|
|||
|
font-size: 15px;
|
|||
|
margin-left: 5px;
|
|||
|
}
|
|||
|
#TOC ul {
|
|||
|
padding-left: 40px;
|
|||
|
margin-left: -1.5em;
|
|||
|
margin-top: 5px;
|
|||
|
margin-bottom: 5px;
|
|||
|
}
|
|||
|
#TOC ul ul {
|
|||
|
margin-left: -2em;
|
|||
|
}
|
|||
|
#TOC li {
|
|||
|
line-height: 16px;
|
|||
|
}
|
|||
|
table {
|
|||
|
margin: 1em auto;
|
|||
|
border-width: 1px;
|
|||
|
border-color: #DDDDDD;
|
|||
|
border-style: outset;
|
|||
|
border-collapse: collapse;
|
|||
|
}
|
|||
|
table th {
|
|||
|
border-width: 2px;
|
|||
|
padding: 5px;
|
|||
|
border-style: inset;
|
|||
|
}
|
|||
|
table td {
|
|||
|
border-width: 1px;
|
|||
|
border-style: inset;
|
|||
|
line-height: 18px;
|
|||
|
padding: 5px 5px;
|
|||
|
}
|
|||
|
table, table th, table td {
|
|||
|
border-left-style: none;
|
|||
|
border-right-style: none;
|
|||
|
}
|
|||
|
table thead, table tr.even {
|
|||
|
background-color: #f7f7f7;
|
|||
|
}
|
|||
|
p {
|
|||
|
margin: 0.5em 0;
|
|||
|
}
|
|||
|
blockquote {
|
|||
|
background-color: #f6f6f6;
|
|||
|
padding: 0.25em 0.75em;
|
|||
|
}
|
|||
|
hr {
|
|||
|
border-style: solid;
|
|||
|
border: none;
|
|||
|
border-top: 1px solid #777;
|
|||
|
margin: 28px 0;
|
|||
|
}
|
|||
|
dl {
|
|||
|
margin-left: 0;
|
|||
|
}
|
|||
|
dl dd {
|
|||
|
margin-bottom: 13px;
|
|||
|
margin-left: 13px;
|
|||
|
}
|
|||
|
dl dt {
|
|||
|
font-weight: bold;
|
|||
|
}
|
|||
|
ul {
|
|||
|
margin-top: 0;
|
|||
|
}
|
|||
|
ul li {
|
|||
|
list-style: circle outside;
|
|||
|
}
|
|||
|
ul ul {
|
|||
|
margin-bottom: 0;
|
|||
|
}
|
|||
|
pre, code {
|
|||
|
background-color: #f7f7f7;
|
|||
|
border-radius: 3px;
|
|||
|
color: #333;
|
|||
|
white-space: pre-wrap;
|
|||
|
}
|
|||
|
pre {
|
|||
|
border-radius: 3px;
|
|||
|
margin: 5px 0px 10px 0px;
|
|||
|
padding: 10px;
|
|||
|
}
|
|||
|
pre:not([class]) {
|
|||
|
background-color: #f7f7f7;
|
|||
|
}
|
|||
|
code {
|
|||
|
font-family: Consolas, Monaco, 'Courier New', monospace;
|
|||
|
font-size: 85%;
|
|||
|
}
|
|||
|
p > code, li > code {
|
|||
|
padding: 2px 0px;
|
|||
|
}
|
|||
|
div.figure {
|
|||
|
text-align: center;
|
|||
|
}
|
|||
|
img {
|
|||
|
background-color: #FFFFFF;
|
|||
|
padding: 2px;
|
|||
|
border: 1px solid #DDDDDD;
|
|||
|
border-radius: 3px;
|
|||
|
border: 1px solid #CCCCCC;
|
|||
|
margin: 0 5px;
|
|||
|
}
|
|||
|
h1 {
|
|||
|
margin-top: 0;
|
|||
|
font-size: 35px;
|
|||
|
line-height: 40px;
|
|||
|
}
|
|||
|
h2 {
|
|||
|
border-bottom: 4px solid #f7f7f7;
|
|||
|
padding-top: 10px;
|
|||
|
padding-bottom: 2px;
|
|||
|
font-size: 145%;
|
|||
|
}
|
|||
|
h3 {
|
|||
|
border-bottom: 2px solid #f7f7f7;
|
|||
|
padding-top: 10px;
|
|||
|
font-size: 120%;
|
|||
|
}
|
|||
|
h4 {
|
|||
|
border-bottom: 1px solid #f7f7f7;
|
|||
|
margin-left: 8px;
|
|||
|
font-size: 105%;
|
|||
|
}
|
|||
|
h5, h6 {
|
|||
|
border-bottom: 1px solid #ccc;
|
|||
|
font-size: 105%;
|
|||
|
}
|
|||
|
a {
|
|||
|
color: #0033dd;
|
|||
|
text-decoration: none;
|
|||
|
}
|
|||
|
a:hover {
|
|||
|
color: #6666ff; }
|
|||
|
a:visited {
|
|||
|
color: #800080; }
|
|||
|
a:visited:hover {
|
|||
|
color: #BB00BB; }
|
|||
|
a[href^="http:"] {
|
|||
|
text-decoration: underline; }
|
|||
|
a[href^="https:"] {
|
|||
|
text-decoration: underline; }
|
|||
|
|
|||
|
code > span.kw { color: #555; font-weight: bold; }
|
|||
|
code > span.dt { color: #902000; }
|
|||
|
code > span.dv { color: #40a070; }
|
|||
|
code > span.bn { color: #d14; }
|
|||
|
code > span.fl { color: #d14; }
|
|||
|
code > span.ch { color: #d14; }
|
|||
|
code > span.st { color: #d14; }
|
|||
|
code > span.co { color: #888888; font-style: italic; }
|
|||
|
code > span.ot { color: #007020; }
|
|||
|
code > span.al { color: #ff0000; font-weight: bold; }
|
|||
|
code > span.fu { color: #900; font-weight: bold; }
|
|||
|
code > span.er { color: #a61717; background-color: #e3d2d2; }
|
|||
|
</style>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
</head>
|
|||
|
|
|||
|
<body>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<h1 class="title toc-ignore">Invariants: Comparing behavior with data
|
|||
|
frames</h1>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<style type="text/css">
|
|||
|
.dftbl {
|
|||
|
width: 100%;
|
|||
|
table-layout: fixed;
|
|||
|
display: inline-table;
|
|||
|
}
|
|||
|
.error pre code {
|
|||
|
color: red;
|
|||
|
}
|
|||
|
.warning pre code {
|
|||
|
color: violet;
|
|||
|
}
|
|||
|
</style>
|
|||
|
<p>This vignette defines invariants for subsetting and subset-assignment
|
|||
|
for tibbles, and illustrates where their behaviour differs from data
|
|||
|
frames. The goal is to define a small set of invariants that
|
|||
|
consistently define how behaviors interact. Some behaviors are defined
|
|||
|
using functions of the vctrs package, e.g. <code>vec_slice()</code>,
|
|||
|
<code>vec_recycle()</code> and <code>vec_as_index()</code>. Refer to
|
|||
|
their documentation for more details about the invariants that they
|
|||
|
follow.</p>
|
|||
|
<p>The subsetting and subassignment operators for data frames and
|
|||
|
tibbles are particularly tricky, because they support both row and
|
|||
|
column indexes, both of which are optionally missing. We resolve this by
|
|||
|
first defining column access with <code>[[</code> and <code>$</code>,
|
|||
|
then column-wise subsetting with <code>[</code>, then row-wise
|
|||
|
subsetting, then the composition of both.</p>
|
|||
|
<div id="conventions" class="section level2">
|
|||
|
<h2>Conventions</h2>
|
|||
|
<p>In this article, all behaviors are demonstrated using one example
|
|||
|
data frame and its tibble equivalent:</p>
|
|||
|
<div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tibble)</span>
|
|||
|
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(vctrs)</span>
|
|||
|
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a>new_df <span class="ot"><-</span> <span class="cf">function</span>() {</span>
|
|||
|
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a> df <span class="ot"><-</span> <span class="fu">data.frame</span>(<span class="at">n =</span> <span class="fu">c</span>(1L, <span class="cn">NA</span>, 3L, <span class="cn">NA</span>))</span>
|
|||
|
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a> df<span class="sc">$</span>c <span class="ot"><-</span> letters[<span class="dv">5</span><span class="sc">:</span><span class="dv">8</span>]</span>
|
|||
|
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a> df<span class="sc">$</span>li <span class="ot"><-</span> <span class="fu">list</span>(<span class="dv">9</span>, <span class="dv">10</span><span class="sc">:</span><span class="dv">11</span>, <span class="dv">12</span><span class="sc">:</span><span class="dv">14</span>, <span class="st">"text"</span>)</span>
|
|||
|
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a> df</span>
|
|||
|
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a>}</span>
|
|||
|
<span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a>new_tbl <span class="ot"><-</span> <span class="cf">function</span>() {</span>
|
|||
|
<span id="cb1-10"><a href="#cb1-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">as_tibble</span>(<span class="fu">new_df</span>())</span>
|
|||
|
<span id="cb1-11"><a href="#cb1-11" aria-hidden="true" tabindex="-1"></a>}</span></code></pre></div>
|
|||
|
<p>Results of the same code for data frames and tibbles are presented
|
|||
|
side by side:</p>
|
|||
|
<table class="dftbl">
|
|||
|
<tbody>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="fu">new_df</span>()</span>
|
|||
|
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li</span></span>
|
|||
|
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e 9</span></span>
|
|||
|
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 NA f 10, 11</span></span>
|
|||
|
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 3 g 12, 13, 14</span></span>
|
|||
|
<span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 NA h text</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="fu">new_tbl</span>()</span>
|
|||
|
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 4 × 3</span></span>
|
|||
|
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li </span></span>
|
|||
|
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int> <chr> <list> </span></span>
|
|||
|
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e <dbl [1]></span></span>
|
|||
|
<span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 NA f <int [2]></span></span>
|
|||
|
<span id="cb3-7"><a href="#cb3-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 3 g <int [3]></span></span>
|
|||
|
<span id="cb3-8"><a href="#cb3-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 NA h <chr [1]></span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
<p>If the results are identical (after converting to a data frame if
|
|||
|
necessary), only the tibble result is shown.</p>
|
|||
|
<p>Subsetting operations are read-only. The same objects are reused in
|
|||
|
all examples:</p>
|
|||
|
<div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a>df <span class="ot"><-</span> <span class="fu">new_df</span>()</span>
|
|||
|
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a>tbl <span class="ot"><-</span> <span class="fu">new_tbl</span>()</span></code></pre></div>
|
|||
|
<p>Where needed, we also show examples with hierarchical columns
|
|||
|
containing a data frame or a matrix:</p>
|
|||
|
<div class="sourceCode" id="cb5"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a>new_tbl2 <span class="ot"><-</span> <span class="cf">function</span>() {</span>
|
|||
|
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">tibble</span>(</span>
|
|||
|
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a> <span class="at">tb =</span> tbl,</span>
|
|||
|
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a> <span class="at">m =</span> <span class="fu">diag</span>(<span class="dv">4</span>)</span>
|
|||
|
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a> )</span>
|
|||
|
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a>}</span>
|
|||
|
<span id="cb5-7"><a href="#cb5-7" aria-hidden="true" tabindex="-1"></a>new_df2 <span class="ot"><-</span> <span class="cf">function</span>() {</span>
|
|||
|
<span id="cb5-8"><a href="#cb5-8" aria-hidden="true" tabindex="-1"></a> df2 <span class="ot"><-</span> <span class="fu">new_tbl2</span>()</span>
|
|||
|
<span id="cb5-9"><a href="#cb5-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">class</span>(df2) <span class="ot"><-</span> <span class="st">"data.frame"</span></span>
|
|||
|
<span id="cb5-10"><a href="#cb5-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">class</span>(df2<span class="sc">$</span>tb) <span class="ot"><-</span> <span class="st">"data.frame"</span></span>
|
|||
|
<span id="cb5-11"><a href="#cb5-11" aria-hidden="true" tabindex="-1"></a> df2</span>
|
|||
|
<span id="cb5-12"><a href="#cb5-12" aria-hidden="true" tabindex="-1"></a>}</span>
|
|||
|
<span id="cb5-13"><a href="#cb5-13" aria-hidden="true" tabindex="-1"></a>df2 <span class="ot"><-</span> <span class="fu">new_df2</span>()</span>
|
|||
|
<span id="cb5-14"><a href="#cb5-14" aria-hidden="true" tabindex="-1"></a>tbl2 <span class="ot"><-</span> <span class="fu">new_tbl2</span>()</span></code></pre></div>
|
|||
|
<table class="dftbl">
|
|||
|
<tbody>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="fu">new_tbl</span>()</span>
|
|||
|
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 4 × 3</span></span>
|
|||
|
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li </span></span>
|
|||
|
<span id="cb6-4"><a href="#cb6-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int> <chr> <list> </span></span>
|
|||
|
<span id="cb6-5"><a href="#cb6-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e <dbl [1]></span></span>
|
|||
|
<span id="cb6-6"><a href="#cb6-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 NA f <int [2]></span></span>
|
|||
|
<span id="cb6-7"><a href="#cb6-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 3 g <int [3]></span></span>
|
|||
|
<span id="cb6-8"><a href="#cb6-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 NA h <chr [1]></span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
<p>For subset assignment (subassignment, for short), we need a fresh
|
|||
|
copy of the data for each test. The <code>with_*()</code> functions
|
|||
|
(omitted here for brevity) allow for a more concise notation. These
|
|||
|
functions take an assignment expression, execute it on a fresh copy of
|
|||
|
the data, and return the data for printing. The first example prints
|
|||
|
what’s really executed, further examples omit this output.</p>
|
|||
|
<table class="dftbl">
|
|||
|
<tbody>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_df</span>(df<span class="sc">$</span>n <span class="ot"><-</span> <span class="fu">rev</span>(df<span class="sc">$</span>n), <span class="at">verbose =</span> <span class="cn">TRUE</span>)</span>
|
|||
|
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> {</span></span>
|
|||
|
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> df <- new_df()</span></span>
|
|||
|
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> df$n <- rev(df$n)</span></span>
|
|||
|
<span id="cb7-5"><a href="#cb7-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> df</span></span>
|
|||
|
<span id="cb7-6"><a href="#cb7-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> }</span></span>
|
|||
|
<span id="cb7-7"><a href="#cb7-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li</span></span>
|
|||
|
<span id="cb7-8"><a href="#cb7-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 NA e 9</span></span>
|
|||
|
<span id="cb7-9"><a href="#cb7-9" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 3 f 10, 11</span></span>
|
|||
|
<span id="cb7-10"><a href="#cb7-10" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 NA g 12, 13, 14</span></span>
|
|||
|
<span id="cb7-11"><a href="#cb7-11" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 1 h text</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl<span class="sc">$</span>n <span class="ot"><-</span> <span class="fu">rev</span>(tbl<span class="sc">$</span>n), <span class="at">verbose =</span> <span class="cn">TRUE</span>)</span>
|
|||
|
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> {</span></span>
|
|||
|
<span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> tbl <- new_tbl()</span></span>
|
|||
|
<span id="cb8-4"><a href="#cb8-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> tbl$n <- rev(tbl$n)</span></span>
|
|||
|
<span id="cb8-5"><a href="#cb8-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> tbl</span></span>
|
|||
|
<span id="cb8-6"><a href="#cb8-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> }</span></span>
|
|||
|
<span id="cb8-7"><a href="#cb8-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 4 × 3</span></span>
|
|||
|
<span id="cb8-8"><a href="#cb8-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li </span></span>
|
|||
|
<span id="cb8-9"><a href="#cb8-9" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int> <chr> <list> </span></span>
|
|||
|
<span id="cb8-10"><a href="#cb8-10" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 NA e <dbl [1]></span></span>
|
|||
|
<span id="cb8-11"><a href="#cb8-11" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 3 f <int [2]></span></span>
|
|||
|
<span id="cb8-12"><a href="#cb8-12" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 NA g <int [3]></span></span>
|
|||
|
<span id="cb8-13"><a href="#cb8-13" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 1 h <chr [1]></span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
</div>
|
|||
|
<div id="column-extraction" class="section level2">
|
|||
|
<h2>Column extraction</h2>
|
|||
|
<div id="definition-of-xj" class="section level3">
|
|||
|
<h3>Definition of <code>x[[j]]</code></h3>
|
|||
|
<p><code>x[[j]]</code> is equal to <code>.subset2(x, j)</code>.</p>
|
|||
|
<table class="dftbl">
|
|||
|
<tbody>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb9"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a>tbl[[<span class="dv">1</span>]]</span>
|
|||
|
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] 1 NA 3 NA</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb10"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="fu">.subset2</span>(tbl, <span class="dv">1</span>)</span>
|
|||
|
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] 1 NA 3 NA</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
<p>NB: <code>x[[j]]</code> always returns an object of size
|
|||
|
<code>nrow(x)</code> if the column exists.</p>
|
|||
|
<p><code>j</code> must be a single number or a string, as enforced by
|
|||
|
<code>.subset2(x, j)</code>.</p>
|
|||
|
<table class="dftbl">
|
|||
|
<tbody>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb11"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a>df[[<span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>]]</span>
|
|||
|
<span id="cb11-2"><a href="#cb11-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] NA</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb12"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a>tbl[[<span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>]]</span>
|
|||
|
<span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error:</span></span>
|
|||
|
<span id="cb12-3"><a href="#cb12-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> ! The `j` argument of</span></span>
|
|||
|
<span id="cb12-4"><a href="#cb12-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> `[[.tbl_df` can't be a vector of</span></span>
|
|||
|
<span id="cb12-5"><a href="#cb12-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> length 2 as of tibble 3.0.0.</span></span>
|
|||
|
<span id="cb12-6"><a href="#cb12-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> ℹ Recursive subsetting is</span></span>
|
|||
|
<span id="cb12-7"><a href="#cb12-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> deprecated for tibbles.</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb13"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a>df[[<span class="fu">c</span>(<span class="st">"n"</span>, <span class="st">"c"</span>)]]</span>
|
|||
|
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in .subset2(x, i, exact = exact): subscript out of bounds</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb14"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a>tbl[[<span class="fu">c</span>(<span class="st">"n"</span>, <span class="st">"c"</span>)]]</span>
|
|||
|
<span id="cb14-2"><a href="#cb14-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in `tbl[[c("n", "c")]]`:</span></span>
|
|||
|
<span id="cb14-3"><a href="#cb14-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> ! Can't extract column with `c("n", "c")`.</span></span>
|
|||
|
<span id="cb14-4"><a href="#cb14-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> ✖ Subscript `c("n", "c")` must be size 1, not 2.</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb15"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a>df[[<span class="cn">TRUE</span>]]</span>
|
|||
|
<span id="cb15-2"><a href="#cb15-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] 1 NA 3 NA</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb16"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a>tbl[[<span class="cn">TRUE</span>]]</span>
|
|||
|
<span id="cb16-2"><a href="#cb16-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in `tbl[[TRUE]]`:</span></span>
|
|||
|
<span id="cb16-3"><a href="#cb16-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> ! Can't extract column with `TRUE`.</span></span>
|
|||
|
<span id="cb16-4"><a href="#cb16-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> ✖ `TRUE` must be numeric or character, not `TRUE`.</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb17"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true" tabindex="-1"></a>df[[mean]]</span>
|
|||
|
<span id="cb17-2"><a href="#cb17-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in .subset2(x, i, exact = exact): invalid subscript type 'closure'</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb18"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a>tbl[[mean]]</span>
|
|||
|
<span id="cb18-2"><a href="#cb18-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in `tbl[[mean]]`:</span></span>
|
|||
|
<span id="cb18-3"><a href="#cb18-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> ! Can't extract column with `mean`.</span></span>
|
|||
|
<span id="cb18-4"><a href="#cb18-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> ✖ `mean` must be numeric or character, not a function.</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
<p><code>NA</code> indexes, numeric out-of-bounds (OOB) values, and
|
|||
|
non-integers throw an error:</p>
|
|||
|
<table class="dftbl">
|
|||
|
<tbody>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb19"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true" tabindex="-1"></a>df[[<span class="cn">NA</span>]]</span>
|
|||
|
<span id="cb19-2"><a href="#cb19-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> NULL</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb20"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a>tbl[[<span class="cn">NA</span>]]</span>
|
|||
|
<span id="cb20-2"><a href="#cb20-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in `tbl[[NA]]`:</span></span>
|
|||
|
<span id="cb20-3"><a href="#cb20-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> ! Can't extract column with `NA`.</span></span>
|
|||
|
<span id="cb20-4"><a href="#cb20-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> ✖ Subscript `NA` must be a location, not an integer `NA`.</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb21"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb21-1"><a href="#cb21-1" aria-hidden="true" tabindex="-1"></a>df[[<span class="cn">NA_character_</span>]]</span>
|
|||
|
<span id="cb21-2"><a href="#cb21-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> NULL</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb22"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb22-1"><a href="#cb22-1" aria-hidden="true" tabindex="-1"></a>tbl[[<span class="cn">NA_character_</span>]]</span>
|
|||
|
<span id="cb22-2"><a href="#cb22-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in `tbl[[NA_character_]]`:</span></span>
|
|||
|
<span id="cb22-3"><a href="#cb22-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> ! Can't extract column with `NA_character_`.</span></span>
|
|||
|
<span id="cb22-4"><a href="#cb22-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> ✖ Subscript `NA_character_` must be a location, not a character `NA`.</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb23"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb23-1"><a href="#cb23-1" aria-hidden="true" tabindex="-1"></a>df[[<span class="cn">NA_integer_</span>]]</span>
|
|||
|
<span id="cb23-2"><a href="#cb23-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> NULL</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb24"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a>tbl[[<span class="cn">NA_integer_</span>]]</span>
|
|||
|
<span id="cb24-2"><a href="#cb24-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in `tbl[[NA_integer_]]`:</span></span>
|
|||
|
<span id="cb24-3"><a href="#cb24-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> ! Can't extract column with `NA_integer_`.</span></span>
|
|||
|
<span id="cb24-4"><a href="#cb24-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> ✖ Subscript `NA_integer_` must be a location, not an integer `NA`.</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb25"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a>df[[<span class="sc">-</span><span class="dv">1</span>]]</span>
|
|||
|
<span id="cb25-2"><a href="#cb25-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in .subset2(x, i, exact = exact): invalid negative subscript in get1index <real></span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb26"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb26-1"><a href="#cb26-1" aria-hidden="true" tabindex="-1"></a>tbl[[<span class="sc">-</span><span class="dv">1</span>]]</span>
|
|||
|
<span id="cb26-2"><a href="#cb26-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in `tbl[[-1]]`:</span></span>
|
|||
|
<span id="cb26-3"><a href="#cb26-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> ! Can't extract column with `-1`.</span></span>
|
|||
|
<span id="cb26-4"><a href="#cb26-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> ✖ Subscript `-1` must be a positive location, not -1.</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb27"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb27-1"><a href="#cb27-1" aria-hidden="true" tabindex="-1"></a>df[[<span class="dv">4</span>]]</span>
|
|||
|
<span id="cb27-2"><a href="#cb27-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in .subset2(x, i, exact = exact): subscript out of bounds</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb28"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb28-1"><a href="#cb28-1" aria-hidden="true" tabindex="-1"></a>tbl[[<span class="dv">4</span>]]</span>
|
|||
|
<span id="cb28-2"><a href="#cb28-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in `tbl[[4]]`:</span></span>
|
|||
|
<span id="cb28-3"><a href="#cb28-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> ! Can't extract columns past the end.</span></span>
|
|||
|
<span id="cb28-4"><a href="#cb28-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> ℹ Location 4 doesn't exist.</span></span>
|
|||
|
<span id="cb28-5"><a href="#cb28-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> ℹ There are only 3 columns.</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb29"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb29-1"><a href="#cb29-1" aria-hidden="true" tabindex="-1"></a>df[[<span class="fl">1.5</span>]]</span>
|
|||
|
<span id="cb29-2"><a href="#cb29-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] 1 NA 3 NA</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb30"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb30-1"><a href="#cb30-1" aria-hidden="true" tabindex="-1"></a>tbl[[<span class="fl">1.5</span>]]</span>
|
|||
|
<span id="cb30-2"><a href="#cb30-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in `tbl[[1.5]]`:</span></span>
|
|||
|
<span id="cb30-3"><a href="#cb30-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> ! Can't extract column with `1.5`.</span></span>
|
|||
|
<span id="cb30-4"><a href="#cb30-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> ✖ Can't convert from `j` <double> to <integer> due to loss of precision.</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb31"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb31-1"><a href="#cb31-1" aria-hidden="true" tabindex="-1"></a>df[[<span class="cn">Inf</span>]]</span>
|
|||
|
<span id="cb31-2"><a href="#cb31-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> NULL</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb32"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb32-1"><a href="#cb32-1" aria-hidden="true" tabindex="-1"></a>tbl[[<span class="cn">Inf</span>]]</span>
|
|||
|
<span id="cb32-2"><a href="#cb32-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in `tbl[[Inf]]`:</span></span>
|
|||
|
<span id="cb32-3"><a href="#cb32-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> ! Can't extract column with `Inf`.</span></span>
|
|||
|
<span id="cb32-4"><a href="#cb32-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> ✖ Can't convert from `j` <double> to <integer> due to loss of precision.</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
<p>Character OOB access is silent because a common package idiom is to
|
|||
|
check for the absence of a column with
|
|||
|
<code>is.null(df[[var]])</code>.</p>
|
|||
|
<table class="dftbl">
|
|||
|
<tbody>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb33"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb33-1"><a href="#cb33-1" aria-hidden="true" tabindex="-1"></a>tbl[[<span class="st">"x"</span>]]</span>
|
|||
|
<span id="cb33-2"><a href="#cb33-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> NULL</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
</div>
|
|||
|
<div id="definition-of-xname" class="section level3">
|
|||
|
<h3>Definition of <code>x$name</code></h3>
|
|||
|
<p><code>x$name</code> and <code>x$"name"</code> are equal to
|
|||
|
<code>x[["name"]]</code>.</p>
|
|||
|
<table class="dftbl">
|
|||
|
<tbody>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb34"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb34-1"><a href="#cb34-1" aria-hidden="true" tabindex="-1"></a>tbl<span class="sc">$</span>n</span>
|
|||
|
<span id="cb34-2"><a href="#cb34-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] 1 NA 3 NA</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb35"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb35-1"><a href="#cb35-1" aria-hidden="true" tabindex="-1"></a>tbl<span class="sc">$</span><span class="st">"n"</span></span>
|
|||
|
<span id="cb35-2"><a href="#cb35-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] 1 NA 3 NA</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb36"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb36-1"><a href="#cb36-1" aria-hidden="true" tabindex="-1"></a>tbl[[<span class="st">"n"</span>]]</span>
|
|||
|
<span id="cb36-2"><a href="#cb36-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] 1 NA 3 NA</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
<p>Unlike data frames, tibbles do not partially match names. Because
|
|||
|
<code>df$x</code> is rarely used in packages, it can raise a
|
|||
|
warning:</p>
|
|||
|
<table class="dftbl">
|
|||
|
<tbody>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb37"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb37-1"><a href="#cb37-1" aria-hidden="true" tabindex="-1"></a>df<span class="sc">$</span>l</span>
|
|||
|
<span id="cb37-2"><a href="#cb37-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> [[1]]</span></span>
|
|||
|
<span id="cb37-3"><a href="#cb37-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] 9</span></span>
|
|||
|
<span id="cb37-4"><a href="#cb37-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> </span></span>
|
|||
|
<span id="cb37-5"><a href="#cb37-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> [[2]]</span></span>
|
|||
|
<span id="cb37-6"><a href="#cb37-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] 10 11</span></span>
|
|||
|
<span id="cb37-7"><a href="#cb37-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> </span></span>
|
|||
|
<span id="cb37-8"><a href="#cb37-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> [[3]]</span></span>
|
|||
|
<span id="cb37-9"><a href="#cb37-9" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] 12 13 14</span></span>
|
|||
|
<span id="cb37-10"><a href="#cb37-10" aria-hidden="true" tabindex="-1"></a><span class="co">#> </span></span>
|
|||
|
<span id="cb37-11"><a href="#cb37-11" aria-hidden="true" tabindex="-1"></a><span class="co">#> [[4]]</span></span>
|
|||
|
<span id="cb37-12"><a href="#cb37-12" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] "text"</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb38"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb38-1"><a href="#cb38-1" aria-hidden="true" tabindex="-1"></a>tbl<span class="sc">$</span>l</span>
|
|||
|
<span id="cb38-2"><a href="#cb38-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Warning: Unknown or uninitialised</span></span>
|
|||
|
<span id="cb38-3"><a href="#cb38-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> column: `l`.</span></span>
|
|||
|
<span id="cb38-4"><a href="#cb38-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> NULL</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb39"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb39-1"><a href="#cb39-1" aria-hidden="true" tabindex="-1"></a>df<span class="sc">$</span>not_present</span>
|
|||
|
<span id="cb39-2"><a href="#cb39-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> NULL</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb40"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb40-1"><a href="#cb40-1" aria-hidden="true" tabindex="-1"></a>tbl<span class="sc">$</span>not_present</span>
|
|||
|
<span id="cb40-2"><a href="#cb40-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Warning: Unknown or uninitialised</span></span>
|
|||
|
<span id="cb40-3"><a href="#cb40-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> column: `not_present`.</span></span>
|
|||
|
<span id="cb40-4"><a href="#cb40-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> NULL</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<div id="column-subsetting" class="section level2">
|
|||
|
<h2>Column subsetting</h2>
|
|||
|
<div id="definition-of-xj-1" class="section level3">
|
|||
|
<h3>Definition of <code>x[j]</code></h3>
|
|||
|
<p><code>j</code> is converted to an integer vector by
|
|||
|
<code>vec_as_index(j, ncol(x), names = names(x))</code>. Then
|
|||
|
<code>x[c(j_1, j_2, ..., j_n)]</code> is equivalent to
|
|||
|
<code>tibble(x[[j_1]], x[[j_2]], ..., x[[j_n]])</code>, keeping the
|
|||
|
corresponding column names. This implies that <code>j</code> must be a
|
|||
|
numeric or character vector, or a logical vector with length 1 or
|
|||
|
<code>ncol(x)</code>.<a href="#fn1" class="footnote-ref" id="fnref1"><sup>1</sup></a></p>
|
|||
|
<table class="dftbl">
|
|||
|
<tbody>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb41"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb41-1"><a href="#cb41-1" aria-hidden="true" tabindex="-1"></a>tbl[<span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>]</span>
|
|||
|
<span id="cb41-2"><a href="#cb41-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 4 × 2</span></span>
|
|||
|
<span id="cb41-3"><a href="#cb41-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c </span></span>
|
|||
|
<span id="cb41-4"><a href="#cb41-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int> <chr></span></span>
|
|||
|
<span id="cb41-5"><a href="#cb41-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e </span></span>
|
|||
|
<span id="cb41-6"><a href="#cb41-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 NA f </span></span>
|
|||
|
<span id="cb41-7"><a href="#cb41-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 3 g </span></span>
|
|||
|
<span id="cb41-8"><a href="#cb41-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 NA h</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
<p>When subsetting repeated indexes, the resulting column names are
|
|||
|
undefined, do not rely on them.</p>
|
|||
|
<table class="dftbl">
|
|||
|
<tbody>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb42"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb42-1"><a href="#cb42-1" aria-hidden="true" tabindex="-1"></a>df[<span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">1</span>)]</span>
|
|||
|
<span id="cb42-2"><a href="#cb42-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> n n.1</span></span>
|
|||
|
<span id="cb42-3"><a href="#cb42-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 1</span></span>
|
|||
|
<span id="cb42-4"><a href="#cb42-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 NA NA</span></span>
|
|||
|
<span id="cb42-5"><a href="#cb42-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 3 3</span></span>
|
|||
|
<span id="cb42-6"><a href="#cb42-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 NA NA</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb43"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb43-1"><a href="#cb43-1" aria-hidden="true" tabindex="-1"></a>tbl[<span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">1</span>)]</span>
|
|||
|
<span id="cb43-2"><a href="#cb43-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 4 × 2</span></span>
|
|||
|
<span id="cb43-3"><a href="#cb43-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n n</span></span>
|
|||
|
<span id="cb43-4"><a href="#cb43-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int> <int></span></span>
|
|||
|
<span id="cb43-5"><a href="#cb43-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 1</span></span>
|
|||
|
<span id="cb43-6"><a href="#cb43-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 NA NA</span></span>
|
|||
|
<span id="cb43-7"><a href="#cb43-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 3 3</span></span>
|
|||
|
<span id="cb43-8"><a href="#cb43-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 NA NA</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
<p>For tibbles with repeated column names, subsetting by name uses the
|
|||
|
first matching column.</p>
|
|||
|
<p><code>nrow(df[j])</code> equals <code>nrow(df)</code>.</p>
|
|||
|
<table class="dftbl">
|
|||
|
<tbody>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb44"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb44-1"><a href="#cb44-1" aria-hidden="true" tabindex="-1"></a>tbl[<span class="fu">integer</span>()]</span>
|
|||
|
<span id="cb44-2"><a href="#cb44-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 4 × 0</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
<p>Tibbles support indexing by a logical matrix, but only if all values
|
|||
|
in the returned vector are compatible.</p>
|
|||
|
<table class="dftbl">
|
|||
|
<tbody>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb45"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb45-1"><a href="#cb45-1" aria-hidden="true" tabindex="-1"></a>df[<span class="fu">is.na</span>(df)]</span>
|
|||
|
<span id="cb45-2"><a href="#cb45-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> [[1]]</span></span>
|
|||
|
<span id="cb45-3"><a href="#cb45-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] NA</span></span>
|
|||
|
<span id="cb45-4"><a href="#cb45-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> </span></span>
|
|||
|
<span id="cb45-5"><a href="#cb45-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> [[2]]</span></span>
|
|||
|
<span id="cb45-6"><a href="#cb45-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] NA</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb46"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb46-1"><a href="#cb46-1" aria-hidden="true" tabindex="-1"></a>tbl[<span class="fu">is.na</span>(tbl)]</span>
|
|||
|
<span id="cb46-2"><a href="#cb46-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] NA NA</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb47"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb47-1"><a href="#cb47-1" aria-hidden="true" tabindex="-1"></a>df[<span class="sc">!</span><span class="fu">is.na</span>(df)]</span>
|
|||
|
<span id="cb47-2"><a href="#cb47-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> [[1]]</span></span>
|
|||
|
<span id="cb47-3"><a href="#cb47-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] 1</span></span>
|
|||
|
<span id="cb47-4"><a href="#cb47-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> </span></span>
|
|||
|
<span id="cb47-5"><a href="#cb47-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> [[2]]</span></span>
|
|||
|
<span id="cb47-6"><a href="#cb47-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] 3</span></span>
|
|||
|
<span id="cb47-7"><a href="#cb47-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> </span></span>
|
|||
|
<span id="cb47-8"><a href="#cb47-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> [[3]]</span></span>
|
|||
|
<span id="cb47-9"><a href="#cb47-9" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] "e"</span></span>
|
|||
|
<span id="cb47-10"><a href="#cb47-10" aria-hidden="true" tabindex="-1"></a><span class="co">#> </span></span>
|
|||
|
<span id="cb47-11"><a href="#cb47-11" aria-hidden="true" tabindex="-1"></a><span class="co">#> [[4]]</span></span>
|
|||
|
<span id="cb47-12"><a href="#cb47-12" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] "f"</span></span>
|
|||
|
<span id="cb47-13"><a href="#cb47-13" aria-hidden="true" tabindex="-1"></a><span class="co">#> </span></span>
|
|||
|
<span id="cb47-14"><a href="#cb47-14" aria-hidden="true" tabindex="-1"></a><span class="co">#> [[5]]</span></span>
|
|||
|
<span id="cb47-15"><a href="#cb47-15" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] "g"</span></span>
|
|||
|
<span id="cb47-16"><a href="#cb47-16" aria-hidden="true" tabindex="-1"></a><span class="co">#> </span></span>
|
|||
|
<span id="cb47-17"><a href="#cb47-17" aria-hidden="true" tabindex="-1"></a><span class="co">#> [[6]]</span></span>
|
|||
|
<span id="cb47-18"><a href="#cb47-18" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] "h"</span></span>
|
|||
|
<span id="cb47-19"><a href="#cb47-19" aria-hidden="true" tabindex="-1"></a><span class="co">#> </span></span>
|
|||
|
<span id="cb47-20"><a href="#cb47-20" aria-hidden="true" tabindex="-1"></a><span class="co">#> [[7]]</span></span>
|
|||
|
<span id="cb47-21"><a href="#cb47-21" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] 9</span></span>
|
|||
|
<span id="cb47-22"><a href="#cb47-22" aria-hidden="true" tabindex="-1"></a><span class="co">#> </span></span>
|
|||
|
<span id="cb47-23"><a href="#cb47-23" aria-hidden="true" tabindex="-1"></a><span class="co">#> [[8]]</span></span>
|
|||
|
<span id="cb47-24"><a href="#cb47-24" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] 10 11</span></span>
|
|||
|
<span id="cb47-25"><a href="#cb47-25" aria-hidden="true" tabindex="-1"></a><span class="co">#> </span></span>
|
|||
|
<span id="cb47-26"><a href="#cb47-26" aria-hidden="true" tabindex="-1"></a><span class="co">#> [[9]]</span></span>
|
|||
|
<span id="cb47-27"><a href="#cb47-27" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] 12 13 14</span></span>
|
|||
|
<span id="cb47-28"><a href="#cb47-28" aria-hidden="true" tabindex="-1"></a><span class="co">#> </span></span>
|
|||
|
<span id="cb47-29"><a href="#cb47-29" aria-hidden="true" tabindex="-1"></a><span class="co">#> [[10]]</span></span>
|
|||
|
<span id="cb47-30"><a href="#cb47-30" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] "text"</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb48"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb48-1"><a href="#cb48-1" aria-hidden="true" tabindex="-1"></a>tbl[<span class="sc">!</span><span class="fu">is.na</span>(tbl)]</span>
|
|||
|
<span id="cb48-2"><a href="#cb48-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in `vec_c()` at ]8;line = 12:col = 2;file:///private/var/folders/dj/yhk9rkx97wn_ykqtnmk18xvc0000gn/T/RtmpECx3vm/Rbuild51ca1b126fa/tibble/R/subsetting-matrix.Rtibble/R/subsetting-matrix.R:12:2]8;;:</span></span>
|
|||
|
<span id="cb48-3"><a href="#cb48-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> ! Can't combine `n` <integer> and `c` <character>.</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
</div>
|
|||
|
<div id="definition-of-x-j" class="section level3">
|
|||
|
<h3>Definition of <code>x[, j]</code></h3>
|
|||
|
<p><code>x[, j]</code> is equal to <code>x[j]</code>. Tibbles do not
|
|||
|
perform column extraction if <code>x[j]</code> would yield a single
|
|||
|
column.</p>
|
|||
|
<table class="dftbl">
|
|||
|
<tbody>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb49"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb49-1"><a href="#cb49-1" aria-hidden="true" tabindex="-1"></a>df[, <span class="dv">1</span>]</span>
|
|||
|
<span id="cb49-2"><a href="#cb49-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] 1 NA 3 NA</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb50"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb50-1"><a href="#cb50-1" aria-hidden="true" tabindex="-1"></a>tbl[, <span class="dv">1</span>]</span>
|
|||
|
<span id="cb50-2"><a href="#cb50-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 4 × 1</span></span>
|
|||
|
<span id="cb50-3"><a href="#cb50-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n</span></span>
|
|||
|
<span id="cb50-4"><a href="#cb50-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int></span></span>
|
|||
|
<span id="cb50-5"><a href="#cb50-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1</span></span>
|
|||
|
<span id="cb50-6"><a href="#cb50-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 NA</span></span>
|
|||
|
<span id="cb50-7"><a href="#cb50-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 3</span></span>
|
|||
|
<span id="cb50-8"><a href="#cb50-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 NA</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb51"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb51-1"><a href="#cb51-1" aria-hidden="true" tabindex="-1"></a>tbl[, <span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>]</span>
|
|||
|
<span id="cb51-2"><a href="#cb51-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 4 × 2</span></span>
|
|||
|
<span id="cb51-3"><a href="#cb51-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c </span></span>
|
|||
|
<span id="cb51-4"><a href="#cb51-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int> <chr></span></span>
|
|||
|
<span id="cb51-5"><a href="#cb51-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e </span></span>
|
|||
|
<span id="cb51-6"><a href="#cb51-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 NA f </span></span>
|
|||
|
<span id="cb51-7"><a href="#cb51-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 3 g </span></span>
|
|||
|
<span id="cb51-8"><a href="#cb51-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 NA h</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
</div>
|
|||
|
<div id="definition-of-x-j-drop-true" class="section level3">
|
|||
|
<h3>Definition of <code>x[, j, drop = TRUE]</code></h3>
|
|||
|
<p>For backward compatiblity, <code>x[, j, drop = TRUE]</code> performs
|
|||
|
column <strong>extraction</strong>, returning <code>x[j][[1]]</code>
|
|||
|
when <code>ncol(x[j])</code> is 1.</p>
|
|||
|
<table class="dftbl">
|
|||
|
<tbody>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb52"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb52-1"><a href="#cb52-1" aria-hidden="true" tabindex="-1"></a>tbl[, <span class="dv">1</span>, drop <span class="ot">=</span> <span class="cn">TRUE</span>]</span>
|
|||
|
<span id="cb52-2"><a href="#cb52-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] 1 NA 3 NA</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<div id="row-subsetting" class="section level2">
|
|||
|
<h2>Row subsetting</h2>
|
|||
|
<div id="definition-of-xi" class="section level3">
|
|||
|
<h3>Definition of <code>x[i, ]</code></h3>
|
|||
|
<p><code>x[i, ]</code> is equal to
|
|||
|
<code>tibble(vec_slice(x[[1]], i), vec_slice(x[[2]], i), ...)</code>.<a href="#fn2" class="footnote-ref" id="fnref2"><sup>2</sup></a></p>
|
|||
|
<table class="dftbl">
|
|||
|
<tbody>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb53"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb53-1"><a href="#cb53-1" aria-hidden="true" tabindex="-1"></a>tbl[<span class="dv">3</span>, ]</span>
|
|||
|
<span id="cb53-2"><a href="#cb53-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 1 × 3</span></span>
|
|||
|
<span id="cb53-3"><a href="#cb53-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li </span></span>
|
|||
|
<span id="cb53-4"><a href="#cb53-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int> <chr> <list> </span></span>
|
|||
|
<span id="cb53-5"><a href="#cb53-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 3 g <int [3]></span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
<p>This means that <code>i</code> must be a numeric vector, or a logical
|
|||
|
vector of length <code>nrow(x)</code> or 1. For compatibility,
|
|||
|
<code>i</code> can also be a character vector containing positive
|
|||
|
numbers.</p>
|
|||
|
<table class="dftbl">
|
|||
|
<tbody>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb54"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb54-1"><a href="#cb54-1" aria-hidden="true" tabindex="-1"></a>df[mean, ]</span>
|
|||
|
<span id="cb54-2"><a href="#cb54-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in xj[i]: invalid subscript type 'closure'</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb55"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb55-1"><a href="#cb55-1" aria-hidden="true" tabindex="-1"></a>tbl[mean, ]</span>
|
|||
|
<span id="cb55-2"><a href="#cb55-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in `tbl[mean, ]`:</span></span>
|
|||
|
<span id="cb55-3"><a href="#cb55-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> ! Can't subset rows with `mean`.</span></span>
|
|||
|
<span id="cb55-4"><a href="#cb55-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> ✖ `mean` must be logical, numeric, or character, not a function.</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb56"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb56-1"><a href="#cb56-1" aria-hidden="true" tabindex="-1"></a>df[<span class="fu">list</span>(<span class="dv">1</span>), ]</span>
|
|||
|
<span id="cb56-2"><a href="#cb56-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in xj[i]: invalid subscript type 'list'</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb57"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb57-1"><a href="#cb57-1" aria-hidden="true" tabindex="-1"></a>tbl[<span class="fu">list</span>(<span class="dv">1</span>), ]</span>
|
|||
|
<span id="cb57-2"><a href="#cb57-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in `tbl[list(1), ]`:</span></span>
|
|||
|
<span id="cb57-3"><a href="#cb57-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> ! Can't subset rows with `list(1)`.</span></span>
|
|||
|
<span id="cb57-4"><a href="#cb57-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> ✖ `list(1)` must be logical, numeric, or character, not a list.</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb58"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb58-1"><a href="#cb58-1" aria-hidden="true" tabindex="-1"></a>tbl[<span class="st">"1"</span>, ]</span>
|
|||
|
<span id="cb58-2"><a href="#cb58-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 1 × 3</span></span>
|
|||
|
<span id="cb58-3"><a href="#cb58-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li </span></span>
|
|||
|
<span id="cb58-4"><a href="#cb58-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int> <chr> <list> </span></span>
|
|||
|
<span id="cb58-5"><a href="#cb58-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e <dbl [1]></span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
<p>Exception: OOB values generate warnings instead of errors:</p>
|
|||
|
<table class="dftbl">
|
|||
|
<tbody>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb59"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb59-1"><a href="#cb59-1" aria-hidden="true" tabindex="-1"></a>tbl[<span class="dv">10</span>, ]</span>
|
|||
|
<span id="cb59-2"><a href="#cb59-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 1 × 3</span></span>
|
|||
|
<span id="cb59-3"><a href="#cb59-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li </span></span>
|
|||
|
<span id="cb59-4"><a href="#cb59-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int> <chr> <list></span></span>
|
|||
|
<span id="cb59-5"><a href="#cb59-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 NA <NA> <NULL></span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb60"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb60-1"><a href="#cb60-1" aria-hidden="true" tabindex="-1"></a>tbl[<span class="st">"x"</span>, ]</span>
|
|||
|
<span id="cb60-2"><a href="#cb60-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 1 × 3</span></span>
|
|||
|
<span id="cb60-3"><a href="#cb60-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li </span></span>
|
|||
|
<span id="cb60-4"><a href="#cb60-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int> <chr> <list></span></span>
|
|||
|
<span id="cb60-5"><a href="#cb60-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 NA <NA> <NULL></span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
<p>Unlike data frames, only logical vectors of length 1 are recycled.
|
|||
|
<!-- TODO: should this be an error? #648 --></p>
|
|||
|
<table class="dftbl">
|
|||
|
<tbody>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb61"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb61-1"><a href="#cb61-1" aria-hidden="true" tabindex="-1"></a>df[<span class="fu">c</span>(<span class="cn">TRUE</span>, <span class="cn">FALSE</span>), ]</span>
|
|||
|
<span id="cb61-2"><a href="#cb61-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li</span></span>
|
|||
|
<span id="cb61-3"><a href="#cb61-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e 9</span></span>
|
|||
|
<span id="cb61-4"><a href="#cb61-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 3 g 12, 13, 14</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb62"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb62-1"><a href="#cb62-1" aria-hidden="true" tabindex="-1"></a>tbl[<span class="fu">c</span>(<span class="cn">TRUE</span>, <span class="cn">FALSE</span>), ]</span>
|
|||
|
<span id="cb62-2"><a href="#cb62-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in `tbl[c(TRUE, FALSE), ]`:</span></span>
|
|||
|
<span id="cb62-3"><a href="#cb62-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> ! Can't subset rows with `c(TRUE, FALSE)`.</span></span>
|
|||
|
<span id="cb62-4"><a href="#cb62-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> ✖ Logical subscript `c(TRUE, FALSE)` must be size 1 or 4, not 2.</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
<p>NB: scalar logicals are recycled, but scalar numerics are not. That
|
|||
|
makes the <code>x[NA, ]</code> and <code>x[NA_integer_, ]</code> return
|
|||
|
different results.</p>
|
|||
|
<table class="dftbl">
|
|||
|
<tbody>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb63"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb63-1"><a href="#cb63-1" aria-hidden="true" tabindex="-1"></a>tbl[<span class="cn">NA</span>, ]</span>
|
|||
|
<span id="cb63-2"><a href="#cb63-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 4 × 3</span></span>
|
|||
|
<span id="cb63-3"><a href="#cb63-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li </span></span>
|
|||
|
<span id="cb63-4"><a href="#cb63-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int> <chr> <list></span></span>
|
|||
|
<span id="cb63-5"><a href="#cb63-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 NA <NA> <NULL></span></span>
|
|||
|
<span id="cb63-6"><a href="#cb63-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 NA <NA> <NULL></span></span>
|
|||
|
<span id="cb63-7"><a href="#cb63-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 NA <NA> <NULL></span></span>
|
|||
|
<span id="cb63-8"><a href="#cb63-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 NA <NA> <NULL></span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb64"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb64-1"><a href="#cb64-1" aria-hidden="true" tabindex="-1"></a>tbl[<span class="cn">NA_integer_</span>, ]</span>
|
|||
|
<span id="cb64-2"><a href="#cb64-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 1 × 3</span></span>
|
|||
|
<span id="cb64-3"><a href="#cb64-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li </span></span>
|
|||
|
<span id="cb64-4"><a href="#cb64-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int> <chr> <list></span></span>
|
|||
|
<span id="cb64-5"><a href="#cb64-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 NA <NA> <NULL></span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
</div>
|
|||
|
<div id="definition-of-xi-drop-true" class="section level3">
|
|||
|
<h3>Definition of <code>x[i, , drop = TRUE]</code></h3>
|
|||
|
<p><code>drop = TRUE</code> has no effect when not selecting a single
|
|||
|
row:</p>
|
|||
|
<table class="dftbl">
|
|||
|
<tbody>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb65"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb65-1"><a href="#cb65-1" aria-hidden="true" tabindex="-1"></a>df[<span class="dv">1</span>, , drop <span class="ot">=</span> <span class="cn">TRUE</span>]</span>
|
|||
|
<span id="cb65-2"><a href="#cb65-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> $n</span></span>
|
|||
|
<span id="cb65-3"><a href="#cb65-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] 1</span></span>
|
|||
|
<span id="cb65-4"><a href="#cb65-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> </span></span>
|
|||
|
<span id="cb65-5"><a href="#cb65-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> $c</span></span>
|
|||
|
<span id="cb65-6"><a href="#cb65-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] "e"</span></span>
|
|||
|
<span id="cb65-7"><a href="#cb65-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> </span></span>
|
|||
|
<span id="cb65-8"><a href="#cb65-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> $li</span></span>
|
|||
|
<span id="cb65-9"><a href="#cb65-9" aria-hidden="true" tabindex="-1"></a><span class="co">#> $li[[1]]</span></span>
|
|||
|
<span id="cb65-10"><a href="#cb65-10" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] 9</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb66"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb66-1"><a href="#cb66-1" aria-hidden="true" tabindex="-1"></a>tbl[<span class="dv">1</span>, , drop <span class="ot">=</span> <span class="cn">TRUE</span>]</span>
|
|||
|
<span id="cb66-2"><a href="#cb66-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 1 × 3</span></span>
|
|||
|
<span id="cb66-3"><a href="#cb66-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li </span></span>
|
|||
|
<span id="cb66-4"><a href="#cb66-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int> <chr> <list> </span></span>
|
|||
|
<span id="cb66-5"><a href="#cb66-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e <dbl [1]></span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
<!-- TODO: soft-deprecate -->
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<div id="row-and-column-subsetting" class="section level2">
|
|||
|
<h2>Row and column subsetting</h2>
|
|||
|
<div id="definition-of-x-and-x" class="section level3">
|
|||
|
<h3>Definition of <code>x[]</code> and <code>x[,]</code></h3>
|
|||
|
<p><code>x[]</code> and <code>x[,]</code> are equivalent to
|
|||
|
<code>x</code>.<a href="#fn3" class="footnote-ref" id="fnref3"><sup>3</sup></a></p>
|
|||
|
</div>
|
|||
|
<div id="definition-of-xi-j" class="section level3">
|
|||
|
<h3>Definition of <code>x[i, j]</code></h3>
|
|||
|
<p><code>x[i, j]</code> is equal to <code>x[i, ][j]</code>.<a href="#fn4" class="footnote-ref" id="fnref4"><sup>4</sup></a></p>
|
|||
|
</div>
|
|||
|
<div id="definition-of-xi-j-1" class="section level3">
|
|||
|
<h3>Definition of <code>x[[i, j]]</code></h3>
|
|||
|
<p><code>i</code> must be a numeric vector of length 1.
|
|||
|
<code>x[[i, j]]</code> is equal to <code>x[i, ][[j]]</code>, or
|
|||
|
<code>vctrs::vec_slice(x[[j]], i)</code>.<a href="#fn5" class="footnote-ref" id="fnref5"><sup>5</sup></a></p>
|
|||
|
<div class="sourceCode" id="cb67"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb67-1"><a href="#cb67-1" aria-hidden="true" tabindex="-1"></a>df[[<span class="dv">1</span>, <span class="dv">1</span>]]</span>
|
|||
|
<span id="cb67-2"><a href="#cb67-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] 1</span></span>
|
|||
|
<span id="cb67-3"><a href="#cb67-3" aria-hidden="true" tabindex="-1"></a>df[[<span class="dv">1</span>, <span class="dv">3</span>]]</span>
|
|||
|
<span id="cb67-4"><a href="#cb67-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] 9</span></span></code></pre></div>
|
|||
|
<p>This implies that <code>j</code> must be a numeric or character
|
|||
|
vector of length 1.</p>
|
|||
|
<p>NB: <code>vec_size(x[[i, j]])</code> always equals 1. Unlike
|
|||
|
<code>x[i, ]</code>, <code>x[[i, ]]</code> is not valid.</p>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<div id="column-update" class="section level2">
|
|||
|
<h2>Column update</h2>
|
|||
|
<div id="definition-of-xj---a" class="section level3">
|
|||
|
<h3>Definition of <code>x[[j]] <- a</code></h3>
|
|||
|
<p>If <code>a</code> is a vector then <code>x[[j]] <- a</code>
|
|||
|
replaces the <code>j</code>th column with value <code>a</code>.</p>
|
|||
|
<p><code>a</code> is recycled to the same size as <code>x</code> so must
|
|||
|
have size <code>nrow(x)</code> or 1. (The only exception is when
|
|||
|
<code>a</code> is <code>NULL</code>, as described below.) Recycling also
|
|||
|
works for list, data frame, and matrix columns.</p>
|
|||
|
<table class="dftbl">
|
|||
|
<tbody>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb68"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb68-1"><a href="#cb68-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[[<span class="st">"li"</span>]] <span class="ot"><-</span> <span class="fu">list</span>(<span class="dv">0</span>))</span>
|
|||
|
<span id="cb68-2"><a href="#cb68-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 4 × 3</span></span>
|
|||
|
<span id="cb68-3"><a href="#cb68-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li </span></span>
|
|||
|
<span id="cb68-4"><a href="#cb68-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int> <chr> <list> </span></span>
|
|||
|
<span id="cb68-5"><a href="#cb68-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e <dbl [1]></span></span>
|
|||
|
<span id="cb68-6"><a href="#cb68-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 NA f <dbl [1]></span></span>
|
|||
|
<span id="cb68-7"><a href="#cb68-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 3 g <dbl [1]></span></span>
|
|||
|
<span id="cb68-8"><a href="#cb68-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 NA h <dbl [1]></span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb69"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb69-1"><a href="#cb69-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_df2</span>(df2[[<span class="st">"tb"</span>]] <span class="ot"><-</span> df[<span class="dv">1</span>, ])</span>
|
|||
|
<span id="cb69-2"><a href="#cb69-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in `[[<-.data.frame`(`*tmp*`, "tb", value = structure(list(n = 1L, : replacement has 1 row, data has 4</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb70"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb70-1"><a href="#cb70-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl2</span>(tbl2[[<span class="st">"tb"</span>]] <span class="ot"><-</span> tbl[<span class="dv">1</span>, ])</span>
|
|||
|
<span id="cb70-2"><a href="#cb70-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 4 × 2</span></span>
|
|||
|
<span id="cb70-3"><a href="#cb70-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> tb$n $c $li m[,1] [,2]</span></span>
|
|||
|
<span id="cb70-4"><a href="#cb70-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int> <chr> <list> <dbl> <dbl></span></span>
|
|||
|
<span id="cb70-5"><a href="#cb70-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e <dbl [1]> 1 0</span></span>
|
|||
|
<span id="cb70-6"><a href="#cb70-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 1 e <dbl [1]> 0 1</span></span>
|
|||
|
<span id="cb70-7"><a href="#cb70-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 1 e <dbl [1]> 0 0</span></span>
|
|||
|
<span id="cb70-8"><a href="#cb70-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 1 e <dbl [1]> 0 0</span></span>
|
|||
|
<span id="cb70-9"><a href="#cb70-9" aria-hidden="true" tabindex="-1"></a><span class="co">#> # … with 1 more variable:</span></span>
|
|||
|
<span id="cb70-10"><a href="#cb70-10" aria-hidden="true" tabindex="-1"></a><span class="co">#> # m[3:4] <dbl></span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb71"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb71-1"><a href="#cb71-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_df2</span>(df2[[<span class="st">"m"</span>]] <span class="ot"><-</span> df2[[<span class="st">"m"</span>]][<span class="dv">1</span>, , <span class="at">drop =</span> <span class="cn">FALSE</span>])</span>
|
|||
|
<span id="cb71-2"><a href="#cb71-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in `[[<-.data.frame`(`*tmp*`, "m", value = structure(c(1, 0, 0, : replacement has 1 row, data has 4</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb72"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb72-1"><a href="#cb72-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl2</span>(tbl2[[<span class="st">"m"</span>]] <span class="ot"><-</span> tbl2[[<span class="st">"m"</span>]][<span class="dv">1</span>, , <span class="at">drop =</span> <span class="cn">FALSE</span>])</span>
|
|||
|
<span id="cb72-2"><a href="#cb72-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 4 × 2</span></span>
|
|||
|
<span id="cb72-3"><a href="#cb72-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> tb$n $c $li m[,1] [,2]</span></span>
|
|||
|
<span id="cb72-4"><a href="#cb72-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int> <chr> <list> <dbl> <dbl></span></span>
|
|||
|
<span id="cb72-5"><a href="#cb72-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e <dbl [1]> 1 0</span></span>
|
|||
|
<span id="cb72-6"><a href="#cb72-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 NA f <int [2]> 1 0</span></span>
|
|||
|
<span id="cb72-7"><a href="#cb72-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 3 g <int [3]> 1 0</span></span>
|
|||
|
<span id="cb72-8"><a href="#cb72-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 NA h <chr [1]> 1 0</span></span>
|
|||
|
<span id="cb72-9"><a href="#cb72-9" aria-hidden="true" tabindex="-1"></a><span class="co">#> # … with 1 more variable:</span></span>
|
|||
|
<span id="cb72-10"><a href="#cb72-10" aria-hidden="true" tabindex="-1"></a><span class="co">#> # m[3:4] <dbl></span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
<p><code>j</code> must be a scalar numeric or a string, and cannot be
|
|||
|
<code>NA</code>. If <code>j</code> is OOB, a new column is added on the
|
|||
|
right hand side, with name repair if needed.</p>
|
|||
|
<table class="dftbl">
|
|||
|
<tbody>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb73"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb73-1"><a href="#cb73-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[[<span class="st">"x"</span>]] <span class="ot"><-</span> <span class="dv">0</span>)</span>
|
|||
|
<span id="cb73-2"><a href="#cb73-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 4 × 4</span></span>
|
|||
|
<span id="cb73-3"><a href="#cb73-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li x</span></span>
|
|||
|
<span id="cb73-4"><a href="#cb73-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int> <chr> <list> <dbl></span></span>
|
|||
|
<span id="cb73-5"><a href="#cb73-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e <dbl [1]> 0</span></span>
|
|||
|
<span id="cb73-6"><a href="#cb73-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 NA f <int [2]> 0</span></span>
|
|||
|
<span id="cb73-7"><a href="#cb73-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 3 g <int [3]> 0</span></span>
|
|||
|
<span id="cb73-8"><a href="#cb73-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 NA h <chr [1]> 0</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb74"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb74-1"><a href="#cb74-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_df</span>(df[[<span class="dv">4</span>]] <span class="ot"><-</span> <span class="dv">0</span>)</span>
|
|||
|
<span id="cb74-2"><a href="#cb74-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li V4</span></span>
|
|||
|
<span id="cb74-3"><a href="#cb74-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e 9 0</span></span>
|
|||
|
<span id="cb74-4"><a href="#cb74-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 NA f 10, 11 0</span></span>
|
|||
|
<span id="cb74-5"><a href="#cb74-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 3 g 12, 13, 14 0</span></span>
|
|||
|
<span id="cb74-6"><a href="#cb74-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 NA h text 0</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb75"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb75-1"><a href="#cb75-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[[<span class="dv">4</span>]] <span class="ot"><-</span> <span class="dv">0</span>)</span>
|
|||
|
<span id="cb75-2"><a href="#cb75-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 4 × 4</span></span>
|
|||
|
<span id="cb75-3"><a href="#cb75-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li ...4</span></span>
|
|||
|
<span id="cb75-4"><a href="#cb75-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int> <chr> <list> <dbl></span></span>
|
|||
|
<span id="cb75-5"><a href="#cb75-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e <dbl [1]> 0</span></span>
|
|||
|
<span id="cb75-6"><a href="#cb75-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 NA f <int [2]> 0</span></span>
|
|||
|
<span id="cb75-7"><a href="#cb75-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 3 g <int [3]> 0</span></span>
|
|||
|
<span id="cb75-8"><a href="#cb75-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 NA h <chr [1]> 0</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb76"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb76-1"><a href="#cb76-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_df</span>(df[[<span class="dv">5</span>]] <span class="ot"><-</span> <span class="dv">0</span>)</span>
|
|||
|
<span id="cb76-2"><a href="#cb76-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Warning in format.data.frame(if</span></span>
|
|||
|
<span id="cb76-3"><a href="#cb76-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> (omit) x[seq_len(n0), , drop =</span></span>
|
|||
|
<span id="cb76-4"><a href="#cb76-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> FALSE] else x, : corrupt data</span></span>
|
|||
|
<span id="cb76-5"><a href="#cb76-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> frame: columns will be truncated or</span></span>
|
|||
|
<span id="cb76-6"><a href="#cb76-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> padded with NAs</span></span>
|
|||
|
<span id="cb76-7"><a href="#cb76-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li V5</span></span>
|
|||
|
<span id="cb76-8"><a href="#cb76-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e 9 NULL 0</span></span>
|
|||
|
<span id="cb76-9"><a href="#cb76-9" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 NA f 10, 11 <NA> 0</span></span>
|
|||
|
<span id="cb76-10"><a href="#cb76-10" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 3 g 12, 13, 14 <NA> 0</span></span>
|
|||
|
<span id="cb76-11"><a href="#cb76-11" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 NA h text <NA> 0</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb77"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb77-1"><a href="#cb77-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[[<span class="dv">5</span>]] <span class="ot"><-</span> <span class="dv">0</span>)</span>
|
|||
|
<span id="cb77-2"><a href="#cb77-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in `[[<-`:</span></span>
|
|||
|
<span id="cb77-3"><a href="#cb77-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> ! Can't assign to columns beyond the end with non-consecutive locations.</span></span>
|
|||
|
<span id="cb77-4"><a href="#cb77-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> ℹ Input has size 3.</span></span>
|
|||
|
<span id="cb77-5"><a href="#cb77-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> ✖ Subscript `5` contains non-consecutive location 5.</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
<!-- HW: should we permitted oob assignment with numeric j? It's a bit weird to create a column with unknonw column -->
|
|||
|
<p><code>df[[j]] <- a</code> replaces the complete column so can
|
|||
|
change the type.</p>
|
|||
|
<p><code>[[<-</code> supports removing a column by assigning
|
|||
|
<code>NULL</code> to it.</p>
|
|||
|
<p>Removing a nonexistent column is a no-op.</p>
|
|||
|
</div>
|
|||
|
<div id="definition-of-xname---a" class="section level3">
|
|||
|
<h3>Definition of <code>x$name <- a</code></h3>
|
|||
|
<p><code>x$name <- a</code> and <code>x$"name" <- a</code> are
|
|||
|
equivalent to <code>x[["name"]] <- a</code>.<a href="#fn6" class="footnote-ref" id="fnref6"><sup>6</sup></a></p>
|
|||
|
<table class="dftbl">
|
|||
|
<tbody>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb78"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb78-1"><a href="#cb78-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl<span class="sc">$</span>n <span class="ot"><-</span> <span class="dv">0</span>)</span>
|
|||
|
<span id="cb78-2"><a href="#cb78-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 4 × 3</span></span>
|
|||
|
<span id="cb78-3"><a href="#cb78-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li </span></span>
|
|||
|
<span id="cb78-4"><a href="#cb78-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <dbl> <chr> <list> </span></span>
|
|||
|
<span id="cb78-5"><a href="#cb78-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 0 e <dbl [1]></span></span>
|
|||
|
<span id="cb78-6"><a href="#cb78-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 0 f <int [2]></span></span>
|
|||
|
<span id="cb78-7"><a href="#cb78-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 0 g <int [3]></span></span>
|
|||
|
<span id="cb78-8"><a href="#cb78-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 0 h <chr [1]></span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb79"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb79-1"><a href="#cb79-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[[<span class="st">"n"</span>]] <span class="ot"><-</span> <span class="dv">0</span>)</span>
|
|||
|
<span id="cb79-2"><a href="#cb79-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 4 × 3</span></span>
|
|||
|
<span id="cb79-3"><a href="#cb79-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li </span></span>
|
|||
|
<span id="cb79-4"><a href="#cb79-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <dbl> <chr> <list> </span></span>
|
|||
|
<span id="cb79-5"><a href="#cb79-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 0 e <dbl [1]></span></span>
|
|||
|
<span id="cb79-6"><a href="#cb79-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 0 f <int [2]></span></span>
|
|||
|
<span id="cb79-7"><a href="#cb79-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 0 g <int [3]></span></span>
|
|||
|
<span id="cb79-8"><a href="#cb79-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 0 h <chr [1]></span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
<p><code>$<-</code> does not perform partial matching.</p>
|
|||
|
<table class="dftbl">
|
|||
|
<tbody>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb80"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb80-1"><a href="#cb80-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl<span class="sc">$</span>l <span class="ot"><-</span> <span class="dv">0</span>)</span>
|
|||
|
<span id="cb80-2"><a href="#cb80-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 4 × 4</span></span>
|
|||
|
<span id="cb80-3"><a href="#cb80-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li l</span></span>
|
|||
|
<span id="cb80-4"><a href="#cb80-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int> <chr> <list> <dbl></span></span>
|
|||
|
<span id="cb80-5"><a href="#cb80-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e <dbl [1]> 0</span></span>
|
|||
|
<span id="cb80-6"><a href="#cb80-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 NA f <int [2]> 0</span></span>
|
|||
|
<span id="cb80-7"><a href="#cb80-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 3 g <int [3]> 0</span></span>
|
|||
|
<span id="cb80-8"><a href="#cb80-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 NA h <chr [1]> 0</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb81"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb81-1"><a href="#cb81-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[[<span class="st">"l"</span>]] <span class="ot"><-</span> <span class="dv">0</span>)</span>
|
|||
|
<span id="cb81-2"><a href="#cb81-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 4 × 4</span></span>
|
|||
|
<span id="cb81-3"><a href="#cb81-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li l</span></span>
|
|||
|
<span id="cb81-4"><a href="#cb81-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int> <chr> <list> <dbl></span></span>
|
|||
|
<span id="cb81-5"><a href="#cb81-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e <dbl [1]> 0</span></span>
|
|||
|
<span id="cb81-6"><a href="#cb81-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 NA f <int [2]> 0</span></span>
|
|||
|
<span id="cb81-7"><a href="#cb81-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 3 g <int [3]> 0</span></span>
|
|||
|
<span id="cb81-8"><a href="#cb81-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 NA h <chr [1]> 0</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<div id="column-subassignment-xj---a" class="section level2">
|
|||
|
<h2>Column subassignment: <code>x[j] <- a</code></h2>
|
|||
|
<ul>
|
|||
|
<li>If <code>j</code> is missing, it’s replaced with
|
|||
|
<code>seq_along(x)</code></li>
|
|||
|
<li>If <code>j</code> is logical vector, it’s converted to numeric with
|
|||
|
<code>seq_along(x)[j]</code>.</li>
|
|||
|
</ul>
|
|||
|
<div id="a-is-a-list-or-data-frame" class="section level3">
|
|||
|
<h3><code>a</code> is a list or data frame</h3>
|
|||
|
<p>If <code>inherits(a, "list")</code> or
|
|||
|
<code>inherits(a, "data.frame")</code> is <code>TRUE</code>, then
|
|||
|
<code>x[j] <- a</code> is equivalent to
|
|||
|
<code>x[[j[[1]]] <- a[[1]]</code>,
|
|||
|
<code>x[[j[[2]]]] <- a[[2]]</code>, …</p>
|
|||
|
<table class="dftbl">
|
|||
|
<tbody>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb82"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb82-1"><a href="#cb82-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>] <span class="ot"><-</span> <span class="fu">list</span>(<span class="st">"x"</span>, <span class="dv">4</span><span class="sc">:</span><span class="dv">1</span>))</span>
|
|||
|
<span id="cb82-2"><a href="#cb82-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 4 × 3</span></span>
|
|||
|
<span id="cb82-3"><a href="#cb82-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li </span></span>
|
|||
|
<span id="cb82-4"><a href="#cb82-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <chr> <int> <list> </span></span>
|
|||
|
<span id="cb82-5"><a href="#cb82-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 x 4 <dbl [1]></span></span>
|
|||
|
<span id="cb82-6"><a href="#cb82-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 x 3 <int [2]></span></span>
|
|||
|
<span id="cb82-7"><a href="#cb82-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 x 2 <int [3]></span></span>
|
|||
|
<span id="cb82-8"><a href="#cb82-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 x 1 <chr [1]></span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb83"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb83-1"><a href="#cb83-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="fu">c</span>(<span class="st">"li"</span>, <span class="st">"x"</span>, <span class="st">"c"</span>)] <span class="ot"><-</span> <span class="fu">list</span>(<span class="st">"x"</span>, <span class="dv">4</span><span class="sc">:</span><span class="dv">1</span>, <span class="cn">NULL</span>))</span>
|
|||
|
<span id="cb83-2"><a href="#cb83-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 4 × 3</span></span>
|
|||
|
<span id="cb83-3"><a href="#cb83-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n li x</span></span>
|
|||
|
<span id="cb83-4"><a href="#cb83-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int> <chr> <int></span></span>
|
|||
|
<span id="cb83-5"><a href="#cb83-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 x 4</span></span>
|
|||
|
<span id="cb83-6"><a href="#cb83-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 NA x 3</span></span>
|
|||
|
<span id="cb83-7"><a href="#cb83-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 3 x 2</span></span>
|
|||
|
<span id="cb83-8"><a href="#cb83-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 NA x 1</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
<p>If <code>length(a)</code> equals 1, then it is recycled to the same
|
|||
|
length as <code>j</code>.</p>
|
|||
|
<table class="dftbl">
|
|||
|
<tbody>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb84"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb84-1"><a href="#cb84-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>] <span class="ot"><-</span> <span class="fu">list</span>(<span class="dv">1</span>))</span>
|
|||
|
<span id="cb84-2"><a href="#cb84-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 4 × 3</span></span>
|
|||
|
<span id="cb84-3"><a href="#cb84-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li </span></span>
|
|||
|
<span id="cb84-4"><a href="#cb84-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <dbl> <dbl> <list> </span></span>
|
|||
|
<span id="cb84-5"><a href="#cb84-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 1 <dbl [1]></span></span>
|
|||
|
<span id="cb84-6"><a href="#cb84-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 1 1 <int [2]></span></span>
|
|||
|
<span id="cb84-7"><a href="#cb84-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 1 1 <int [3]></span></span>
|
|||
|
<span id="cb84-8"><a href="#cb84-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 1 1 <chr [1]></span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb85"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb85-1"><a href="#cb85-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_df</span>(df[<span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>] <span class="ot"><-</span> <span class="fu">list</span>(<span class="dv">0</span>, <span class="dv">0</span>, <span class="dv">0</span>))</span>
|
|||
|
<span id="cb85-2"><a href="#cb85-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Warning in</span></span>
|
|||
|
<span id="cb85-3"><a href="#cb85-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> `[<-.data.frame`(`*tmp*`, 1:2,</span></span>
|
|||
|
<span id="cb85-4"><a href="#cb85-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> value = list(0, 0, 0)): provided 3</span></span>
|
|||
|
<span id="cb85-5"><a href="#cb85-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> variables to replace 2 variables</span></span>
|
|||
|
<span id="cb85-6"><a href="#cb85-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li</span></span>
|
|||
|
<span id="cb85-7"><a href="#cb85-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 0 0 9</span></span>
|
|||
|
<span id="cb85-8"><a href="#cb85-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 0 0 10, 11</span></span>
|
|||
|
<span id="cb85-9"><a href="#cb85-9" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 0 0 12, 13, 14</span></span>
|
|||
|
<span id="cb85-10"><a href="#cb85-10" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 0 0 text</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb86"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb86-1"><a href="#cb86-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>] <span class="ot"><-</span> <span class="fu">list</span>(<span class="dv">0</span>, <span class="dv">0</span>, <span class="dv">0</span>))</span>
|
|||
|
<span id="cb86-2"><a href="#cb86-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in `[<-`:</span></span>
|
|||
|
<span id="cb86-3"><a href="#cb86-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> ! Can't recycle `list(0, 0, 0)` (size 3) to size 2.</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb87"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb87-1"><a href="#cb87-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_df</span>(df[<span class="dv">1</span><span class="sc">:</span><span class="dv">3</span>] <span class="ot"><-</span> <span class="fu">list</span>(<span class="dv">0</span>, <span class="dv">0</span>))</span>
|
|||
|
<span id="cb87-2"><a href="#cb87-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li</span></span>
|
|||
|
<span id="cb87-3"><a href="#cb87-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 0 0 0</span></span>
|
|||
|
<span id="cb87-4"><a href="#cb87-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 0 0 0</span></span>
|
|||
|
<span id="cb87-5"><a href="#cb87-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 0 0 0</span></span>
|
|||
|
<span id="cb87-6"><a href="#cb87-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 0 0 0</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb88"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb88-1"><a href="#cb88-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="dv">1</span><span class="sc">:</span><span class="dv">3</span>] <span class="ot"><-</span> <span class="fu">list</span>(<span class="dv">0</span>, <span class="dv">0</span>))</span>
|
|||
|
<span id="cb88-2"><a href="#cb88-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in `[<-`:</span></span>
|
|||
|
<span id="cb88-3"><a href="#cb88-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> ! Can't recycle `list(0, 0)` (size 2) to size 3.</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
<p>An attempt to update the same column twice gives an error.</p>
|
|||
|
<table class="dftbl">
|
|||
|
<tbody>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb89"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb89-1"><a href="#cb89-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_df</span>(df[<span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">1</span>)] <span class="ot"><-</span> <span class="fu">list</span>(<span class="dv">1</span>, <span class="dv">2</span>))</span>
|
|||
|
<span id="cb89-2"><a href="#cb89-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in `[<-.data.frame`(`*tmp*`, c(1, 1), value = list(1, 2)): duplicate subscripts for columns</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb90"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb90-1"><a href="#cb90-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">1</span>)] <span class="ot"><-</span> <span class="fu">list</span>(<span class="dv">1</span>, <span class="dv">2</span>))</span>
|
|||
|
<span id="cb90-2"><a href="#cb90-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in `[<-`:</span></span>
|
|||
|
<span id="cb90-3"><a href="#cb90-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> ! Column index 1 is used</span></span>
|
|||
|
<span id="cb90-4"><a href="#cb90-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> more than once for assignment.</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
<p>If <code>a</code> contains <code>NULL</code> values, the
|
|||
|
corresponding columns are removed <em>after</em> updating (i.e. position
|
|||
|
indexes refer to columns before any modifications).</p>
|
|||
|
<table class="dftbl">
|
|||
|
<tbody>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb91"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb91-1"><a href="#cb91-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>] <span class="ot"><-</span> <span class="fu">list</span>(<span class="cn">NULL</span>, <span class="dv">4</span><span class="sc">:</span><span class="dv">1</span>))</span>
|
|||
|
<span id="cb91-2"><a href="#cb91-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 4 × 2</span></span>
|
|||
|
<span id="cb91-3"><a href="#cb91-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> c li </span></span>
|
|||
|
<span id="cb91-4"><a href="#cb91-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int> <list> </span></span>
|
|||
|
<span id="cb91-5"><a href="#cb91-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 4 <dbl [1]></span></span>
|
|||
|
<span id="cb91-6"><a href="#cb91-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 3 <int [2]></span></span>
|
|||
|
<span id="cb91-7"><a href="#cb91-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 2 <int [3]></span></span>
|
|||
|
<span id="cb91-8"><a href="#cb91-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 1 <chr [1]></span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
<p><code>NA</code> indexes are not supported.</p>
|
|||
|
<p>Just like column updates, <code>[<-</code> supports changing the
|
|||
|
type of an existing column.</p>
|
|||
|
<p>Appending columns at the end (without gaps) is supported. The name of
|
|||
|
new columns is determined by the LHS, the RHS, or by name repair (in
|
|||
|
that order of precedence).</p>
|
|||
|
<table class="dftbl">
|
|||
|
<tbody>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb92"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb92-1"><a href="#cb92-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="fu">c</span>(<span class="st">"x"</span>, <span class="st">"y"</span>)] <span class="ot"><-</span> <span class="fu">tibble</span>(<span class="st">"x"</span>, <span class="at">x =</span> <span class="dv">4</span><span class="sc">:</span><span class="dv">1</span>))</span>
|
|||
|
<span id="cb92-2"><a href="#cb92-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 4 × 5</span></span>
|
|||
|
<span id="cb92-3"><a href="#cb92-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li x y</span></span>
|
|||
|
<span id="cb92-4"><a href="#cb92-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int> <chr> <list> <chr> <int></span></span>
|
|||
|
<span id="cb92-5"><a href="#cb92-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e <dbl [1]> x 4</span></span>
|
|||
|
<span id="cb92-6"><a href="#cb92-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 NA f <int [2]> x 3</span></span>
|
|||
|
<span id="cb92-7"><a href="#cb92-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 3 g <int [3]> x 2</span></span>
|
|||
|
<span id="cb92-8"><a href="#cb92-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 NA h <chr [1]> x 1</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb93"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb93-1"><a href="#cb93-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="dv">3</span><span class="sc">:</span><span class="dv">4</span>] <span class="ot"><-</span> <span class="fu">list</span>(<span class="st">"x"</span>, <span class="at">x =</span> <span class="dv">4</span><span class="sc">:</span><span class="dv">1</span>))</span>
|
|||
|
<span id="cb93-2"><a href="#cb93-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 4 × 4</span></span>
|
|||
|
<span id="cb93-3"><a href="#cb93-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li x</span></span>
|
|||
|
<span id="cb93-4"><a href="#cb93-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int> <chr> <chr> <int></span></span>
|
|||
|
<span id="cb93-5"><a href="#cb93-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e x 4</span></span>
|
|||
|
<span id="cb93-6"><a href="#cb93-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 NA f x 3</span></span>
|
|||
|
<span id="cb93-7"><a href="#cb93-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 3 g x 2</span></span>
|
|||
|
<span id="cb93-8"><a href="#cb93-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 NA h x 1</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb94"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb94-1"><a href="#cb94-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_df</span>(df[<span class="dv">4</span>] <span class="ot"><-</span> <span class="fu">list</span>(<span class="dv">4</span><span class="sc">:</span><span class="dv">1</span>))</span>
|
|||
|
<span id="cb94-2"><a href="#cb94-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li V4</span></span>
|
|||
|
<span id="cb94-3"><a href="#cb94-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e 9 4</span></span>
|
|||
|
<span id="cb94-4"><a href="#cb94-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 NA f 10, 11 3</span></span>
|
|||
|
<span id="cb94-5"><a href="#cb94-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 3 g 12, 13, 14 2</span></span>
|
|||
|
<span id="cb94-6"><a href="#cb94-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 NA h text 1</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb95"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb95-1"><a href="#cb95-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="dv">4</span>] <span class="ot"><-</span> <span class="fu">list</span>(<span class="dv">4</span><span class="sc">:</span><span class="dv">1</span>))</span>
|
|||
|
<span id="cb95-2"><a href="#cb95-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 4 × 4</span></span>
|
|||
|
<span id="cb95-3"><a href="#cb95-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li ...4</span></span>
|
|||
|
<span id="cb95-4"><a href="#cb95-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int> <chr> <list> <int></span></span>
|
|||
|
<span id="cb95-5"><a href="#cb95-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e <dbl [1]> 4</span></span>
|
|||
|
<span id="cb95-6"><a href="#cb95-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 NA f <int [2]> 3</span></span>
|
|||
|
<span id="cb95-7"><a href="#cb95-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 3 g <int [3]> 2</span></span>
|
|||
|
<span id="cb95-8"><a href="#cb95-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 NA h <chr [1]> 1</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb96"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb96-1"><a href="#cb96-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_df</span>(df[<span class="dv">5</span>] <span class="ot"><-</span> <span class="fu">list</span>(<span class="dv">4</span><span class="sc">:</span><span class="dv">1</span>))</span>
|
|||
|
<span id="cb96-2"><a href="#cb96-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in `[<-.data.frame`(`*tmp*`, 5, value = list(4:1)): new columns would leave holes after existing columns</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb97"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb97-1"><a href="#cb97-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="dv">5</span>] <span class="ot"><-</span> <span class="fu">list</span>(<span class="dv">4</span><span class="sc">:</span><span class="dv">1</span>))</span>
|
|||
|
<span id="cb97-2"><a href="#cb97-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in `[<-`:</span></span>
|
|||
|
<span id="cb97-3"><a href="#cb97-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> ! Can't assign to columns beyond the end with non-consecutive locations.</span></span>
|
|||
|
<span id="cb97-4"><a href="#cb97-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> ℹ Input has size 3.</span></span>
|
|||
|
<span id="cb97-5"><a href="#cb97-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> ✖ Subscript `5` contains non-consecutive location 5.</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
<p>Tibbles support indexing by a logical matrix, but only for a scalar
|
|||
|
RHS, and if all columns updated are compatible with the value
|
|||
|
assigned.</p>
|
|||
|
<table class="dftbl">
|
|||
|
<tbody>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb98"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb98-1"><a href="#cb98-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_df</span>(df[<span class="fu">is.na</span>(df)] <span class="ot"><-</span> <span class="dv">4</span>)</span>
|
|||
|
<span id="cb98-2"><a href="#cb98-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li</span></span>
|
|||
|
<span id="cb98-3"><a href="#cb98-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e 9</span></span>
|
|||
|
<span id="cb98-4"><a href="#cb98-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 4 f 10, 11</span></span>
|
|||
|
<span id="cb98-5"><a href="#cb98-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 3 g 12, 13, 14</span></span>
|
|||
|
<span id="cb98-6"><a href="#cb98-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 4 h text</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb99"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb99-1"><a href="#cb99-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="fu">is.na</span>(tbl)] <span class="ot"><-</span> <span class="dv">4</span>)</span>
|
|||
|
<span id="cb99-2"><a href="#cb99-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 4 × 3</span></span>
|
|||
|
<span id="cb99-3"><a href="#cb99-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li </span></span>
|
|||
|
<span id="cb99-4"><a href="#cb99-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int> <chr> <list> </span></span>
|
|||
|
<span id="cb99-5"><a href="#cb99-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e <dbl [1]></span></span>
|
|||
|
<span id="cb99-6"><a href="#cb99-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 4 f <int [2]></span></span>
|
|||
|
<span id="cb99-7"><a href="#cb99-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 3 g <int [3]></span></span>
|
|||
|
<span id="cb99-8"><a href="#cb99-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 4 h <chr [1]></span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb100"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb100-1"><a href="#cb100-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_df</span>(df[<span class="fu">is.na</span>(df)] <span class="ot"><-</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>)</span>
|
|||
|
<span id="cb100-2"><a href="#cb100-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li</span></span>
|
|||
|
<span id="cb100-3"><a href="#cb100-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e 9</span></span>
|
|||
|
<span id="cb100-4"><a href="#cb100-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 1 f 10, 11</span></span>
|
|||
|
<span id="cb100-5"><a href="#cb100-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 3 g 12, 13, 14</span></span>
|
|||
|
<span id="cb100-6"><a href="#cb100-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 2 h text</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb101"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb101-1"><a href="#cb101-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="fu">is.na</span>(tbl)] <span class="ot"><-</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>)</span>
|
|||
|
<span id="cb101-2"><a href="#cb101-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in `[<-`:</span></span>
|
|||
|
<span id="cb101-3"><a href="#cb101-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> ! Subscript `is.na(tbl)` is</span></span>
|
|||
|
<span id="cb101-4"><a href="#cb101-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> a matrix, the data `1:2` must</span></span>
|
|||
|
<span id="cb101-5"><a href="#cb101-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> have size 1.</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb102"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb102-1"><a href="#cb102-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_df</span>(df[<span class="fu">matrix</span>(<span class="fu">c</span>(<span class="fu">rep</span>(<span class="cn">TRUE</span>, <span class="dv">5</span>), <span class="fu">rep</span>(<span class="cn">FALSE</span>, <span class="dv">7</span>)), <span class="at">ncol =</span> <span class="dv">3</span>)] <span class="ot"><-</span> <span class="dv">4</span>)</span>
|
|||
|
<span id="cb102-2"><a href="#cb102-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li</span></span>
|
|||
|
<span id="cb102-3"><a href="#cb102-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 4 4 9</span></span>
|
|||
|
<span id="cb102-4"><a href="#cb102-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 4 f 10, 11</span></span>
|
|||
|
<span id="cb102-5"><a href="#cb102-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 4 g 12, 13, 14</span></span>
|
|||
|
<span id="cb102-6"><a href="#cb102-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 4 h text</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb103"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb103-1"><a href="#cb103-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="fu">matrix</span>(<span class="fu">c</span>(<span class="fu">rep</span>(<span class="cn">TRUE</span>, <span class="dv">5</span>), <span class="fu">rep</span>(<span class="cn">FALSE</span>, <span class="dv">7</span>)), <span class="at">ncol =</span> <span class="dv">3</span>)] <span class="ot"><-</span> <span class="dv">4</span>)</span>
|
|||
|
<span id="cb103-2"><a href="#cb103-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in `[<-`:</span></span>
|
|||
|
<span id="cb103-3"><a href="#cb103-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> ! Assigned data `4` must be</span></span>
|
|||
|
<span id="cb103-4"><a href="#cb103-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> compatible with existing data.</span></span>
|
|||
|
<span id="cb103-5"><a href="#cb103-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> ℹ Error occurred for column `c`.</span></span>
|
|||
|
<span id="cb103-6"><a href="#cb103-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> Caused by error in `vec_assign()` at ]8;line = 193:col = 2;file:///private/var/folders/dj/yhk9rkx97wn_ykqtnmk18xvc0000gn/T/RtmpECx3vm/Rbuild51ca1b126fa/tibble/R/subassign-backend.Rtibble/R/subassign-backend.R:193:2]8;;:</span></span>
|
|||
|
<span id="cb103-7"><a href="#cb103-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> ! Can't convert <double> to <character>.</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
</div>
|
|||
|
<div id="a-is-a-matrix-or-array" class="section level3">
|
|||
|
<h3><code>a</code> is a matrix or array</h3>
|
|||
|
<p>If <code>is.matrix(a)</code>, then <code>a</code> is coerced to a
|
|||
|
data frame with <code>as.data.frame()</code> before assigning. If rows
|
|||
|
are assigned, the matrix type must be compatible with all columns. If
|
|||
|
<code>is.array(a)</code> and <code>any(dim(a)[-1:-2] != 1)</code>, an
|
|||
|
error is thrown.</p>
|
|||
|
<table class="dftbl">
|
|||
|
<tbody>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb104"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb104-1"><a href="#cb104-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>] <span class="ot"><-</span> <span class="fu">matrix</span>(<span class="dv">8</span><span class="sc">:</span><span class="dv">1</span>, <span class="at">ncol =</span> <span class="dv">2</span>))</span>
|
|||
|
<span id="cb104-2"><a href="#cb104-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 4 × 3</span></span>
|
|||
|
<span id="cb104-3"><a href="#cb104-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li </span></span>
|
|||
|
<span id="cb104-4"><a href="#cb104-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int> <int> <list> </span></span>
|
|||
|
<span id="cb104-5"><a href="#cb104-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 8 4 <dbl [1]></span></span>
|
|||
|
<span id="cb104-6"><a href="#cb104-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 7 3 <int [2]></span></span>
|
|||
|
<span id="cb104-7"><a href="#cb104-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 6 2 <int [3]></span></span>
|
|||
|
<span id="cb104-8"><a href="#cb104-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 5 1 <chr [1]></span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb105"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb105-1"><a href="#cb105-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_df</span>(df[<span class="dv">1</span><span class="sc">:</span><span class="dv">3</span>, <span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>] <span class="ot"><-</span> <span class="fu">matrix</span>(<span class="dv">6</span><span class="sc">:</span><span class="dv">1</span>, <span class="at">ncol =</span> <span class="dv">2</span>))</span>
|
|||
|
<span id="cb105-2"><a href="#cb105-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li</span></span>
|
|||
|
<span id="cb105-3"><a href="#cb105-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 6 3 9</span></span>
|
|||
|
<span id="cb105-4"><a href="#cb105-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 5 2 10, 11</span></span>
|
|||
|
<span id="cb105-5"><a href="#cb105-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 4 1 12, 13, 14</span></span>
|
|||
|
<span id="cb105-6"><a href="#cb105-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 NA h text</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb106"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb106-1"><a href="#cb106-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="dv">1</span><span class="sc">:</span><span class="dv">3</span>, <span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>] <span class="ot"><-</span> <span class="fu">matrix</span>(<span class="dv">6</span><span class="sc">:</span><span class="dv">1</span>, <span class="at">ncol =</span> <span class="dv">2</span>))</span>
|
|||
|
<span id="cb106-2"><a href="#cb106-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in `[<-`:</span></span>
|
|||
|
<span id="cb106-3"><a href="#cb106-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> ! Assigned data `matrix(6:1,</span></span>
|
|||
|
<span id="cb106-4"><a href="#cb106-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> ncol = 2)` must be compatible</span></span>
|
|||
|
<span id="cb106-5"><a href="#cb106-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> with existing data.</span></span>
|
|||
|
<span id="cb106-6"><a href="#cb106-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> ℹ Error occurred for column `c`.</span></span>
|
|||
|
<span id="cb106-7"><a href="#cb106-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> Caused by error in `vec_assign()` at ]8;line = 193:col = 2;file:///private/var/folders/dj/yhk9rkx97wn_ykqtnmk18xvc0000gn/T/RtmpECx3vm/Rbuild51ca1b126fa/tibble/R/subassign-backend.Rtibble/R/subassign-backend.R:193:2]8;;:</span></span>
|
|||
|
<span id="cb106-8"><a href="#cb106-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> ! Can't convert <integer> to <character>.</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb107"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb107-1"><a href="#cb107-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>] <span class="ot"><-</span> <span class="fu">array</span>(<span class="dv">4</span><span class="sc">:</span><span class="dv">1</span>, <span class="at">dim =</span> <span class="fu">c</span>(<span class="dv">4</span>, <span class="dv">1</span>, <span class="dv">1</span>)))</span>
|
|||
|
<span id="cb107-2"><a href="#cb107-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 4 × 3</span></span>
|
|||
|
<span id="cb107-3"><a href="#cb107-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li </span></span>
|
|||
|
<span id="cb107-4"><a href="#cb107-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int> <int> <list> </span></span>
|
|||
|
<span id="cb107-5"><a href="#cb107-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 4 4 <dbl [1]></span></span>
|
|||
|
<span id="cb107-6"><a href="#cb107-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 3 3 <int [2]></span></span>
|
|||
|
<span id="cb107-7"><a href="#cb107-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 2 2 <int [3]></span></span>
|
|||
|
<span id="cb107-8"><a href="#cb107-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 1 1 <chr [1]></span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb108"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb108-1"><a href="#cb108-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>] <span class="ot"><-</span> <span class="fu">array</span>(<span class="dv">8</span><span class="sc">:</span><span class="dv">1</span>, <span class="at">dim =</span> <span class="fu">c</span>(<span class="dv">4</span>, <span class="dv">2</span>, <span class="dv">1</span>)))</span>
|
|||
|
<span id="cb108-2"><a href="#cb108-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 4 × 3</span></span>
|
|||
|
<span id="cb108-3"><a href="#cb108-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li </span></span>
|
|||
|
<span id="cb108-4"><a href="#cb108-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int> <int> <list> </span></span>
|
|||
|
<span id="cb108-5"><a href="#cb108-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 8 4 <dbl [1]></span></span>
|
|||
|
<span id="cb108-6"><a href="#cb108-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 7 3 <int [2]></span></span>
|
|||
|
<span id="cb108-7"><a href="#cb108-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 6 2 <int [3]></span></span>
|
|||
|
<span id="cb108-8"><a href="#cb108-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 5 1 <chr [1]></span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb109"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb109-1"><a href="#cb109-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_df</span>(df[<span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>] <span class="ot"><-</span> <span class="fu">array</span>(<span class="dv">8</span><span class="sc">:</span><span class="dv">1</span>, <span class="at">dim =</span> <span class="fu">c</span>(<span class="dv">2</span>, <span class="dv">1</span>, <span class="dv">4</span>)))</span>
|
|||
|
<span id="cb109-2"><a href="#cb109-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li</span></span>
|
|||
|
<span id="cb109-3"><a href="#cb109-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 8 4 9</span></span>
|
|||
|
<span id="cb109-4"><a href="#cb109-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 7 3 10, 11</span></span>
|
|||
|
<span id="cb109-5"><a href="#cb109-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 6 2 12, 13, 14</span></span>
|
|||
|
<span id="cb109-6"><a href="#cb109-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 5 1 text</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb110"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb110-1"><a href="#cb110-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>] <span class="ot"><-</span> <span class="fu">array</span>(<span class="dv">8</span><span class="sc">:</span><span class="dv">1</span>, <span class="at">dim =</span> <span class="fu">c</span>(<span class="dv">2</span>, <span class="dv">1</span>, <span class="dv">4</span>)))</span>
|
|||
|
<span id="cb110-2"><a href="#cb110-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in `[<-`:</span></span>
|
|||
|
<span id="cb110-3"><a href="#cb110-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> ! `array(8:1, dim = c(2, 1,</span></span>
|
|||
|
<span id="cb110-4"><a href="#cb110-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4))` must be a vector, a bare</span></span>
|
|||
|
<span id="cb110-5"><a href="#cb110-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> list, a data frame, a matrix, or</span></span>
|
|||
|
<span id="cb110-6"><a href="#cb110-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> NULL.</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb111"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb111-1"><a href="#cb111-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_df</span>(df[<span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>] <span class="ot"><-</span> <span class="fu">array</span>(<span class="dv">8</span><span class="sc">:</span><span class="dv">1</span>, <span class="at">dim =</span> <span class="fu">c</span>(<span class="dv">4</span>, <span class="dv">1</span>, <span class="dv">2</span>)))</span>
|
|||
|
<span id="cb111-2"><a href="#cb111-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li</span></span>
|
|||
|
<span id="cb111-3"><a href="#cb111-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 8 4 9</span></span>
|
|||
|
<span id="cb111-4"><a href="#cb111-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 7 3 10, 11</span></span>
|
|||
|
<span id="cb111-5"><a href="#cb111-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 6 2 12, 13, 14</span></span>
|
|||
|
<span id="cb111-6"><a href="#cb111-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 5 1 text</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb112"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb112-1"><a href="#cb112-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>] <span class="ot"><-</span> <span class="fu">array</span>(<span class="dv">8</span><span class="sc">:</span><span class="dv">1</span>, <span class="at">dim =</span> <span class="fu">c</span>(<span class="dv">4</span>, <span class="dv">1</span>, <span class="dv">2</span>)))</span>
|
|||
|
<span id="cb112-2"><a href="#cb112-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in `[<-`:</span></span>
|
|||
|
<span id="cb112-3"><a href="#cb112-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> ! `array(8:1, dim = c(4, 1,</span></span>
|
|||
|
<span id="cb112-4"><a href="#cb112-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2))` must be a vector, a bare</span></span>
|
|||
|
<span id="cb112-5"><a href="#cb112-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> list, a data frame, a matrix, or</span></span>
|
|||
|
<span id="cb112-6"><a href="#cb112-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> NULL.</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
</div>
|
|||
|
<div id="a-is-another-type-of-vector" class="section level3">
|
|||
|
<h3><code>a</code> is another type of vector</h3>
|
|||
|
<p>If <code>vec_is(a)</code>, then <code>x[j] <- a</code> is
|
|||
|
equivalent to <code>x[j] <- list(a)</code>. This is primarily
|
|||
|
provided for backward compatibility.</p>
|
|||
|
<table class="dftbl">
|
|||
|
<tbody>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb113"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb113-1"><a href="#cb113-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="dv">1</span>] <span class="ot"><-</span> <span class="dv">0</span>)</span>
|
|||
|
<span id="cb113-2"><a href="#cb113-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 4 × 3</span></span>
|
|||
|
<span id="cb113-3"><a href="#cb113-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li </span></span>
|
|||
|
<span id="cb113-4"><a href="#cb113-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <dbl> <chr> <list> </span></span>
|
|||
|
<span id="cb113-5"><a href="#cb113-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 0 e <dbl [1]></span></span>
|
|||
|
<span id="cb113-6"><a href="#cb113-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 0 f <int [2]></span></span>
|
|||
|
<span id="cb113-7"><a href="#cb113-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 0 g <int [3]></span></span>
|
|||
|
<span id="cb113-8"><a href="#cb113-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 0 h <chr [1]></span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb114"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb114-1"><a href="#cb114-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="dv">1</span>] <span class="ot"><-</span> <span class="fu">list</span>(<span class="dv">0</span>))</span>
|
|||
|
<span id="cb114-2"><a href="#cb114-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 4 × 3</span></span>
|
|||
|
<span id="cb114-3"><a href="#cb114-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li </span></span>
|
|||
|
<span id="cb114-4"><a href="#cb114-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <dbl> <chr> <list> </span></span>
|
|||
|
<span id="cb114-5"><a href="#cb114-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 0 e <dbl [1]></span></span>
|
|||
|
<span id="cb114-6"><a href="#cb114-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 0 f <int [2]></span></span>
|
|||
|
<span id="cb114-7"><a href="#cb114-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 0 g <int [3]></span></span>
|
|||
|
<span id="cb114-8"><a href="#cb114-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 0 h <chr [1]></span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
<p>Matrices must be wrapped in <code>list()</code> before assignment to
|
|||
|
create a matrix column.</p>
|
|||
|
<table class="dftbl">
|
|||
|
<tbody>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb115"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb115-1"><a href="#cb115-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="dv">1</span>] <span class="ot"><-</span> <span class="fu">list</span>(<span class="fu">matrix</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">8</span>, <span class="at">ncol =</span> <span class="dv">2</span>)))</span>
|
|||
|
<span id="cb115-2"><a href="#cb115-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 4 × 3</span></span>
|
|||
|
<span id="cb115-3"><a href="#cb115-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n[,1] [,2] c li </span></span>
|
|||
|
<span id="cb115-4"><a href="#cb115-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int> <int> <chr> <list> </span></span>
|
|||
|
<span id="cb115-5"><a href="#cb115-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 5 e <dbl [1]></span></span>
|
|||
|
<span id="cb115-6"><a href="#cb115-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 2 6 f <int [2]></span></span>
|
|||
|
<span id="cb115-7"><a href="#cb115-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 3 7 g <int [3]></span></span>
|
|||
|
<span id="cb115-8"><a href="#cb115-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 4 8 h <chr [1]></span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb116"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb116-1"><a href="#cb116-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>] <span class="ot"><-</span> <span class="fu">list</span>(<span class="fu">matrix</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">8</span>, <span class="at">ncol =</span> <span class="dv">2</span>)))</span>
|
|||
|
<span id="cb116-2"><a href="#cb116-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 4 × 3</span></span>
|
|||
|
<span id="cb116-3"><a href="#cb116-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n[,1] [,2] c[,1] [,2] li </span></span>
|
|||
|
<span id="cb116-4"><a href="#cb116-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int> <int> <int> <int> <list> </span></span>
|
|||
|
<span id="cb116-5"><a href="#cb116-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 5 1 5 <dbl [1]></span></span>
|
|||
|
<span id="cb116-6"><a href="#cb116-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 2 6 2 6 <int [2]></span></span>
|
|||
|
<span id="cb116-7"><a href="#cb116-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 3 7 3 7 <int [3]></span></span>
|
|||
|
<span id="cb116-8"><a href="#cb116-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 4 8 4 8 <chr [1]></span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
</div>
|
|||
|
<div id="a-is-null" class="section level3">
|
|||
|
<h3><code>a</code> is <code>NULL</code></h3>
|
|||
|
<p>Entire columns can be removed. Specifying <code>i</code> is an
|
|||
|
error.</p>
|
|||
|
<table class="dftbl">
|
|||
|
<tbody>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb117"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb117-1"><a href="#cb117-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="dv">1</span>] <span class="ot"><-</span> <span class="cn">NULL</span>)</span>
|
|||
|
<span id="cb117-2"><a href="#cb117-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 4 × 2</span></span>
|
|||
|
<span id="cb117-3"><a href="#cb117-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> c li </span></span>
|
|||
|
<span id="cb117-4"><a href="#cb117-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <chr> <list> </span></span>
|
|||
|
<span id="cb117-5"><a href="#cb117-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 e <dbl [1]></span></span>
|
|||
|
<span id="cb117-6"><a href="#cb117-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 f <int [2]></span></span>
|
|||
|
<span id="cb117-7"><a href="#cb117-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 g <int [3]></span></span>
|
|||
|
<span id="cb117-8"><a href="#cb117-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 h <chr [1]></span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb118"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb118-1"><a href="#cb118-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[, <span class="dv">2</span><span class="sc">:</span><span class="dv">3</span>] <span class="ot"><-</span> <span class="cn">NULL</span>)</span>
|
|||
|
<span id="cb118-2"><a href="#cb118-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 4 × 1</span></span>
|
|||
|
<span id="cb118-3"><a href="#cb118-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n</span></span>
|
|||
|
<span id="cb118-4"><a href="#cb118-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int></span></span>
|
|||
|
<span id="cb118-5"><a href="#cb118-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1</span></span>
|
|||
|
<span id="cb118-6"><a href="#cb118-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 NA</span></span>
|
|||
|
<span id="cb118-7"><a href="#cb118-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 3</span></span>
|
|||
|
<span id="cb118-8"><a href="#cb118-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 NA</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb119"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb119-1"><a href="#cb119-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_df</span>(df[<span class="dv">1</span>, <span class="dv">2</span><span class="sc">:</span><span class="dv">3</span>] <span class="ot"><-</span> <span class="cn">NULL</span>)</span>
|
|||
|
<span id="cb119-2"><a href="#cb119-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in x[[jj]][iseq] <- vjj: replacement has length zero</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb120"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb120-1"><a href="#cb120-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="dv">1</span>, <span class="dv">2</span><span class="sc">:</span><span class="dv">3</span>] <span class="ot"><-</span> <span class="cn">NULL</span>)</span>
|
|||
|
<span id="cb120-2"><a href="#cb120-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in `[<-`:</span></span>
|
|||
|
<span id="cb120-3"><a href="#cb120-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> ! `NULL` must be a vector, a</span></span>
|
|||
|
<span id="cb120-4"><a href="#cb120-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> bare list, a data frame or a</span></span>
|
|||
|
<span id="cb120-5"><a href="#cb120-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> matrix.</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
</div>
|
|||
|
<div id="a-is-not-a-vector" class="section level3">
|
|||
|
<h3><code>a</code> is not a vector</h3>
|
|||
|
<p>Any other type for <code>a</code> is an error. Note that if
|
|||
|
<code>is.list(a)</code> is <code>TRUE</code>, but
|
|||
|
<code>inherits(a, "list")</code> is <code>FALSE</code>, then
|
|||
|
<code>a</code> is considered to be a scalar. See <code>?vec_is</code>
|
|||
|
and <code>?vec_proxy</code> for details.</p>
|
|||
|
<table class="dftbl">
|
|||
|
<tbody>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb121"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb121-1"><a href="#cb121-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_df</span>(df[<span class="dv">1</span>] <span class="ot"><-</span> mean)</span>
|
|||
|
<span id="cb121-2"><a href="#cb121-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in rep(value, length.out = n): attempt to replicate an object of type 'closure'</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb122"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb122-1"><a href="#cb122-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="dv">1</span>] <span class="ot"><-</span> mean)</span>
|
|||
|
<span id="cb122-2"><a href="#cb122-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in `[<-`:</span></span>
|
|||
|
<span id="cb122-3"><a href="#cb122-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> ! `mean` must be a vector, a</span></span>
|
|||
|
<span id="cb122-4"><a href="#cb122-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> bare list, a data frame, a</span></span>
|
|||
|
<span id="cb122-5"><a href="#cb122-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> matrix, or NULL.</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb123"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb123-1"><a href="#cb123-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_df</span>(df[<span class="dv">1</span>] <span class="ot"><-</span> <span class="fu">lm</span>(mpg <span class="sc">~</span> wt, <span class="at">data =</span> mtcars))</span>
|
|||
|
<span id="cb123-2"><a href="#cb123-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Warning in</span></span>
|
|||
|
<span id="cb123-3"><a href="#cb123-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> `[<-.data.frame`(`*tmp*`, 1, value</span></span>
|
|||
|
<span id="cb123-4"><a href="#cb123-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> = structure(list(coefficients =</span></span>
|
|||
|
<span id="cb123-5"><a href="#cb123-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> c(`(Intercept)` = 37.285126167342,</span></span>
|
|||
|
<span id="cb123-6"><a href="#cb123-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> : replacement element 2 has 32 rows</span></span>
|
|||
|
<span id="cb123-7"><a href="#cb123-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> to replace 4 rows</span></span>
|
|||
|
<span id="cb123-8"><a href="#cb123-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> Warning in</span></span>
|
|||
|
<span id="cb123-9"><a href="#cb123-9" aria-hidden="true" tabindex="-1"></a><span class="co">#> `[<-.data.frame`(`*tmp*`, 1, value</span></span>
|
|||
|
<span id="cb123-10"><a href="#cb123-10" aria-hidden="true" tabindex="-1"></a><span class="co">#> = structure(list(coefficients =</span></span>
|
|||
|
<span id="cb123-11"><a href="#cb123-11" aria-hidden="true" tabindex="-1"></a><span class="co">#> c(`(Intercept)` = 37.285126167342,</span></span>
|
|||
|
<span id="cb123-12"><a href="#cb123-12" aria-hidden="true" tabindex="-1"></a><span class="co">#> : replacement element 3 has 32 rows</span></span>
|
|||
|
<span id="cb123-13"><a href="#cb123-13" aria-hidden="true" tabindex="-1"></a><span class="co">#> to replace 4 rows</span></span>
|
|||
|
<span id="cb123-14"><a href="#cb123-14" aria-hidden="true" tabindex="-1"></a><span class="co">#> Warning in</span></span>
|
|||
|
<span id="cb123-15"><a href="#cb123-15" aria-hidden="true" tabindex="-1"></a><span class="co">#> `[<-.data.frame`(`*tmp*`, 1, value</span></span>
|
|||
|
<span id="cb123-16"><a href="#cb123-16" aria-hidden="true" tabindex="-1"></a><span class="co">#> = structure(list(coefficients =</span></span>
|
|||
|
<span id="cb123-17"><a href="#cb123-17" aria-hidden="true" tabindex="-1"></a><span class="co">#> c(`(Intercept)` = 37.285126167342,</span></span>
|
|||
|
<span id="cb123-18"><a href="#cb123-18" aria-hidden="true" tabindex="-1"></a><span class="co">#> : replacement element 5 has 32 rows</span></span>
|
|||
|
<span id="cb123-19"><a href="#cb123-19" aria-hidden="true" tabindex="-1"></a><span class="co">#> to replace 4 rows</span></span>
|
|||
|
<span id="cb123-20"><a href="#cb123-20" aria-hidden="true" tabindex="-1"></a><span class="co">#> Warning in</span></span>
|
|||
|
<span id="cb123-21"><a href="#cb123-21" aria-hidden="true" tabindex="-1"></a><span class="co">#> `[<-.data.frame`(`*tmp*`, 1, value</span></span>
|
|||
|
<span id="cb123-22"><a href="#cb123-22" aria-hidden="true" tabindex="-1"></a><span class="co">#> = structure(list(coefficients =</span></span>
|
|||
|
<span id="cb123-23"><a href="#cb123-23" aria-hidden="true" tabindex="-1"></a><span class="co">#> c(`(Intercept)` = 37.285126167342,</span></span>
|
|||
|
<span id="cb123-24"><a href="#cb123-24" aria-hidden="true" tabindex="-1"></a><span class="co">#> : replacement element 7 has 5 rows</span></span>
|
|||
|
<span id="cb123-25"><a href="#cb123-25" aria-hidden="true" tabindex="-1"></a><span class="co">#> to replace 4 rows</span></span>
|
|||
|
<span id="cb123-26"><a href="#cb123-26" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in `[<-.data.frame`(`*tmp*`, 1, value = structure(list(coefficients = c(`(Intercept)` = 37.285126167342, : replacement element 10 has 3 rows, need 4</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb124"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb124-1"><a href="#cb124-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="dv">1</span>] <span class="ot"><-</span> <span class="fu">lm</span>(mpg <span class="sc">~</span> wt, <span class="at">data =</span> mtcars))</span>
|
|||
|
<span id="cb124-2"><a href="#cb124-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in `[<-`:</span></span>
|
|||
|
<span id="cb124-3"><a href="#cb124-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> ! `lm(mpg ~ wt, data =</span></span>
|
|||
|
<span id="cb124-4"><a href="#cb124-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> mtcars)` must be a vector, a bare</span></span>
|
|||
|
<span id="cb124-5"><a href="#cb124-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> list, a data frame, a matrix, or</span></span>
|
|||
|
<span id="cb124-6"><a href="#cb124-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> NULL.</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
<!-- HW: we need better error messages for these cases -->
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<div id="row-subassignment-xi---list..." class="section level2">
|
|||
|
<h2>Row subassignment: <code>x[i, ] <- list(...)</code></h2>
|
|||
|
<p><code>x[i, ] <- a</code> is the same as
|
|||
|
<code>vec_slice(x[[j_1]], i) <- a[[1]]</code>,
|
|||
|
<code>vec_slice(x[[j_2]], i) <- a[[2]]</code>, … .<a href="#fn7" class="footnote-ref" id="fnref7"><sup>7</sup></a></p>
|
|||
|
<table class="dftbl">
|
|||
|
<tbody>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb125"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb125-1"><a href="#cb125-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="dv">2</span><span class="sc">:</span><span class="dv">3</span>, ] <span class="ot"><-</span> tbl[<span class="dv">1</span>, ])</span>
|
|||
|
<span id="cb125-2"><a href="#cb125-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 4 × 3</span></span>
|
|||
|
<span id="cb125-3"><a href="#cb125-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li </span></span>
|
|||
|
<span id="cb125-4"><a href="#cb125-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int> <chr> <list> </span></span>
|
|||
|
<span id="cb125-5"><a href="#cb125-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e <dbl [1]></span></span>
|
|||
|
<span id="cb125-6"><a href="#cb125-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 1 e <dbl [1]></span></span>
|
|||
|
<span id="cb125-7"><a href="#cb125-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 1 e <dbl [1]></span></span>
|
|||
|
<span id="cb125-8"><a href="#cb125-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 NA h <chr [1]></span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb126"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb126-1"><a href="#cb126-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="fu">c</span>(<span class="cn">FALSE</span>, <span class="cn">TRUE</span>, <span class="cn">TRUE</span>, <span class="cn">FALSE</span>), ] <span class="ot"><-</span> tbl[<span class="dv">1</span>, ])</span>
|
|||
|
<span id="cb126-2"><a href="#cb126-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 4 × 3</span></span>
|
|||
|
<span id="cb126-3"><a href="#cb126-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li </span></span>
|
|||
|
<span id="cb126-4"><a href="#cb126-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int> <chr> <list> </span></span>
|
|||
|
<span id="cb126-5"><a href="#cb126-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e <dbl [1]></span></span>
|
|||
|
<span id="cb126-6"><a href="#cb126-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 1 e <dbl [1]></span></span>
|
|||
|
<span id="cb126-7"><a href="#cb126-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 1 e <dbl [1]></span></span>
|
|||
|
<span id="cb126-8"><a href="#cb126-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 NA h <chr [1]></span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
<p>Only values of size one can be recycled.</p>
|
|||
|
<table class="dftbl">
|
|||
|
<tbody>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb127"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb127-1"><a href="#cb127-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="dv">2</span><span class="sc">:</span><span class="dv">3</span>, ] <span class="ot"><-</span> tbl[<span class="dv">1</span>, ])</span>
|
|||
|
<span id="cb127-2"><a href="#cb127-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 4 × 3</span></span>
|
|||
|
<span id="cb127-3"><a href="#cb127-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li </span></span>
|
|||
|
<span id="cb127-4"><a href="#cb127-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int> <chr> <list> </span></span>
|
|||
|
<span id="cb127-5"><a href="#cb127-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e <dbl [1]></span></span>
|
|||
|
<span id="cb127-6"><a href="#cb127-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 1 e <dbl [1]></span></span>
|
|||
|
<span id="cb127-7"><a href="#cb127-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 1 e <dbl [1]></span></span>
|
|||
|
<span id="cb127-8"><a href="#cb127-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 NA h <chr [1]></span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb128"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb128-1"><a href="#cb128-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="dv">2</span><span class="sc">:</span><span class="dv">3</span>, ] <span class="ot"><-</span> <span class="fu">list</span>(tbl<span class="sc">$</span>n[<span class="dv">1</span>], tbl<span class="sc">$</span>c[<span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>], tbl<span class="sc">$</span>li[<span class="dv">1</span>]))</span>
|
|||
|
<span id="cb128-2"><a href="#cb128-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 4 × 3</span></span>
|
|||
|
<span id="cb128-3"><a href="#cb128-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li </span></span>
|
|||
|
<span id="cb128-4"><a href="#cb128-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int> <chr> <list> </span></span>
|
|||
|
<span id="cb128-5"><a href="#cb128-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e <dbl [1]></span></span>
|
|||
|
<span id="cb128-6"><a href="#cb128-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 1 e <dbl [1]></span></span>
|
|||
|
<span id="cb128-7"><a href="#cb128-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 1 f <dbl [1]></span></span>
|
|||
|
<span id="cb128-8"><a href="#cb128-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 NA h <chr [1]></span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb129"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb129-1"><a href="#cb129-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_df</span>(df[<span class="dv">2</span><span class="sc">:</span><span class="dv">4</span>, ] <span class="ot"><-</span> df[<span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>, ])</span>
|
|||
|
<span id="cb129-2"><a href="#cb129-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in `[<-.data.frame`(`*tmp*`, 2:4, , value = structure(list(n = c(1L, : replacement element 1 has 2 rows, need 3</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb130"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb130-1"><a href="#cb130-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="dv">2</span><span class="sc">:</span><span class="dv">4</span>, ] <span class="ot"><-</span> tbl[<span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>, ])</span>
|
|||
|
<span id="cb130-2"><a href="#cb130-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in `[<-`:</span></span>
|
|||
|
<span id="cb130-3"><a href="#cb130-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> ! Assigned data `tbl[1:2, ]`</span></span>
|
|||
|
<span id="cb130-4"><a href="#cb130-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> must be compatible with row</span></span>
|
|||
|
<span id="cb130-5"><a href="#cb130-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> subscript `2:4`.</span></span>
|
|||
|
<span id="cb130-6"><a href="#cb130-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> ✖ 3 rows must be assigned.</span></span>
|
|||
|
<span id="cb130-7"><a href="#cb130-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> ✖ Element 1 of assigned data has 2</span></span>
|
|||
|
<span id="cb130-8"><a href="#cb130-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> rows.</span></span>
|
|||
|
<span id="cb130-9"><a href="#cb130-9" aria-hidden="true" tabindex="-1"></a><span class="co">#> ℹ Only vectors of size 1 are</span></span>
|
|||
|
<span id="cb130-10"><a href="#cb130-10" aria-hidden="true" tabindex="-1"></a><span class="co">#> recycled.</span></span>
|
|||
|
<span id="cb130-11"><a href="#cb130-11" aria-hidden="true" tabindex="-1"></a><span class="co">#> Caused by error in `vectbl_recycle_rhs_rows()` at ]8;line = 169:col = 6;file:///private/var/folders/dj/yhk9rkx97wn_ykqtnmk18xvc0000gn/T/RtmpECx3vm/Rbuild51ca1b126fa/tibble/R/subassign-backend.Rtibble/R/subassign-backend.R:169:6]8;;:</span></span>
|
|||
|
<span id="cb130-12"><a href="#cb130-12" aria-hidden="true" tabindex="-1"></a><span class="co">#> ! Can't recycle input of size 2 to size 3.</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
<p>For compatibility, only a warning is issued for indexing beyond the
|
|||
|
number of rows. Appending rows right at the end of the existing data is
|
|||
|
supported, without warning.</p>
|
|||
|
<table class="dftbl">
|
|||
|
<tbody>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb131"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb131-1"><a href="#cb131-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="dv">5</span>, ] <span class="ot"><-</span> tbl[<span class="dv">1</span>, ])</span>
|
|||
|
<span id="cb131-2"><a href="#cb131-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 5 × 3</span></span>
|
|||
|
<span id="cb131-3"><a href="#cb131-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li </span></span>
|
|||
|
<span id="cb131-4"><a href="#cb131-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int> <chr> <list> </span></span>
|
|||
|
<span id="cb131-5"><a href="#cb131-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e <dbl [1]></span></span>
|
|||
|
<span id="cb131-6"><a href="#cb131-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 NA f <int [2]></span></span>
|
|||
|
<span id="cb131-7"><a href="#cb131-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 3 g <int [3]></span></span>
|
|||
|
<span id="cb131-8"><a href="#cb131-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 NA h <chr [1]></span></span>
|
|||
|
<span id="cb131-9"><a href="#cb131-9" aria-hidden="true" tabindex="-1"></a><span class="co">#> 5 1 e <dbl [1]></span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb132"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb132-1"><a href="#cb132-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="dv">5</span><span class="sc">:</span><span class="dv">7</span>, ] <span class="ot"><-</span> tbl[<span class="dv">1</span>, ])</span>
|
|||
|
<span id="cb132-2"><a href="#cb132-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 7 × 3</span></span>
|
|||
|
<span id="cb132-3"><a href="#cb132-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li </span></span>
|
|||
|
<span id="cb132-4"><a href="#cb132-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int> <chr> <list> </span></span>
|
|||
|
<span id="cb132-5"><a href="#cb132-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e <dbl [1]></span></span>
|
|||
|
<span id="cb132-6"><a href="#cb132-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 NA f <int [2]></span></span>
|
|||
|
<span id="cb132-7"><a href="#cb132-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 3 g <int [3]></span></span>
|
|||
|
<span id="cb132-8"><a href="#cb132-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 NA h <chr [1]></span></span>
|
|||
|
<span id="cb132-9"><a href="#cb132-9" aria-hidden="true" tabindex="-1"></a><span class="co">#> 5 1 e <dbl [1]></span></span>
|
|||
|
<span id="cb132-10"><a href="#cb132-10" aria-hidden="true" tabindex="-1"></a><span class="co">#> 6 1 e <dbl [1]></span></span>
|
|||
|
<span id="cb132-11"><a href="#cb132-11" aria-hidden="true" tabindex="-1"></a><span class="co">#> 7 1 e <dbl [1]></span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb133"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb133-1"><a href="#cb133-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_df</span>(df[<span class="dv">6</span>, ] <span class="ot"><-</span> df[<span class="dv">1</span>, ])</span>
|
|||
|
<span id="cb133-2"><a href="#cb133-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li</span></span>
|
|||
|
<span id="cb133-3"><a href="#cb133-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e 9</span></span>
|
|||
|
<span id="cb133-4"><a href="#cb133-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 NA f 10, 11</span></span>
|
|||
|
<span id="cb133-5"><a href="#cb133-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 3 g 12, 13, 14</span></span>
|
|||
|
<span id="cb133-6"><a href="#cb133-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 NA h text</span></span>
|
|||
|
<span id="cb133-7"><a href="#cb133-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 5 NA <NA> NULL</span></span>
|
|||
|
<span id="cb133-8"><a href="#cb133-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 6 1 e 9</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb134"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb134-1"><a href="#cb134-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="dv">6</span>, ] <span class="ot"><-</span> tbl[<span class="dv">1</span>, ])</span>
|
|||
|
<span id="cb134-2"><a href="#cb134-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in `[<-`:</span></span>
|
|||
|
<span id="cb134-3"><a href="#cb134-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> ! Can't assign to rows beyond the end with non-consecutive locations.</span></span>
|
|||
|
<span id="cb134-4"><a href="#cb134-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> ℹ Input has size 4.</span></span>
|
|||
|
<span id="cb134-5"><a href="#cb134-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> ✖ Subscript `6` contains non-consecutive location 6.</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb135"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb135-1"><a href="#cb135-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_df</span>(df[<span class="sc">-</span><span class="dv">5</span>, ] <span class="ot"><-</span> df[<span class="dv">1</span>, ])</span>
|
|||
|
<span id="cb135-2"><a href="#cb135-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li</span></span>
|
|||
|
<span id="cb135-3"><a href="#cb135-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e 9</span></span>
|
|||
|
<span id="cb135-4"><a href="#cb135-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 1 e 9</span></span>
|
|||
|
<span id="cb135-5"><a href="#cb135-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 1 e 9</span></span>
|
|||
|
<span id="cb135-6"><a href="#cb135-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 1 e 9</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb136"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb136-1"><a href="#cb136-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="sc">-</span><span class="dv">5</span>, ] <span class="ot"><-</span> tbl[<span class="dv">1</span>, ])</span>
|
|||
|
<span id="cb136-2"><a href="#cb136-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in `[<-`:</span></span>
|
|||
|
<span id="cb136-3"><a href="#cb136-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> ! Can't negate rows past the end.</span></span>
|
|||
|
<span id="cb136-4"><a href="#cb136-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> ℹ Location 5 doesn't exist.</span></span>
|
|||
|
<span id="cb136-5"><a href="#cb136-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> ℹ There are only 4 rows.</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb137"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb137-1"><a href="#cb137-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_df</span>(df[<span class="sc">-</span>(<span class="dv">5</span><span class="sc">:</span><span class="dv">7</span>), ] <span class="ot"><-</span> df[<span class="dv">1</span>, ])</span>
|
|||
|
<span id="cb137-2"><a href="#cb137-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li</span></span>
|
|||
|
<span id="cb137-3"><a href="#cb137-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e 9</span></span>
|
|||
|
<span id="cb137-4"><a href="#cb137-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 1 e 9</span></span>
|
|||
|
<span id="cb137-5"><a href="#cb137-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 1 e 9</span></span>
|
|||
|
<span id="cb137-6"><a href="#cb137-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 1 e 9</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb138"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb138-1"><a href="#cb138-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="sc">-</span>(<span class="dv">5</span><span class="sc">:</span><span class="dv">7</span>), ] <span class="ot"><-</span> tbl[<span class="dv">1</span>, ])</span>
|
|||
|
<span id="cb138-2"><a href="#cb138-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in `[<-`:</span></span>
|
|||
|
<span id="cb138-3"><a href="#cb138-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> ! Can't negate rows past the end.</span></span>
|
|||
|
<span id="cb138-4"><a href="#cb138-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> ℹ Locations 5, 6, and 7</span></span>
|
|||
|
<span id="cb138-5"><a href="#cb138-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> don't exist.</span></span>
|
|||
|
<span id="cb138-6"><a href="#cb138-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> ℹ There are only 4 rows.</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb139"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb139-1"><a href="#cb139-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_df</span>(df[<span class="sc">-</span><span class="dv">6</span>, ] <span class="ot"><-</span> df[<span class="dv">1</span>, ])</span>
|
|||
|
<span id="cb139-2"><a href="#cb139-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li</span></span>
|
|||
|
<span id="cb139-3"><a href="#cb139-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e 9</span></span>
|
|||
|
<span id="cb139-4"><a href="#cb139-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 1 e 9</span></span>
|
|||
|
<span id="cb139-5"><a href="#cb139-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 1 e 9</span></span>
|
|||
|
<span id="cb139-6"><a href="#cb139-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 1 e 9</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb140"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb140-1"><a href="#cb140-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="sc">-</span><span class="dv">6</span>, ] <span class="ot"><-</span> tbl[<span class="dv">1</span>, ])</span>
|
|||
|
<span id="cb140-2"><a href="#cb140-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in `[<-`:</span></span>
|
|||
|
<span id="cb140-3"><a href="#cb140-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> ! Can't negate rows past the end.</span></span>
|
|||
|
<span id="cb140-4"><a href="#cb140-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> ℹ Location 6 doesn't exist.</span></span>
|
|||
|
<span id="cb140-5"><a href="#cb140-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> ℹ There are only 4 rows.</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
<p>For compatibility, <code>i</code> can also be a character vector
|
|||
|
containing positive numbers.</p>
|
|||
|
<table class="dftbl">
|
|||
|
<tbody>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb141"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb141-1"><a href="#cb141-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="fu">as.character</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">3</span>), ] <span class="ot"><-</span> tbl[<span class="dv">1</span>, ])</span>
|
|||
|
<span id="cb141-2"><a href="#cb141-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 4 × 3</span></span>
|
|||
|
<span id="cb141-3"><a href="#cb141-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li </span></span>
|
|||
|
<span id="cb141-4"><a href="#cb141-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int> <chr> <list> </span></span>
|
|||
|
<span id="cb141-5"><a href="#cb141-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e <dbl [1]></span></span>
|
|||
|
<span id="cb141-6"><a href="#cb141-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 1 e <dbl [1]></span></span>
|
|||
|
<span id="cb141-7"><a href="#cb141-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 1 e <dbl [1]></span></span>
|
|||
|
<span id="cb141-8"><a href="#cb141-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 NA h <chr [1]></span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
</div>
|
|||
|
<div id="row-and-column-subassignment" class="section level2">
|
|||
|
<h2>Row and column subassignment</h2>
|
|||
|
<div id="definition-of-xi-j---a" class="section level3">
|
|||
|
<h3>Definition of <code>x[i, j] <- a</code></h3>
|
|||
|
<p><code>x[i, j] <- a</code> is equivalent to
|
|||
|
<code>x[i, ][j] <- a</code>.<a href="#fn8" class="footnote-ref" id="fnref8"><sup>8</sup></a></p>
|
|||
|
<p>Subassignment to <code>x[i, j]</code> is stricter for tibbles than
|
|||
|
for data frames. <code>x[i, j] <- a</code> can’t change the data type
|
|||
|
of existing columns.</p>
|
|||
|
<table class="dftbl">
|
|||
|
<tbody>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb142"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb142-1"><a href="#cb142-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_df</span>(df[<span class="dv">2</span><span class="sc">:</span><span class="dv">3</span>, <span class="dv">1</span>] <span class="ot"><-</span> df[<span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>, <span class="dv">2</span>])</span>
|
|||
|
<span id="cb142-2"><a href="#cb142-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li</span></span>
|
|||
|
<span id="cb142-3"><a href="#cb142-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e 9</span></span>
|
|||
|
<span id="cb142-4"><a href="#cb142-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 e f 10, 11</span></span>
|
|||
|
<span id="cb142-5"><a href="#cb142-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 f g 12, 13, 14</span></span>
|
|||
|
<span id="cb142-6"><a href="#cb142-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 <NA> h text</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb143"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb143-1"><a href="#cb143-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="dv">2</span><span class="sc">:</span><span class="dv">3</span>, <span class="dv">1</span>] <span class="ot"><-</span> tbl[<span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>, <span class="dv">2</span>])</span>
|
|||
|
<span id="cb143-2"><a href="#cb143-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in `[<-`:</span></span>
|
|||
|
<span id="cb143-3"><a href="#cb143-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> ! Assigned data `tbl[1:2,</span></span>
|
|||
|
<span id="cb143-4"><a href="#cb143-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2]` must be compatible with</span></span>
|
|||
|
<span id="cb143-5"><a href="#cb143-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> existing data.</span></span>
|
|||
|
<span id="cb143-6"><a href="#cb143-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> ℹ Error occurred for column `n`.</span></span>
|
|||
|
<span id="cb143-7"><a href="#cb143-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> Caused by error in `vec_assign()` at ]8;line = 193:col = 2;file:///private/var/folders/dj/yhk9rkx97wn_ykqtnmk18xvc0000gn/T/RtmpECx3vm/Rbuild51ca1b126fa/tibble/R/subassign-backend.Rtibble/R/subassign-backend.R:193:2]8;;:</span></span>
|
|||
|
<span id="cb143-8"><a href="#cb143-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> ! Can't convert <character> to <integer>.</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb144"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb144-1"><a href="#cb144-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_df</span>(df[<span class="dv">2</span><span class="sc">:</span><span class="dv">3</span>, <span class="dv">2</span>] <span class="ot"><-</span> df[<span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>, <span class="dv">3</span>])</span>
|
|||
|
<span id="cb144-2"><a href="#cb144-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Warning in</span></span>
|
|||
|
<span id="cb144-3"><a href="#cb144-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> `[<-.data.frame`(`*tmp*`, 2:3, 2,</span></span>
|
|||
|
<span id="cb144-4"><a href="#cb144-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> value = list(9, 10:11)): provided 2</span></span>
|
|||
|
<span id="cb144-5"><a href="#cb144-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> variables to replace 1 variables</span></span>
|
|||
|
<span id="cb144-6"><a href="#cb144-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li</span></span>
|
|||
|
<span id="cb144-7"><a href="#cb144-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e 9</span></span>
|
|||
|
<span id="cb144-8"><a href="#cb144-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 NA 9 10, 11</span></span>
|
|||
|
<span id="cb144-9"><a href="#cb144-9" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 3 9 12, 13, 14</span></span>
|
|||
|
<span id="cb144-10"><a href="#cb144-10" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 NA h text</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb145"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb145-1"><a href="#cb145-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="dv">2</span><span class="sc">:</span><span class="dv">3</span>, <span class="dv">2</span>] <span class="ot"><-</span> tbl[<span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>, <span class="dv">3</span>])</span>
|
|||
|
<span id="cb145-2"><a href="#cb145-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in `[<-`:</span></span>
|
|||
|
<span id="cb145-3"><a href="#cb145-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> ! Assigned data `tbl[1:2,</span></span>
|
|||
|
<span id="cb145-4"><a href="#cb145-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3]` must be compatible with</span></span>
|
|||
|
<span id="cb145-5"><a href="#cb145-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> existing data.</span></span>
|
|||
|
<span id="cb145-6"><a href="#cb145-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> ℹ Error occurred for column `c`.</span></span>
|
|||
|
<span id="cb145-7"><a href="#cb145-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> Caused by error in `vec_assign()` at ]8;line = 193:col = 2;file:///private/var/folders/dj/yhk9rkx97wn_ykqtnmk18xvc0000gn/T/RtmpECx3vm/Rbuild51ca1b126fa/tibble/R/subassign-backend.Rtibble/R/subassign-backend.R:193:2]8;;:</span></span>
|
|||
|
<span id="cb145-8"><a href="#cb145-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> ! Can't convert <list> to <character>.</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb146"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb146-1"><a href="#cb146-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_df</span>(df[<span class="dv">2</span><span class="sc">:</span><span class="dv">3</span>, <span class="dv">3</span>] <span class="ot"><-</span> df2[<span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>, <span class="dv">1</span>])</span>
|
|||
|
<span id="cb146-2"><a href="#cb146-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Warning in</span></span>
|
|||
|
<span id="cb146-3"><a href="#cb146-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> `[<-.data.frame`(`*tmp*`, 2:3, 3,</span></span>
|
|||
|
<span id="cb146-4"><a href="#cb146-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> value = structure(list(n = c(1L, :</span></span>
|
|||
|
<span id="cb146-5"><a href="#cb146-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> provided 3 variables to replace 1</span></span>
|
|||
|
<span id="cb146-6"><a href="#cb146-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> variables</span></span>
|
|||
|
<span id="cb146-7"><a href="#cb146-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li</span></span>
|
|||
|
<span id="cb146-8"><a href="#cb146-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e 9</span></span>
|
|||
|
<span id="cb146-9"><a href="#cb146-9" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 NA f 1</span></span>
|
|||
|
<span id="cb146-10"><a href="#cb146-10" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 3 g NA</span></span>
|
|||
|
<span id="cb146-11"><a href="#cb146-11" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 NA h text</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb147"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb147-1"><a href="#cb147-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="dv">2</span><span class="sc">:</span><span class="dv">3</span>, <span class="dv">3</span>] <span class="ot"><-</span> tbl2[<span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>, <span class="dv">1</span>])</span>
|
|||
|
<span id="cb147-2"><a href="#cb147-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in `[<-`:</span></span>
|
|||
|
<span id="cb147-3"><a href="#cb147-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> ! Assigned data `tbl2[1:2,</span></span>
|
|||
|
<span id="cb147-4"><a href="#cb147-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1]` must be compatible with</span></span>
|
|||
|
<span id="cb147-5"><a href="#cb147-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> existing data.</span></span>
|
|||
|
<span id="cb147-6"><a href="#cb147-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> ℹ Error occurred for column `li`.</span></span>
|
|||
|
<span id="cb147-7"><a href="#cb147-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> Caused by error in `vec_assign()` at ]8;line = 193:col = 2;file:///private/var/folders/dj/yhk9rkx97wn_ykqtnmk18xvc0000gn/T/RtmpECx3vm/Rbuild51ca1b126fa/tibble/R/subassign-backend.Rtibble/R/subassign-backend.R:193:2]8;;:</span></span>
|
|||
|
<span id="cb147-8"><a href="#cb147-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> ! Can't convert <tbl_df> to <list>.</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb148"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb148-1"><a href="#cb148-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_df2</span>(df2[<span class="dv">2</span><span class="sc">:</span><span class="dv">3</span>, <span class="dv">1</span>] <span class="ot"><-</span> df2[<span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>, <span class="dv">2</span>])</span>
|
|||
|
<span id="cb148-2"><a href="#cb148-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Warning in matrix(value, n, p):</span></span>
|
|||
|
<span id="cb148-3"><a href="#cb148-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> data length [8] is not a</span></span>
|
|||
|
<span id="cb148-4"><a href="#cb148-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> sub-multiple or multiple of the</span></span>
|
|||
|
<span id="cb148-5"><a href="#cb148-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> number of columns [3]</span></span>
|
|||
|
<span id="cb148-6"><a href="#cb148-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> tb.n tb.c tb.li m.1 m.2 m.3 m.4</span></span>
|
|||
|
<span id="cb148-7"><a href="#cb148-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e 9 1 0 0 0</span></span>
|
|||
|
<span id="cb148-8"><a href="#cb148-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 1 0 0 0 1 0 0</span></span>
|
|||
|
<span id="cb148-9"><a href="#cb148-9" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 0 1 0 0 0 1 0</span></span>
|
|||
|
<span id="cb148-10"><a href="#cb148-10" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 NA h text 0 0 0 1</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb149"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb149-1"><a href="#cb149-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl2</span>(tbl2[<span class="dv">2</span><span class="sc">:</span><span class="dv">3</span>, <span class="dv">1</span>] <span class="ot"><-</span> tbl2[<span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>, <span class="dv">2</span>])</span>
|
|||
|
<span id="cb149-2"><a href="#cb149-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in `[<-`:</span></span>
|
|||
|
<span id="cb149-3"><a href="#cb149-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> ! Assigned data `tbl2[1:2,</span></span>
|
|||
|
<span id="cb149-4"><a href="#cb149-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2]` must be compatible with</span></span>
|
|||
|
<span id="cb149-5"><a href="#cb149-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> existing data.</span></span>
|
|||
|
<span id="cb149-6"><a href="#cb149-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> ℹ Error occurred for column `tb`.</span></span>
|
|||
|
<span id="cb149-7"><a href="#cb149-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> Caused by error in `vec_assign()` at ]8;line = 193:col = 2;file:///private/var/folders/dj/yhk9rkx97wn_ykqtnmk18xvc0000gn/T/RtmpECx3vm/Rbuild51ca1b126fa/tibble/R/subassign-backend.Rtibble/R/subassign-backend.R:193:2]8;;:</span></span>
|
|||
|
<span id="cb149-8"><a href="#cb149-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> ! Can't convert <double[,4]> to <tbl_df>.</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb150"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb150-1"><a href="#cb150-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl2</span>(tbl2[<span class="dv">2</span><span class="sc">:</span><span class="dv">3</span>, <span class="dv">2</span>] <span class="ot"><-</span> tbl[<span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>, <span class="dv">1</span>])</span>
|
|||
|
<span id="cb150-2"><a href="#cb150-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 4 × 2</span></span>
|
|||
|
<span id="cb150-3"><a href="#cb150-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> tb$n $c $li m[,1] [,2]</span></span>
|
|||
|
<span id="cb150-4"><a href="#cb150-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int> <chr> <list> <dbl> <dbl></span></span>
|
|||
|
<span id="cb150-5"><a href="#cb150-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e <dbl [1]> 1 0</span></span>
|
|||
|
<span id="cb150-6"><a href="#cb150-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 NA f <int [2]> 1 1</span></span>
|
|||
|
<span id="cb150-7"><a href="#cb150-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 3 g <int [3]> NA NA</span></span>
|
|||
|
<span id="cb150-8"><a href="#cb150-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 NA h <chr [1]> 0 0</span></span>
|
|||
|
<span id="cb150-9"><a href="#cb150-9" aria-hidden="true" tabindex="-1"></a><span class="co">#> # … with 1 more variable:</span></span>
|
|||
|
<span id="cb150-10"><a href="#cb150-10" aria-hidden="true" tabindex="-1"></a><span class="co">#> # m[3:4] <dbl></span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
<p>A notable exception is the population of a column full of
|
|||
|
<code>NA</code> (which is of type <code>logical</code>), or the use of
|
|||
|
<code>NA</code> on the right-hand side of the assignment.</p>
|
|||
|
<table class="dftbl">
|
|||
|
<tbody>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb151"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb151-1"><a href="#cb151-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>({tbl<span class="sc">$</span>x <span class="ot"><-</span> <span class="cn">NA</span>; tbl[<span class="dv">2</span><span class="sc">:</span><span class="dv">3</span>, <span class="st">"x"</span>] <span class="ot"><-</span> <span class="dv">3</span><span class="sc">:</span><span class="dv">2</span>})</span>
|
|||
|
<span id="cb151-2"><a href="#cb151-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 4 × 4</span></span>
|
|||
|
<span id="cb151-3"><a href="#cb151-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li x</span></span>
|
|||
|
<span id="cb151-4"><a href="#cb151-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int> <chr> <list> <int></span></span>
|
|||
|
<span id="cb151-5"><a href="#cb151-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e <dbl [1]> NA</span></span>
|
|||
|
<span id="cb151-6"><a href="#cb151-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 NA f <int [2]> 3</span></span>
|
|||
|
<span id="cb151-7"><a href="#cb151-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 3 g <int [3]> 2</span></span>
|
|||
|
<span id="cb151-8"><a href="#cb151-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 NA h <chr [1]> NA</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb152"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb152-1"><a href="#cb152-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_df</span>({df[<span class="dv">2</span><span class="sc">:</span><span class="dv">3</span>, <span class="dv">2</span><span class="sc">:</span><span class="dv">3</span>] <span class="ot"><-</span> <span class="cn">NA</span>})</span>
|
|||
|
<span id="cb152-2"><a href="#cb152-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li</span></span>
|
|||
|
<span id="cb152-3"><a href="#cb152-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e 9</span></span>
|
|||
|
<span id="cb152-4"><a href="#cb152-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 NA <NA> NA</span></span>
|
|||
|
<span id="cb152-5"><a href="#cb152-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 3 <NA> NA</span></span>
|
|||
|
<span id="cb152-6"><a href="#cb152-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 NA h text</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb153"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb153-1"><a href="#cb153-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>({tbl[<span class="dv">2</span><span class="sc">:</span><span class="dv">3</span>, <span class="dv">2</span><span class="sc">:</span><span class="dv">3</span>] <span class="ot"><-</span> <span class="cn">NA</span>})</span>
|
|||
|
<span id="cb153-2"><a href="#cb153-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 4 × 3</span></span>
|
|||
|
<span id="cb153-3"><a href="#cb153-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li </span></span>
|
|||
|
<span id="cb153-4"><a href="#cb153-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int> <chr> <list> </span></span>
|
|||
|
<span id="cb153-5"><a href="#cb153-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e <dbl [1]></span></span>
|
|||
|
<span id="cb153-6"><a href="#cb153-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 NA <NA> <NULL> </span></span>
|
|||
|
<span id="cb153-7"><a href="#cb153-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 3 <NA> <NULL> </span></span>
|
|||
|
<span id="cb153-8"><a href="#cb153-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 NA h <chr [1]></span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
<p>For programming, it is always safer (and faster) to use the correct
|
|||
|
type of <code>NA</code> to initialize columns.</p>
|
|||
|
<table class="dftbl">
|
|||
|
<tbody>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb154"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb154-1"><a href="#cb154-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>({tbl<span class="sc">$</span>x <span class="ot"><-</span> <span class="cn">NA_integer_</span>; tbl[<span class="dv">2</span><span class="sc">:</span><span class="dv">3</span>, <span class="st">"x"</span>] <span class="ot"><-</span> <span class="dv">3</span><span class="sc">:</span><span class="dv">2</span>})</span>
|
|||
|
<span id="cb154-2"><a href="#cb154-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 4 × 4</span></span>
|
|||
|
<span id="cb154-3"><a href="#cb154-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li x</span></span>
|
|||
|
<span id="cb154-4"><a href="#cb154-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int> <chr> <list> <int></span></span>
|
|||
|
<span id="cb154-5"><a href="#cb154-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e <dbl [1]> NA</span></span>
|
|||
|
<span id="cb154-6"><a href="#cb154-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 NA f <int [2]> 3</span></span>
|
|||
|
<span id="cb154-7"><a href="#cb154-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 3 g <int [3]> 2</span></span>
|
|||
|
<span id="cb154-8"><a href="#cb154-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 NA h <chr [1]> NA</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
<p>For new columns, <code>x[i, j] <- a</code> fills the unassigned
|
|||
|
rows with <code>NA</code>.</p>
|
|||
|
<table class="dftbl">
|
|||
|
<tbody>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb155"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb155-1"><a href="#cb155-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_df</span>(df[<span class="dv">2</span><span class="sc">:</span><span class="dv">3</span>, <span class="st">"n"</span>] <span class="ot"><-</span> <span class="dv">1</span>)</span>
|
|||
|
<span id="cb155-2"><a href="#cb155-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li</span></span>
|
|||
|
<span id="cb155-3"><a href="#cb155-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e 9</span></span>
|
|||
|
<span id="cb155-4"><a href="#cb155-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 1 f 10, 11</span></span>
|
|||
|
<span id="cb155-5"><a href="#cb155-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 1 g 12, 13, 14</span></span>
|
|||
|
<span id="cb155-6"><a href="#cb155-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 NA h text</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb156"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb156-1"><a href="#cb156-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="dv">2</span><span class="sc">:</span><span class="dv">3</span>, <span class="st">"n"</span>] <span class="ot"><-</span> <span class="dv">1</span>)</span>
|
|||
|
<span id="cb156-2"><a href="#cb156-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 4 × 3</span></span>
|
|||
|
<span id="cb156-3"><a href="#cb156-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li </span></span>
|
|||
|
<span id="cb156-4"><a href="#cb156-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int> <chr> <list> </span></span>
|
|||
|
<span id="cb156-5"><a href="#cb156-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e <dbl [1]></span></span>
|
|||
|
<span id="cb156-6"><a href="#cb156-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 1 f <int [2]></span></span>
|
|||
|
<span id="cb156-7"><a href="#cb156-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 1 g <int [3]></span></span>
|
|||
|
<span id="cb156-8"><a href="#cb156-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 NA h <chr [1]></span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb157"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb157-1"><a href="#cb157-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="dv">2</span><span class="sc">:</span><span class="dv">3</span>, <span class="st">"x"</span>] <span class="ot"><-</span> <span class="dv">1</span>)</span>
|
|||
|
<span id="cb157-2"><a href="#cb157-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 4 × 4</span></span>
|
|||
|
<span id="cb157-3"><a href="#cb157-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li x</span></span>
|
|||
|
<span id="cb157-4"><a href="#cb157-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int> <chr> <list> <dbl></span></span>
|
|||
|
<span id="cb157-5"><a href="#cb157-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e <dbl [1]> NA</span></span>
|
|||
|
<span id="cb157-6"><a href="#cb157-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 NA f <int [2]> 1</span></span>
|
|||
|
<span id="cb157-7"><a href="#cb157-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 3 g <int [3]> 1</span></span>
|
|||
|
<span id="cb157-8"><a href="#cb157-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 NA h <chr [1]> NA</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb158"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb158-1"><a href="#cb158-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_df</span>(df[<span class="dv">2</span><span class="sc">:</span><span class="dv">3</span>, <span class="st">"n"</span>] <span class="ot"><-</span> <span class="cn">NULL</span>)</span>
|
|||
|
<span id="cb158-2"><a href="#cb158-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in x[[jj]][iseq] <- vjj: replacement has length zero</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb159"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb159-1"><a href="#cb159-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="dv">2</span><span class="sc">:</span><span class="dv">3</span>, <span class="st">"n"</span>] <span class="ot"><-</span> <span class="cn">NULL</span>)</span>
|
|||
|
<span id="cb159-2"><a href="#cb159-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Error in `[<-`:</span></span>
|
|||
|
<span id="cb159-3"><a href="#cb159-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> ! `NULL` must be a vector, a</span></span>
|
|||
|
<span id="cb159-4"><a href="#cb159-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> bare list, a data frame or a</span></span>
|
|||
|
<span id="cb159-5"><a href="#cb159-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> matrix.</span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
<p>Likewise, for new rows, <code>x[i, j] <- a</code> fills the
|
|||
|
unassigned columns with <code>NA</code>.</p>
|
|||
|
<table class="dftbl">
|
|||
|
<tbody>
|
|||
|
<tr style="vertical-align:top">
|
|||
|
<td>
|
|||
|
</td>
|
|||
|
<td>
|
|||
|
<div class="sourceCode" id="cb160"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb160-1"><a href="#cb160-1" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tbl</span>(tbl[<span class="dv">5</span>, <span class="st">"n"</span>] <span class="ot"><-</span> <span class="fu">list</span>(0L))</span>
|
|||
|
<span id="cb160-2"><a href="#cb160-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> # A tibble: 5 × 3</span></span>
|
|||
|
<span id="cb160-3"><a href="#cb160-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> n c li </span></span>
|
|||
|
<span id="cb160-4"><a href="#cb160-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> <int> <chr> <list> </span></span>
|
|||
|
<span id="cb160-5"><a href="#cb160-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> 1 1 e <dbl [1]></span></span>
|
|||
|
<span id="cb160-6"><a href="#cb160-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> 2 NA f <int [2]></span></span>
|
|||
|
<span id="cb160-7"><a href="#cb160-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> 3 3 g <int [3]></span></span>
|
|||
|
<span id="cb160-8"><a href="#cb160-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> 4 NA h <chr [1]></span></span>
|
|||
|
<span id="cb160-9"><a href="#cb160-9" aria-hidden="true" tabindex="-1"></a><span class="co">#> 5 0 <NA> <NULL></span></span></code></pre></div>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
</div>
|
|||
|
<div id="definition-of-xi-j---a-1" class="section level3">
|
|||
|
<h3>Definition of <code>x[[i, j]] <- a</code></h3>
|
|||
|
<p><code>i</code> must be a numeric vector of length 1.
|
|||
|
<code>x[[i, j]] <- a</code> is equivalent to
|
|||
|
<code>x[i, ][[j]] <- a</code>.<a href="#fn9" class="footnote-ref" id="fnref9"><sup>9</sup></a></p>
|
|||
|
<p>NB: <code>vec_size(a)</code> must equal 1. Unlike
|
|||
|
<code>x[i, ] <-</code>, <code>x[[i, ]] <-</code> is not valid.</p>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<div class="footnotes footnotes-end-of-document">
|
|||
|
<hr />
|
|||
|
<ol>
|
|||
|
<li id="fn1"><p><code>x[j][[jj]]</code> is equal to
|
|||
|
<code>x[[ j[[jj]] ]]</code>, in particular <code>x[j][[1]]</code> is
|
|||
|
equal to <code>x[[j]]</code> for scalar numeric or integer
|
|||
|
<code>j</code>.<a href="#fnref1" class="footnote-back">↩︎</a></p></li>
|
|||
|
<li id="fn2"><p>Row subsetting <code>x[i, ]</code> is not defined in
|
|||
|
terms of <code>x[[j]][i]</code> because that definition does not
|
|||
|
generalise to matrix and data frame columns. For efficiency and backward
|
|||
|
compatibility, <code>i</code> is converted to an integer vector by
|
|||
|
<code>vec_as_index(i, nrow(x))</code> first.<a href="#fnref2" class="footnote-back">↩︎</a></p></li>
|
|||
|
<li id="fn3"><p><code>x[,]</code> is equivalent to <code>x[]</code>
|
|||
|
because <code>x[, j]</code> is equivalent to <code>x[j]</code>.<a href="#fnref3" class="footnote-back">↩︎</a></p></li>
|
|||
|
<li id="fn4"><p>A more efficient implementation of <code>x[i, j]</code>
|
|||
|
would forward to <code>x[j][i, ]</code>.<a href="#fnref4" class="footnote-back">↩︎</a></p></li>
|
|||
|
<li id="fn5"><p>Cell subsetting <code>x[[i, j]]</code> is not defined in
|
|||
|
terms of <code>x[[j]][[i]]</code> because that definition does not
|
|||
|
generalise to list, matrix and data frame columns. A more efficient
|
|||
|
implementation of <code>x[[i, j]]</code> would check that <code>j</code>
|
|||
|
is a scalar and forward to <code>x[i, j][[1]]</code>.<a href="#fnref5" class="footnote-back">↩︎</a></p></li>
|
|||
|
<li id="fn6"><p><code>$</code> behaves almost completely symmetrically
|
|||
|
to <code>[[</code> when comparing subsetting and subassignment.<a href="#fnref6" class="footnote-back">↩︎</a></p></li>
|
|||
|
<li id="fn7"><p><code>x[i, ]</code> is symmetrical for subset and
|
|||
|
subassignment.<a href="#fnref7" class="footnote-back">↩︎</a></p></li>
|
|||
|
<li id="fn8"><p><code>x[i, j]</code> is symmetrical for subsetting and
|
|||
|
subassignment. A more efficient implementation of
|
|||
|
<code>x[i, j] <- a</code> would forward to
|
|||
|
<code>x[j][i, ] <- a</code>.<a href="#fnref8" class="footnote-back">↩︎</a></p></li>
|
|||
|
<li id="fn9"><p><code>x[[i, j]]</code> is symmetrical for subsetting and
|
|||
|
subassignment. An efficient implementation would check that
|
|||
|
<code>i</code> and <code>j</code> are scalar and forward to
|
|||
|
<code>x[i, j][[1]] <- a</code>.<a href="#fnref9" class="footnote-back">↩︎</a></p></li>
|
|||
|
</ol>
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<!-- code folding -->
|
|||
|
|
|||
|
|
|||
|
<!-- dynamically load mathjax for compatibility with self-contained -->
|
|||
|
<script>
|
|||
|
(function () {
|
|||
|
var script = document.createElement("script");
|
|||
|
script.type = "text/javascript";
|
|||
|
script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
|
|||
|
document.getElementsByTagName("head")[0].appendChild(script);
|
|||
|
})();
|
|||
|
</script>
|
|||
|
|
|||
|
</body>
|
|||
|
</html>
|