EVOLUTION-MANAGER
Edit File: subsetting.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Subsetting tibbles</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for subsetting {tibble}"><tr><td>subsetting {tibble}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Subsetting tibbles</h2> <h3>Description</h3> <p>Accessing columns, rows, or cells via <code>$</code>, <code>[[</code>, or <code>[</code> is mostly similar to <a href="../../base/html/Extract.html">regular data frames</a>. However, the behavior is different for tibbles and data frames in some cases: </p> <ul> <li> <p><code>[</code> always returns a tibble by default, even if only one column is accessed. </p> </li> <li><p> Partial matching of column names with <code>$</code> and <code>[[</code> is not supported, and <code>NULL</code> is returned. For <code>$</code>, a warning is given. </p> </li> <li><p> Only scalars (vectors of length one) or vectors with the same length as the number of rows can be used for assignment. </p> </li> <li><p> Rows outside of the tibble's boundaries cannot be accessed. </p> </li> <li><p> When updating with <code style="white-space: pre;">[[<-</code> and <code style="white-space: pre;">[<-</code>, type changes of entire columns are supported, but updating a part of a column requires that the new value is coercible to the existing type. See <code><a href="../../vctrs/html/vec_slice.html">vec_slice()</a></code> for the underlying implementation. </p> </li></ul> <p>Unstable return type and implicit partial matching can lead to surprises and bugs that are hard to catch. If you rely on code that requires the original data frame behavior, coerce to a data frame via <code><a href="../../base/html/as.data.frame.html">as.data.frame()</a></code>. </p> <h3>Usage</h3> <pre> ## S3 method for class 'tbl_df' x$name ## S3 replacement method for class 'tbl_df' x$name <- value ## S3 method for class 'tbl_df' x[[i, j, ..., exact = TRUE]] ## S3 replacement method for class 'tbl_df' x[[i, j, ...]] <- value ## S3 method for class 'tbl_df' x[i, j, drop = FALSE, ...] ## S3 replacement method for class 'tbl_df' x[i, j, ...] <- value </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>x</code></td> <td> <p>A tibble.</p> </td></tr> <tr valign="top"><td><code>name</code></td> <td> <p>A <a href="../../base/html/name.html">name</a> or a string.</p> </td></tr> <tr valign="top"><td><code>value</code></td> <td> <p>A value to store in a row, column, range or cell. Tibbles are stricter than data frames in what is accepted here.</p> </td></tr> <tr valign="top"><td><code>i, j</code></td> <td> <p>Row and column indices. If <code>j</code> is omitted, <code>i</code> is used as column index.</p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>Ignored.</p> </td></tr> <tr valign="top"><td><code>exact</code></td> <td> <p>Ignored, with a warning.</p> </td></tr> <tr valign="top"><td><code>drop</code></td> <td> <p>Coerce to a vector if fetching one column via <code>tbl[, j]</code> . Default <code>FALSE</code>, ignored when accessing a column via <code>tbl[j]</code> .</p> </td></tr> </table> <h3>Details</h3> <p>For better compatibility with older code written for regular data frames, <code>[</code> supports a <code>drop</code> argument which defaults to <code>FALSE</code>. New code should use <code>[[</code> to turn a column into a vector. </p> <h3>Examples</h3> <pre> df <- data.frame(a = 1:3, bc = 4:6) tbl <- tibble(a = 1:3, bc = 4:6) # Subsetting single columns: df[, "a"] tbl[, "a"] tbl[, "a", drop = TRUE] as.data.frame(tbl)[, "a"] # Subsetting single rows with the drop argument: df[1, , drop = TRUE] tbl[1, , drop = TRUE] as.list(tbl[1, ]) # Accessing non-existent columns: df$b tbl$b df[["b", exact = FALSE]] tbl[["b", exact = FALSE]] df$bd <- c("n", "e", "w") tbl$bd <- c("n", "e", "w") df$b tbl$b df$b <- 7:9 tbl$b <- 7:9 df$b tbl$b # Identical behavior: tbl[1, ] tbl[1, c("bc", "a")] tbl[, c("bc", "a")] tbl[c("bc", "a")] tbl["a"] tbl$a tbl[["a"]] </pre> <hr /><div style="text-align: center;">[Package <em>tibble</em> version 3.1.8 <a href="00Index.html">Index</a>]</div> </body></html>