% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/subsetting.R
\name{subsetting}
\alias{subsetting}
\alias{$.tbl_df}
\alias{$<-.tbl_df}
\alias{[[.tbl_df}
\alias{[[<-.tbl_df}
\alias{[.tbl_df}
\alias{[<-.tbl_df}
\title{Subsetting tibbles}
\usage{
\method{$}{tbl_df}(x, name)

\method{$}{tbl_df}(x, name) <- value

\method{[[}{tbl_df}(x, i, j, ..., exact = TRUE)

\method{[[}{tbl_df}(x, i, j, ...) <- value

\method{[}{tbl_df}(x, i, j, drop = FALSE, ...)

\method{[}{tbl_df}(x, i, j, ...) <- value
}
\arguments{
\item{x}{A tibble.}

\item{name}{A \link{name} or a string.}

\item{value}{A value to store in a row, column, range or cell.
Tibbles are stricter than data frames in what is accepted here.}

\item{i, j}{Row and column indices. If \code{j} is omitted, \code{i} is used as column index.}

\item{...}{Ignored.}

\item{exact}{Ignored, with a warning.}

\item{drop}{Coerce to a vector if fetching one column via \code{tbl[, j]} .
Default \code{FALSE}, ignored when accessing a column via \code{tbl[j]} .}
}
\description{
Accessing columns, rows, or cells via \code{$}, \code{[[}, or \code{[} is mostly similar to
\link[base:Extract]{regular data frames}. However, the
behavior is different for tibbles and data frames in some cases:
\itemize{
\item \code{[} always returns a tibble by default, even if
only one column is accessed.
\item Partial matching of column names with \code{$} and \code{[[} is not supported, and
\code{NULL} is returned.
For \code{$}, a warning is given.
\item Only scalars (vectors of length one) or vectors with the
same length as the number of rows can be used for assignment.
\item Rows outside of the tibble's boundaries cannot be accessed.
\item When updating with \verb{[[<-} and \verb{[<-}, 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{\link[=vec_slice]{vec_slice()}} for the underlying implementation.
}

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{\link[=as.data.frame]{as.data.frame()}}.
}
\details{
For better compatibility with older code written for regular data frames,
\code{[} supports a \code{drop} argument which defaults to \code{FALSE}.
New code should use \code{[[} to turn a column into a vector.
}
\examples{
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, ])

\dontshow{if ((Sys.getenv("NOT_CRAN") != "true" || Sys.getenv("IN_PKGDOWN") == "true")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf}
# 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
\dontshow{\}) # examplesIf}

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"]]
}
