
[.tbl_df is careful about names (#1245)
=======================================

> foo <- tibble(x = 1:10, y = 1:10)
> foo[c("x", "y", "z")]
Error: Can't subset columns that don't exist.
x The column `z` doesn't exist.

> foo[c("w", "x", "y", "z")]
Error: Can't subset columns that don't exist.
x The columns `w` and `z` don't exist.

> foo[as.matrix("x")]
Error: Subscript `as.matrix("x")` is a matrix, it must be of type logical.

> foo[array("x", dim = c(1, 1, 1))]
Error: `i` must have one dimension, not 3.


[.tbl_df is careful about column indexes (#83)
==============================================

> foo <- tibble(x = 1:10, y = 1:10, z = 1:10)
> foo[0.5]
Error: Must subset columns with a valid subscript vector.
x Lossy cast from `0.5` <double> to <integer>.

> foo[1:5]
Error: Can't subset columns that don't exist.
x The locations 4 and 5 don't exist.
i There are only 3 columns.

> foo[-1:1]
Error: Must subset columns with a valid subscript vector.
x Negative locations can't be mixed with positive locations.
i The subscript `-1:1` has a positive value at location 3.

> foo[c(-1, 1)]
Error: Must subset columns with a valid subscript vector.
x Negative locations can't be mixed with positive locations.
i The subscript `c(-1, 1)` has a positive value at location 2.

> foo[c(-1, NA)]
Error: Can't use NA as column index with `[` at position 2.

> foo[-4]
Error: Can't negate columns that don't exist.
x The location 4 doesn't exist.
i There are only 3 columns.

> foo[c(1:3, NA)]
Error: Can't use NA as column index with `[` at position 4.

> foo[as.matrix(1)]
Error: Subscript `as.matrix(1)` is a matrix, it must be of type logical.

> foo[array(1, dim = c(1, 1, 1))]
Error: `i` must have one dimension, not 3.

> foo[mean]
Error: Must subset columns with a valid subscript vector.
x The subscript `mean` has the wrong type `closure`.
i It must be logical, numeric, or character.

> foo[foo]
Error: Must subset columns with a valid subscript vector.
x The subscript `foo` has the wrong type `tbl_df<
  x: integer
  y: integer
  z: integer
>`.
i It must be logical, numeric, or character.


[.tbl_df is careful about row indexes
=====================================

> foo <- tibble(x = 1:3, y = 1:3, z = 1:3)
> foo[0.5, ]
Error: Must subset rows with a valid subscript vector.
x Lossy cast from `0.5` <double> to <integer>.

> invisible(foo[1:5, ])
Warning: The `i` argument of ``[.tbl_df`()` must lie in [0, rows] if positive, as of tibble 3.0.0.
Use `NA` as row index to obtain a row full of `NA` values.

> foo[-1:1, ]
Error: Must subset rows with a valid subscript vector.
x Negative locations can't be mixed with positive locations.
i The subscript `-1:1` has a positive value at location 3.

> foo[c(-1, 1), ]
Error: Must subset rows with a valid subscript vector.
x Negative locations can't be mixed with positive locations.
i The subscript `c(-1, 1)` has a positive value at location 2.

> foo[c(-1, NA), ]
Error: Must subset rows with a valid subscript vector.
x Negative locations can't have missing values.
i The subscript `c(-1, NA)` has a missing value at location 2.

> invisible(foo[-4, ])
Warning: The `i` argument of ``[.tbl_df`()` must lie in [-rows, 0] if negative, as of tibble 3.0.0.
Use `NA` as row index to obtain a row full of `NA` values.

> foo[array(1, dim = c(1, 1, 1)), ]
Error: `i` must have one dimension, not 3.

> foo[mean, ]
Error: Must subset rows with a valid subscript vector.
x The subscript `mean` has the wrong type `closure`.
i It must be logical, numeric, or character.

> foo[foo, ]
Error: Must subset rows with a valid subscript vector.
x The subscript `foo` has the wrong type `tbl_df<
  x: integer
  y: integer
  z: integer
>`.
i It must be logical, numeric, or character.


[.tbl_df is careful about column flags (#83)
============================================

> foo <- tibble(x = 1:10, y = 1:10, z = 1:10)
> foo[c(TRUE, TRUE)]
Error: Must subset columns with a valid subscript vector.
i Logical subscripts must match the size of the indexed input.
x The input has size 3 but the subscript `c(TRUE, TRUE)` has size 2.

> foo[c(TRUE, TRUE, FALSE, FALSE)]
Error: Must subset columns with a valid subscript vector.
i Logical subscripts must match the size of the indexed input.
x The input has size 3 but the subscript `c(TRUE, TRUE, FALSE, FALSE)` has size 4.

> foo[c(TRUE, TRUE, NA)]
Error: Can't use NA as column index with `[` at position 3.

> foo[as.matrix(TRUE)]
Error: Subscript `as.matrix(TRUE)` is a matrix, it must have the same dimensions as the input.

> foo[array(TRUE, dim = c(1, 1, 1))]
Error: `i` must have one dimension, not 3.


[.tbl_df is careful about row flags
===================================

> foo <- tibble(x = 1:3, y = 1:3, z = 1:3)
> foo[c(TRUE, TRUE), ]
Error: Must subset rows with a valid subscript vector.
i Logical subscripts must match the size of the indexed input.
x The input has size 3 but the subscript `c(TRUE, TRUE)` has size 2.

> foo[c(TRUE, TRUE, FALSE, FALSE), ]
Error: Must subset rows with a valid subscript vector.
i Logical subscripts must match the size of the indexed input.
x The input has size 3 but the subscript `c(TRUE, TRUE, FALSE, FALSE)` has size 4.

> foo[array(TRUE, dim = c(1, 1, 1)), ]
Error: `i` must have one dimension, not 3.


[.tbl_df rejects unknown column indexes (#83)
=============================================

> foo <- tibble(x = 1:10, y = 1:10, z = 1:10)
> foo[list(1:3)]
Error: Must subset columns with a valid subscript vector.
x The subscript `list(1:3)` has the wrong type `list`.
i It must be logical, numeric, or character.

> foo[as.list(1:3)]
Error: Must subset columns with a valid subscript vector.
x The subscript `as.list(1:3)` has the wrong type `list`.
i It must be logical, numeric, or character.

> foo[factor(1:3)]
Error: Can't subset columns that don't exist.
x The columns `1`, `2`, and `3` don't exist.

> foo[Sys.Date()]
Error: Must subset columns with a valid subscript vector.
x The subscript `Sys.Date()` has the wrong type `date`.
i It must be logical, numeric, or character.


[.tbl_df rejects unknown row indexes
====================================

> foo <- tibble(x = 1:10, y = 1:10, z = 1:10)
> foo[list(1:3), ]
Error: Must subset rows with a valid subscript vector.
x The subscript `list(1:3)` has the wrong type `list`.
i It must be logical, numeric, or character.

> foo[as.list(1:3), ]
Error: Must subset rows with a valid subscript vector.
x The subscript `as.list(1:3)` has the wrong type `list`.
i It must be logical, numeric, or character.

> foo[factor(1:3), ]
Error: Can't use character names to index an unnamed vector.

> foo[Sys.Date(), ]
Error: Must subset rows with a valid subscript vector.
x The subscript `Sys.Date()` has the wrong type `date`.
i It must be logical, numeric, or character.


[.tbl_df and matrix subsetting
==============================

> foo <- tibble(a = 1:3, b = letters[1:3])
> foo[is.na(foo)]
<unspecified> [0]

> foo[!is.na(foo)]
Error: No common type for `a` <integer> and `b` <character>.

> foo[as.matrix("x")]
Error: Subscript `as.matrix("x")` is a matrix, it must be of type logical.

> foo[array("x", dim = c(1, 1, 1))]
Error: `i` must have one dimension, not 3.


[.tbl_df and OOB indexing
=========================

> foo <- tibble(a = 1:3, b = letters[1:3])
> invisible(foo[3:5, ])
Warning: The `i` argument of ``[.tbl_df`()` must lie in [0, rows] if positive, as of tibble 3.0.0.
Use `NA` as row index to obtain a row full of `NA` values.

> invisible(foo[-(3:5), ])
Warning: The `i` argument of ``[.tbl_df`()` must lie in [-rows, 0] if negative, as of tibble 3.0.0.
Use `NA` as row index to obtain a row full of `NA` values.

> invisible(foo["x", ])
Warning: The `i` argument of ``[.tbl_df`()` must use valid row names as of tibble 3.0.0.
Use `NA` as row index to obtain a row full of `NA` values.


[.tbl_df and logical recycling
==============================

> foo <- tibble(a = 1:4, b = a)
> foo[c(TRUE, FALSE), ]
Error: Must subset rows with a valid subscript vector.
i Logical subscripts must match the size of the indexed input.
x The input has size 4 but the subscript `c(TRUE, FALSE)` has size 2.


[[.tbl_df rejects invalid column indexes
========================================

> foo <- tibble(x = 1:10, y = 1:10)
> foo[[]]
Error: Subscript can't be missing for tibbles in `[[`.

> foo[[, 1]]
Error: Subscript can't be missing for tibbles in `[[`.

> foo[[1, ]]
Error: Subscript can't be missing for tibbles in `[[`.

> foo[[, ]]
Error: Subscript can't be missing for tibbles in `[[`.

> foo[[1:3]]
Error: Must extract column with a single valid subscript.
x The subscript `1:3` has size 3 but must be size 1.

> foo[[letters[1:3]]]
Error: Must extract column with a single valid subscript.
x The subscript `letters[1:3]` has size 3 but must be size 1.

> foo[[TRUE]]
Error: Must extract column with a single valid subscript.
x The subscript `TRUE` has the wrong type `logical`.
i It must be numeric or character.

> foo[[mean]]
Error: Must extract column with a single valid subscript.
x The subscript `mean` has the wrong type `closure`.
i It must be numeric or character.

> foo[[foo]]
Error: Must extract column with a single valid subscript.
x The subscript `foo` has the wrong type `tbl_df<
  x: integer
  y: integer
>`.
i It must be numeric or character.


[[.tbl_df throws error with NA index
====================================

> foo <- tibble(x = 1:10, y = 1:10)
> foo[[NA]]
Error: Must extract column with a single valid subscript.
x The subscript `NA` can't be `NA`.


$.tbl_df and partial matching/invalid columns
=============================================

> foo <- tibble(data = 1:10)
> foo$d
Warning: Unknown or uninitialised column: `d`.

NULL

> foo$e
Warning: Unknown or uninitialised column: `e`.

NULL


[<-.tbl_df rejects unknown column indexes (#83)
===============================================

> foo <- tibble(x = 1:10, y = 1:10, z = 1:10)
> foo[list(1:3)] <- 1
Error: Must assign to columns with a valid subscript vector.
x The subscript `list(1:3)` has the wrong type `list`.
i It must be logical, numeric, or character.

> foo[as.list(1:3)] <- 1
Error: Must assign to columns with a valid subscript vector.
x The subscript `as.list(1:3)` has the wrong type `list`.
i It must be logical, numeric, or character.

> foo[factor(1:3)] <- 1
Error: Can't assign to columns that don't exist.
x The columns `1`, `2`, and `3` don't exist.

> foo[Sys.Date()] <- 1
Error: Must assign to columns with a valid subscript vector.
x The subscript `Sys.Date()` has the wrong type `date`.
i It must be logical, numeric, or character.


[.tbl_df emits lifecycle warnings with one-column matrix indexes (#760)
=======================================================================

> foo <- tibble(x = 1:10, y = 1:10, z = 1:10)
> invisible(foo[matrix(1:2, ncol = 1), ])
Warning: The `i` argument of ``[`()` can't be a matrix as of tibble 3.0.0.
Convert to a vector.

> invisible(foo[matrix(rep(TRUE, 10), ncol = 1), ])
Warning: The `i` argument of ``[`()` can't be a matrix as of tibble 3.0.0.
Convert to a vector.


[<-.tbl_df rejects unknown row indexes
======================================

> foo <- tibble(x = 1:10, y = 1:10, z = 1:10)
> foo[list(1:3), ] <- 1
Error: Must assign to rows with a valid subscript vector.
x The subscript `list(1:3)` has the wrong type `list`.
i It must be logical, numeric, or character.

> foo[as.list(1:3), ] <- 1
Error: Must assign to rows with a valid subscript vector.
x The subscript `as.list(1:3)` has the wrong type `list`.
i It must be logical, numeric, or character.

> foo[factor(1:3), ] <- 1
Error: Can't use character names to index an unnamed vector.

> foo[Sys.Date(), ] <- 1
Error: Must assign to rows with a valid subscript vector.
x The subscript `Sys.Date()` has the wrong type `date`.
i It must be logical, numeric, or character.


[<-.tbl_df throws an error with duplicate indexes (#658)
========================================================

> df <- tibble(x = 1:2, y = x)
> df[c(1, 1)] <- 3
Error: Column index 1 is used more than once for assignment.

> df[, c(1, 1)] <- 3
Error: Column index 1 is used more than once for assignment.

> df[c(1, 1), ] <- 3
Error: Row index 1 is used more than once for assignment.


[<-.tbl_df throws an error with NA indexes
==========================================

> df <- tibble(x = 1:2, y = x)
> df[NA] <- 3
Error: Can't use NA as column index in a tibble for assignment.

> df[NA, ] <- 3
Error: Can't use NA as row index in a tibble for assignment.


[<-.tbl_df throws an error with bad RHS
=======================================

> df <- tibble(x = 1:2, y = x)
> df[] <- mean
Error: `mean` must be a vector, a bare list, a data frame, a matrix, or NULL.

> df[] <- lm(y ~ x, df)
Error: `lm(y ~ x, df)` must be a vector, a bare list, a data frame, a matrix, or NULL.


[<-.tbl_df throws an error with OOB assignment
==============================================

> df <- tibble(x = 1:2, y = x)
> df[4:5] <- 3
Error: Can't assign columns 4 and 5 in a tibble with 2 columns.

> df[4:5, ] <- 3
Error: Can't assign rows 4 and 5 in a tibble with 2 rows.

> df[-4, ] <- 3
Error: Can't negate rows that don't exist.
x The location 4 doesn't exist.
i There are only 2 rows.

> df[-(4:5), ] <- 3
Error: Can't negate rows that don't exist.
x The locations 4 and 5 don't exist.
i There are only 2 rows.


[<-.tbl_df and recycling
========================

> df <- tibble(x = 1:3, y = x, z = y)
> df[1:2] <- list(0, 0, 0)
Error: `list(0, 0, 0)` can't be recycled to size 2.
x It must be size 2 or 1, not 3.

> df[] <- list(0, 0)
Error: `x` can't be recycled to size 3.
x It must be size 3 or 1, not 2.

> df[1, ] <- 1:3
Error: Assigned data `1:3` must be compatible with row subscript `1`.
x 1 row must be assigned.
x Assigned data has 3 rows.
i Row updates require a list value. Do you need `list()` or `as.list()`?

> df[1:2, ] <- 1:3
Error: Assigned data `1:3` must be compatible with row subscript `1:2`.
x 2 rows must be assigned.
x Assigned data has 3 rows.
i Only vectors of size 1 are recycled.

> df[, ] <- 1:2
Error: Assigned data `1:2` must be compatible with existing data.
x Existing data has 3 rows.
x Assigned data has 2 rows.
i Only vectors of size 1 are recycled.


[<-.tbl_df and coercion
=======================

> df <- tibble(x = 1:3, y = letters[1:3], z = as.list(1:3))
> df[1:3, 1:2] <- df[2:3]
Error: Assigned data `df[2:3]` must be compatible with existing data.
i Error occurred for column `x`.
x No common type for `value` <character> and `x` <integer>.

> df[1:3, 1:2] <- df[1]
Error: Assigned data `df[1]` must be compatible with existing data.
i Error occurred for column `y`.
x No common type for `value` <integer> and `x` <character>.

> df[1:3, 1:2] <- df[[1]]
Error: Assigned data `df[[1]]` must be compatible with existing data.
i Error occurred for column `y`.
x No common type for `value` <integer> and `x` <character>.

> df[1:3, 1:3] <- df[3:1]
Error: Assigned data `df[3:1]` must be compatible with existing data.
i Error occurred for column `x`.
x No common type for `value` <list> and `x` <integer>.

> df[1:3, 1:3] <- NULL
Error: `NULL` must be a vector, a bare list, a data frame or a matrix.


[<-.tbl_df and overwriting NA
=============================

> df <- tibble(x = rep(NA, 3))
> df[1, "x"] <- 5
Error: Assigned data `5` must be compatible with existing data.
i Error occurred for column `x`.
x Lossy cast from `value` <double> to `x` <logical>.
* Locations: 1.


[<-.tbl_df and matrix subsetting
================================

> foo <- tibble(a = 1:3, b = letters[1:3])
> foo[!is.na(foo)] <- "bogus"
Error: Assigned data `"bogus"` must be compatible with existing data.
i Error occurred for column `a`.
x No common type for `value` <character> and `x` <integer>.

> foo[as.matrix("x")] <- NA
Error: Subscript `as.matrix("x")` is a matrix, it must be of type logical.

> foo[array("x", dim = c(1, 1, 1))] <- NA
> foo[is.na(foo)] <- 1:3
Error: Subscript `is.na(foo)` is a matrix, the data `1:3` must have size 1.

> foo[is.na(foo)] <- lm(a ~ b, foo)
Error: Subscript `is.na(foo)` is a matrix, the data `lm(a ~ b, foo)` must be a vector of size 1.


[[.tbl_df rejects invalid column indexes
========================================

> foo <- tibble(x = 1:10, y = 1:10)
> foo[[]] <- 1
Error: Subscript can't be missing for tibbles in `[[<-`.

> foo[[, 1]] <- 1
Error: Subscript can't be missing for tibbles in `[[<-`.

> foo[[1, ]] <- 1
Error: Subscript can't be missing for tibbles in `[[<-`.

> foo[[, ]] <- 1
Error: Subscript can't be missing for tibbles in `[[<-`.

> foo[[1:3]] <- 1
Error: Must assign to column with a single valid subscript.
x The subscript `1:3` has size 3 but must be size 1.

> foo[[letters[1:3]]] <- 1
Error: Must assign to column with a single valid subscript.
x The subscript `letters[1:3]` has size 3 but must be size 1.

> foo[[TRUE]] <- 1
Error: Must assign to column with a single valid subscript.
x The subscript `TRUE` has size 2 but must be size 1.

> foo[[mean]] <- 1
Error: Must assign to columns with a valid subscript vector.
x The subscript `mean` has the wrong type `closure`.
i It must be logical, numeric, or character.

> foo[[foo]] <- 1
Error: Must assign to columns with a valid subscript vector.
x The subscript `foo` has the wrong type `tbl_df<
  x: integer
  y: integer
>`.
i It must be logical, numeric, or character.

> foo[[1:3, 1]] <- 1
Error: Must assign to row with a single valid subscript.
x The subscript `1:3` has size 3 but must be size 1.

> foo[[TRUE, 1]] <- 1
Error: Must assign to row with a single valid subscript.
x The subscript `TRUE` has the wrong type `logical`.
i It must be numeric or character.

> foo[[mean, 1]] <- 1
Error: Must assign to row with a single valid subscript.
x The subscript `mean` has the wrong type `closure`.
i It must be numeric or character.

> foo[[foo, 1]] <- 1
Error: Must assign to row with a single valid subscript.
x The subscript `foo` has the wrong type `tbl_df<
  x: integer
  y: integer
>`.
i It must be numeric or character.


[[<-.tbl_df throws an error with OOB assignment
===============================================

> df <- tibble(x = 1:2, y = x)
> df[[4]] <- 3
Error: Can't assign column 4 in a tibble with 2 columns.


[[<-.tbl_df throws an error with bad RHS
========================================

> df <- tibble(x = 1:2, y = x)
> df[[1]] <- mean
Error: `x` must be a vector, not a function.

> df[[1]] <- lm(y ~ x, df)
Error: `x` must be a vector, not a `lm` object.


[[<-.tbl_df recycles only values of length one
==============================================

> df <- tibble(x = 1:3)
> df[["x"]] <- 8:9
Error: Assigned data `8:9` must be compatible with existing data.
x Existing data has 3 rows.
x Assigned data has 2 rows.
i Only vectors of size 1 are recycled.

> df[["w"]] <- 8:9
Error: Assigned data `8:9` must be compatible with existing data.
x Existing data has 3 rows.
x Assigned data has 2 rows.
i Only vectors of size 1 are recycled.

> df[["a"]] <- character()
Error: Assigned data `character()` must be compatible with existing data.
x Existing data has 3 rows.
x Assigned data has 0 rows.
i Only vectors of size 1 are recycled.


[<-.tbl_df throws an error with invalid values
==============================================

> df <- tibble(x = 1:2, y = x)
> df[1] <- lm(y ~ x, df)
Error: `lm(y ~ x, df)` must be a vector, a bare list, a data frame, a matrix, or NULL.

> df[1:2, 1] <- NULL
Error: `NULL` must be a vector, a bare list, a data frame or a matrix.


$<- recycles only values of length one
======================================

> df <- tibble(x = 1:3)
> df$x <- 8:9
Error: Assigned data `8:9` must be compatible with existing data.
x Existing data has 3 rows.
x Assigned data has 2 rows.
i Only vectors of size 1 are recycled.

> df$w <- 8:9
Error: Assigned data `8:9` must be compatible with existing data.
x Existing data has 3 rows.
x Assigned data has 2 rows.
i Only vectors of size 1 are recycled.

> df$a <- character()
Error: Assigned data `character()` must be compatible with existing data.
x Existing data has 3 rows.
x Assigned data has 0 rows.
i Only vectors of size 1 are recycled.

