vignettes/invariants.md

title: "Invariants for subsetting and subassignment"

output: rmarkdown::word_document

output: rmarkdown::html_vignette

devtools::load_all(); eval_details <- TRUE; rmarkdown::render("vignettes/invariants.Rmd", output_format = rmarkdown::md_document(preserve_yaml = TRUE)); system("pandoc vignettes/invariants.md -o vignettes/invariants.html")

vignette: > %\VignetteIndexEntry{invariants} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8}

.dftbl { width: 100%; table-layout: fixed; display: inline-table; } .error pre code { color: red; } .warning pre code { color: violet; }

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. vec_slice(), vec_recycle() and vec_as_index(). Refer to their documentation for more details about the invariants that they follow.

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 [[ and $, then column-wise subsetting with [, then row-wise subsetting, then the composition of both.

Conventions

In this article, all behaviors are demonstrated using one example data frame and its tibble equivalent:

library(vctrs)
library(tibble)
new_df <- function() {
  df <- data.frame(n = c(1L, NA, 3L, NA))
  df$c <- letters[5:8]
  df$li <- list(9, 10:11, 12:14, "text")
  df
}
new_tbl <- function() {
  as_tibble(new_df())
}

Results of the same code for data frames and tibbles are presented side by side:

new_df() #> n c li #> 1 1 e 9 #> 2 NA f 10, 11 #> 3 3 g 12, 13, 14 #> 4 NA h text new_tbl() #> # A tibble: 4 x 3 #> n c li #> #> 1 1 e #> 2 NA f #> 3 3 g #> 4 NA h

If the results are identical (after converting to a data frame if necessary), only the tibble result is shown.

Subsetting operations are read-only. The same objects are reused in all examples:

df <- new_df()
tbl <- new_tbl()

Where needed, we also show examples with hierarchical columns containing a data frame or a matrix:

new_tbl2 <- function() {
  tibble(
    tb = tbl,
    m = diag(4)
  )
}
new_df2 <- function() {
  df2 <- new_tbl2()
  class(df2) <- "data.frame"
  class(df2$tb) <- "data.frame"
  df2
}
df2 <- new_df2()
tbl2 <- new_tbl2()
new_tbl() #> # A tibble: 4 x 3 #> n c li #> #> 1 1 e #> 2 NA f #> 3 3 g #> 4 NA h

For subset assignment (subassignment, for short), we need a fresh copy of the data for each test. The with_*() 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.

with_df(df$n <- rev(df$n), verbose = TRUE) #> { #> df <- new_df() #> df$n <- rev(df$n) #> df #> } #> n c li #> 1 NA e 9 #> 2 3 f 10, 11 #> 3 NA g 12, 13, 14 #> 4 1 h text with_tbl(tbl$n <- rev(tbl$n), verbose = TRUE) #> { #> tbl <- new_tbl() #> tbl$n <- rev(tbl$n) #> tbl #> } #> # A tibble: 4 x 3 #> n c li #> #> 1 NA e #> 2 3 f #> 3 NA g #> 4 1 h

Column extraction

Definition of x[[j]]

x[[j]] is equal to .subset2(x, j).

tbl[[1]] #> [1] 1 NA 3 NA .subset2(tbl, 1) #> [1] 1 NA 3 NA identical(tbl[[3]], .subset2(tbl, 3)) #> [1] TRUE identical(tbl2[["tbl"]], .subset2(tbl2, "tbl")) #> [1] TRUE

NB: x[[j]] always returns an object of size nrow(x) if the column exists.

vec_size(tbl[[1]]) #> [1] 4 vec_size(tbl[[3]]) #> [1] 4 vec_size(tbl2[[1]]) #> [1] 4 vec_size(tbl2[[2]]) #> [1] 4

j must be a single number or a string, as enforced by .subset2(x, j).

df[[1:2]] #> [1] NA tbl[[1:2]] #> Warning: Calling `[[` with a vector #> of length 2 (recursive subsetting) #> is deprecated and will eventually be #> converted to an error. #> [1] NA df[[c("n", "c")]] #> Error in .subset2(x, i, exact = #> exact): subscript out of bounds tbl[[c("n", "c")]] #> Error: Must use a scalar in `[[`. df[[TRUE]] #> [1] 1 NA 3 NA tbl[[TRUE]] #> Error: Must extract with a single #> index. #> [31mx[39m `j` has the wrong type #> `logical`. #> [34mℹ[39m This index must be a #> position or a name. df[[mean]] #> Error in .subset2(x, i, exact = #> exact): invalid subscript type #> 'closure' tbl[[mean]] #> Error: Must extract with a single #> index. #> [31mx[39m `j` has the wrong type #> `closure`. #> [34mℹ[39m This index must be a #> position or a name.

NA indexes, numeric out-of-bounds (OOB) values, and non-integers throw an error:

df[[NA]] #> NULL tbl[[NA]] #> Error: Must extract with a single #> index. #> [31mx[39m `j` can't be `NA`. #> [34mℹ[39m This index can't be #> missing. df[[NA_character_]] #> NULL tbl[[NA_character_]] #> Error: Must extract with a single #> index. #> [31mx[39m `j` can't be `NA`. #> [34mℹ[39m This index can't be #> missing. df[[NA_integer_]] #> NULL tbl[[NA_integer_]] #> Error: Must extract with a single #> index. #> [31mx[39m `j` can't be `NA`. #> [34mℹ[39m This index can't be #> missing. df[[-1]] #> Error in .subset2(x, i, exact = #> exact): attempt to select more than #> one element in get1index tbl[[-1]] #> Error: Must extract with a single #> index. #> [31mx[39m `j` (with value -1) has #> the wrong sign. #> [34mℹ[39m This index must be a #> positive integer. df[[4]] #> Error in .subset2(x, i, exact = #> exact): subscript out of bounds tbl[[4]] #> Error: Must index existing elements. #> [31mx[39m Can't subset position 4. #> [34mℹ[39m There are only 3 #> elements. df[[1.5]] #> [1] 1 NA 3 NA tbl[[1.5]] #> Error: Must extract with a single #> index. #> [31mx[39m Lossy cast from `j` #> to . df[[Inf]] #> NULL tbl[[Inf]] #> Error: Must extract with a single #> index. #> [31mx[39m Lossy cast from `j` #> to .

Character OOB access is silent because a common package idiom is to check for the absence of a column with is.null(df[[var]]).

tbl[["x"]] #> NULL

Definition of x$name

x$name and x$"name" are equal to x[["name"]].

tbl$n #> [1] 1 NA 3 NA tbl$"n" #> [1] 1 NA 3 NA tbl[["n"]] #> [1] 1 NA 3 NA identical(tbl$li, tbl[["li"]]) #> [1] TRUE identical(tbl2$tb, tbl2[["tb"]]) #> [1] TRUE identical(tbl2$m, tbl2[["m"]]) #> [1] TRUE

Unlike data frames, tibbles do not partially match names. Because df$x is rarely used in packages, it can raise a warning:

df$l #> Warning in df$l: partial match of 'l' to #> 'li' #> [[1]] #> [1] 9 #> #> [[2]] #> [1] 10 11 #> #> [[3]] #> [1] 12 13 14 #> #> [[4]] #> [1] "text" tbl$l #> Warning: Unknown or uninitialised #> column: `l`. #> NULL df$not_present #> NULL tbl$not_present #> Warning: Unknown or uninitialised #> column: `not_present`. #> NULL

Column subsetting

Definition of x[j]

j is converted to an integer vector by vec_as_index(j, ncol(x), names = names(x)). Then x[c(j_1, j_2, ..., j_n)] is equivalent to tibble(x[[j_1]], x[[j_2]], ..., x[[j_3]]), keeping the corresponding column names. This implies that j must be a numeric or character vector, or a logical vector with length 1 or ncol(x).[1]

tbl[1:2] #> # A tibble: 4 x 2 #> n c #> #> 1 1 e #> 2 NA f #> 3 3 g #> 4 NA h

When subsetting repeated indexes, the resulting column names are undefined, do not rely on them.

df[c(1, 1)] #> n n.1 #> 1 1 1 #> 2 NA NA #> 3 3 3 #> 4 NA NA tbl[c(1, 1)] #> # A tibble: 4 x 2 #> n n #> #> 1 1 1 #> 2 NA NA #> 3 3 3 #> 4 NA NA

For tibbles with repeated column names, subsetting by name uses the first matching column.

nrow(df[j]) equals nrow(df).

tbl[integer()] #> # A tibble: 4 x 0

Tibbles support indexing by a logical matrix, but only if all values in the returned vector are compatible.

df[is.na(df)] #> [[1]] #> [1] NA #> #> [[2]] #> [1] NA tbl[is.na(tbl)] #> [1] NA NA df[!is.na(df)] #> [[1]] #> [1] 1 #> #> [[2]] #> [1] 3 #> #> [[3]] #> [1] "e" #> #> [[4]] #> [1] "f" #> #> [[5]] #> [1] "g" #> #> [[6]] #> [1] "h" #> #> [[7]] #> [1] 9 #> #> [[8]] #> [1] 10 11 #> #> [[9]] #> [1] 12 13 14 #> #> [[10]] #> [1] "text" tbl[!is.na(tbl)] #> Error: No common type for `n` #> and `c` .

Definition of x[, j]

x[, j] is equal to x[j]. Tibbles do not perform column extraction if x[j] would yield a single column.

df[, 1] #> [1] 1 NA 3 NA tbl[, 1] #> # A tibble: 4 x 1 #> n #> #> 1 1 #> 2 NA #> 3 3 #> 4 NA tbl[, 1:2] #> # A tibble: 4 x 2 #> n c #> #> 1 1 e #> 2 NA f #> 3 3 g #> 4 NA h identical(tbl[, 2:3], tbl[2:3]) #> [1] TRUE identical(tbl2[, 1:2], tbl2[1:2]) #> [1] TRUE

Definition of x[, j, drop = TRUE]

For backward compatiblity, x[, j, drop = TRUE] performs column extraction, returning x[j][[1]] when ncol(x[j]) is 1.

tbl[, 1, drop = TRUE] #> [1] 1 NA 3 NA identical(tbl[, 3, drop = TRUE], tbl[[3]]) #> [1] TRUE identical(tbl2[, 1, drop = TRUE], tbl2[[1]]) #> [1] TRUE identical(tbl2[, 2, drop = TRUE], tbl2[[2]]) #> [1] TRUE

Row subsetting

Definition of x[i, ]

x[i, ] is equal to tibble(vec_slice(x[[1]], i), vec_slice(x[[2]], i), ...).[2]

tbl[3, ] #> # A tibble: 1 x 3 #> n c li #> #> 1 3 g

This means that i must be a numeric vector, or a logical vector of length nrow(x) or 1. For compatibility, i can also be a character vector containing positive numbers.

df[mean, ] #> Error in xj[i]: invalid subscript #> type 'closure' tbl[mean, ] #> Error: Must subset with an index #> vector. #> [31mx[39m `i` has the wrong type #> `closure`. #> [34mℹ[39m These indices must be #> indicators, positions or names. df[list(1), ] #> Error in xj[i]: invalid subscript #> type 'list' tbl[list(1), ] #> Error: Must subset with an index #> vector. #> [31mx[39m `i` has the wrong type #> `list`. #> [34mℹ[39m These indices must be #> indicators, positions or names. tbl["1", ] #> # A tibble: 1 x 3 #> n c li #> #> 1 1 e

Exception: OOB values generate warnings instead of errors:

df[10, ] #> n c li #> NA NA NULL tbl[10, ] #> Warning: Row indexes must be between 0 #> and the number of rows (4). Use `NA` as #> row index to obtain a row full of `NA` #> values. #> # A tibble: 1 x 3 #> n c li #> #> 1 NA df["x", ] #> n c li #> NA NA NULL tbl["x", ] #> Warning: Only valid row names can be #> used for indexing. Use `NA` as row index #> to obtain a row full of `NA` values. #> # A tibble: 1 x 3 #> n c li #> #> 1 NA

Unlike data frames, only logical vectors of length 1 are recycled.

df[c(TRUE, FALSE), ] #> n c li #> 1 1 e 9 #> 3 3 g 12, 13, 14 tbl[c(TRUE, FALSE), ] #> Error: Logical indices must have #> length 1 or be as long as the #> indexed vector. #> The vector has size 4 whereas the #> index has size 2.

NB: scalar logicals are recycled, but scalar numerics are not. That makes the x[NA, ] and x[NA_integer_, ] return different results.

tbl[NA, ] #> # A tibble: 4 x 3 #> n c li #> #> 1 NA #> 2 NA #> 3 NA #> 4 NA tbl[NA_integer_, ] #> # A tibble: 1 x 3 #> n c li #> #> 1 NA

Definition of x[i, , drop = TRUE]

drop = TRUE has no effect when not selecting a single row:

df[1, , drop = TRUE] #> $n #> [1] 1 #> #> $c #> [1] "e" #> #> $li #> $li[[1]] #> [1] 9 tbl[1, , drop = TRUE] #> # A tibble: 1 x 3 #> n c li #> #> 1 1 e

Row and column subsetting

Definition of x[] and x[,]

x[] and x[,] are equivalent to x.[3]

Definition of x[i, j]

x[i, j] is equal to x[i, ][j].[4]

df[1, 1] #> [1] 1 tbl[1, 1] #> # A tibble: 1 x 1 #> n #> #> 1 1 tbl[1, ][1] #> # A tibble: 1 x 1 #> n #> #> 1 1 identical(tbl[1, 2:3], tbl[2:3][1, ]) #> [1] TRUE identical(tbl[2:3, 1], tbl[1][2:3, ]) #> [1] TRUE identical(tbl2[2:3, 1:2], tbl2[1:2][2:3, ]) #> [1] TRUE

Definition of x[[i, j]]

i must be a numeric vector of length 1. x[[i, j]] is equal to x[i, ][[j]].[5]

This implies that j must be a numeric or character vector of length 1.

NB: vec_size(x[[i, j]]) always equals 1. Unlike x[i, ], x[[i, ]] is not valid.

Column update

Definition of x[[j]] <- a

If a is a vector then x[[j]] <- a replaces the jth column with value a.

with_tbl(tbl[[1]] <- 0) #> # A tibble: 4 x 3 #> n c li #> #> 1 0 e #> 2 0 f #> 3 0 g #> 4 0 h with_tbl(tbl[[3]] <- 4:1) #> # A tibble: 4 x 3 #> n c li #> #> 1 1 e 4 #> 2 NA f 3 #> 3 3 g 2 #> 4 NA h 1 with_tbl2(tbl2[[1]] <- 0) #> # A tibble: 4 x 2 #> tb m[,1] [,2] [,3] [,4] #> #> 1 0 1 0 0 0 #> 2 0 0 1 0 0 #> 3 0 0 0 1 0 #> 4 0 0 0 0 1 with_tbl2(tbl2[[2]] <- 4:1) #> # A tibble: 4 x 2 #> tb$n $c $li m #> #> 1 1 e 4 #> 2 NA f 3 #> 3 3 g 2 #> 4 NA h 1 with_tbl(tbl[[1]] <- 0) #> # A tibble: 4 x 3 #> n c li #> #> 1 0 e #> 2 0 f #> 3 0 g #> 4 0 h with_tbl(tbl[["c"]] <- 0) #> # A tibble: 4 x 3 #> n c li #> #> 1 1 0 #> 2 NA 0 #> 3 3 0 #> 4 NA 0 with_df(df[[TRUE]] <- 0) #> n c li #> 1 0 e 9 #> 2 0 f 10, 11 #> 3 0 g 12, 13, 14 #> 4 0 h text with_tbl(tbl[[TRUE]] <- 0) #> Error: Must use a scalar in `[[`. with_df(df[[1:3]] <- 0) #> Error in `[[<-`(`*tmp*`, i, value = #> value): recursive indexing failed at #> level 2 with_tbl(tbl[[1:3]] <- 0) #> Error: Must use a scalar in `[[`. with_df(df[[c("n", "c")]] <- 0) #> Error in x[[i]] <- value: more #> elements supplied than there are to #> replace with_tbl(tbl[[c("n", "c")]] <- 0) #> Error: Must use a scalar in `[[`. with_df(df[[FALSE]] <- 0) #> Error in x[[i]] <- value: attempt to #> select less than one element in #> integerOneIndex with_tbl(tbl[[FALSE]] <- 0) #> Error: Must use a scalar in `[[`. with_df(df[[1:2]] <- 0) #> Error in x[[i]] <- value: more #> elements supplied than there are to #> replace with_tbl(tbl[[1:2]] <- 0) #> Error: Must use a scalar in `[[`. with_df(df[[NA_integer_]] <- 0) #> Error in x[[i]] <- value: attempt to #> select more than one element in #> integerOneIndex with_tbl(tbl[[NA_integer_]] <- 0) #> Error: Can't use NA as column index #> in a tibble for assignment. with_df(df[[NA]] <- 0) #> Error in x[[i]] <- value: attempt to #> select more than one element in #> integerOneIndex with_tbl(tbl[[NA]] <- 0) #> Error: Can't use NA as column index #> in a tibble for assignment. with_df(df[[NA_character_]] <- 0) #> Error in if (names(x)[nc] == "") #> names(x)[nc] <- paste0("V", nc): #> missing value where TRUE/FALSE #> needed with_tbl(tbl[[NA_character_]] <- 0) #> Error: Can't use NA as column index #> in a tibble for assignment.

a is recycled to the same size as x so must have size nrow(x) or 1. (The only exception is when a is NULL, as described below.) Recycling also works for list, data frame, and matrix columns.

with_tbl(tbl[["li"]] <- list(0)) #> # A tibble: 4 x 3 #> n c li #> #> 1 1 e #> 2 NA f #> 3 3 g #> 4 NA h with_df2(df2[["tb"]] <- df[1, ]) #> Error in `[[<-.data.frame`(`*tmp*`, #> "tb", value = structure(list(n = 1L, #> : replacement has 1 row, data has 4 with_tbl2(tbl2[["tb"]] <- tbl[1, ]) #> # A tibble: 4 x 2 #> tb$n $c $li m[,1] [,2] [,3] #> #> 1 1 e 2 1 e 3 1 e 4 1 e # … with 1 more variable: [,4] with_df2(df2[["m"]] <- df2[["m"]][1, , drop = FALSE]) #> Error in `[[<-.data.frame`(`*tmp*`, #> "m", value = structure(c(1, 0, 0, : #> replacement has 1 row, data has 4 with_tbl2(tbl2[["m"]] <- tbl2[["m"]][1, , drop = FALSE]) #> # A tibble: 4 x 2 #> tb$n $c $li m[,1] [,2] [,3] #> #> 1 1 e 2 NA f 3 3 g 4 NA h # … with 1 more variable: [,4] with_tbl(tbl[[1]] <- 1) #> # A tibble: 4 x 3 #> n c li #> #> 1 1 e #> 2 1 f #> 3 1 g #> 4 1 h with_tbl(tbl[[1]] <- 4:1) #> # A tibble: 4 x 3 #> n c li #> #> 1 4 e #> 2 3 f #> 3 2 g #> 4 1 h with_df(df[[1]] <- 3:1) #> Error in `[[<-.data.frame`(`*tmp*`, #> 1, value = 3:1): replacement has 3 #> rows, data has 4 with_tbl(tbl[[1]] <- 3:1) #> Error: Tibble columns must have #> consistent sizes, only values of #> size one are recycled: #> * Size 4: Existing data #> * Size 3: Column `n` with_df(df[[1]] <- 2:1) #> n c li #> 1 2 e 9 #> 2 1 f 10, 11 #> 3 2 g 12, 13, 14 #> 4 1 h text with_tbl(tbl[[1]] <- 2:1) #> Error: Tibble columns must have #> consistent sizes, only values of #> size one are recycled: #> * Size 4: Existing data #> * Size 2: Column `n`

j must be a scalar numeric or a string, and cannot be NA. If j is OOB, a new column is added on the right hand side, with name repair if needed.

with_tbl(tbl[["x"]] <- 0) #> # A tibble: 4 x 4 #> n c li x #> #> 1 1 e 0 #> 2 NA f 0 #> 3 3 g 0 #> 4 NA h 0 with_df(df[[4]] <- 0) #> n c li V4 #> 1 1 e 9 0 #> 2 NA f 10, 11 0 #> 3 3 g 12, 13, 14 0 #> 4 NA h text 0 with_tbl(tbl[[4]] <- 0) #> # A tibble: 4 x 4 #> n c li ...4 #> #> 1 1 e 0 #> 2 NA f 0 #> 3 3 g 0 #> 4 NA h 0 with_df(df[[5]] <- 0) #> Warning in format.data.frame(if (omit) #> x[seq_len(n0), , drop = FALSE] else #> x, : corrupt data frame: columns will be #> truncated or padded with NAs #> n c li V5 #> 1 1 e 9 NULL 0 #> 2 NA f 10, 11 0 #> 3 3 g 12, 13, 14 0 #> 4 NA h text 0 with_tbl(tbl[[5]] <- 0) #> Error: Can't assign column 5 in a #> tibble with 3 columns.

df[[j]] <- a replaces the complete column so can change the type.

with_tbl(tbl[[1]] <- tbl[[2]]) #> # A tibble: 4 x 3 #> n c li #> #> 1 e e #> 2 f f #> 3 g g #> 4 h h with_tbl(tbl[[2]] <- tbl[[3]]) #> # A tibble: 4 x 3 #> n c li #> #> 1 1 #> 2 NA #> 3 3 #> 4 NA with_tbl(tbl[[3]] <- tbl2[[1]]) #> # A tibble: 4 x 3 #> n c li$n $c $li #> #> 1 1 e 1 e #> 2 NA f NA f #> 3 3 g 3 g #> 4 NA h NA h with_tbl2(tbl2[[1]] <- tbl2[[2]]) #> # A tibble: 4 x 2 #> tb[,1] [,2] [,3] [,4] m[,1] [,2] #> #> 1 1 0 0 0 1 0 #> 2 0 1 0 0 0 1 #> 3 0 0 1 0 0 0 #> 4 0 0 0 1 0 0 #> # … with 2 more variables: [,3] , #> # [,4] with_tbl2(tbl2[[2]] <- tbl[[1]]) #> # A tibble: 4 x 2 #> tb$n $c $li m #> #> 1 1 e 1 #> 2 NA f NA #> 3 3 g 3 #> 4 NA h NA

[[<- supports removing a column by assigning NULL to it.

with_tbl(tbl[[1]] <- NULL) #> # A tibble: 4 x 2 #> c li #> #> 1 e #> 2 f #> 3 g #> 4 h with_tbl2(tbl2[[2]] <- NULL) #> # A tibble: 4 x 1 #> tb$n $c $li #> #> 1 1 e #> 2 NA f #> 3 3 g #> 4 NA h

Removing a nonexistent column is a no-op.

with_tbl(tbl[["q"]] <- NULL) #> # A tibble: 4 x 3 #> n c li #> #> 1 1 e #> 2 NA f #> 3 3 g #> 4 NA h

Definition of x$name <- a

x$name <- a and x$"name" <- a are equivalent to x[["name"]] <- a.[6]

with_tbl(tbl$n <- 0) #> # A tibble: 4 x 3 #> n c li #> #> 1 0 e #> 2 0 f #> 3 0 g #> 4 0 h with_tbl(tbl[["n"]] <- 0) #> # A tibble: 4 x 3 #> n c li #> #> 1 0 e #> 2 0 f #> 3 0 g #> 4 0 h with_tbl(tbl$"n" <- 0) #> # A tibble: 4 x 3 #> n c li #> #> 1 0 e #> 2 0 f #> 3 0 g #> 4 0 h

$<- does not perform partial matching.

with_tbl(tbl$l <- 0) #> # A tibble: 4 x 4 #> n c li l #> #> 1 1 e 0 #> 2 NA f 0 #> 3 3 g 0 #> 4 NA h 0 with_tbl(tbl[["l"]] <- 0) #> # A tibble: 4 x 4 #> n c li l #> #> 1 1 e 0 #> 2 NA f 0 #> 3 3 g 0 #> 4 NA h 0

Column subassignment: x[j] <- a

a is a list or data frame

If inherits(a, "list") or inherits(a, "data.frame") is TRUE, then x[j] <- a is equivalent to x[[j[[1]]] <- a[[1]], x[[j[[2]]]] <- a[[2]], …

with_tbl(tbl[1:2] <- list("x", 4:1)) #> # A tibble: 4 x 3 #> n c li #> #> 1 x 4 #> 2 x 3 #> 3 x 2 #> 4 x 1 with_tbl(tbl[c("li", "x", "c")] <- list("x", 4:1, NULL)) #> # A tibble: 4 x 3 #> n li x #> #> 1 1 x 4 #> 2 NA x 3 #> 3 3 x 2 #> 4 NA x 1

If length(a) equals 1, then it is recycled to the same length as j.

with_tbl(tbl[1:2] <- list(1)) #> # A tibble: 4 x 3 #> n c li #> #> 1 1 1 #> 2 1 1 #> 3 1 1 #> 4 1 1 with_df(df[1:2] <- list(0, 0, 0)) #> Warning in `[<-.data.frame`(`*tmp*`, #> 1:2, value = list(0, 0, 0)): provided 3 #> variables to replace 2 variables #> n c li #> 1 0 0 9 #> 2 0 0 10, 11 #> 3 0 0 12, 13, 14 #> 4 0 0 text with_tbl(tbl[1:2] <- list(0, 0, 0)) #> Error: Vector of length 3 cannot be #> recycled to length 2. Only vectors #> of length one can be recycled. with_df(df[1:3] <- list(0, 0)) #> n c li #> 1 0 0 0 #> 2 0 0 0 #> 3 0 0 0 #> 4 0 0 0 with_tbl(tbl[1:3] <- list(0, 0)) #> Error: Vector of length 2 cannot be #> recycled to length 3. Only vectors #> of length one can be recycled.

An attempt to update the same column twice gives an error.

with_df(df[c(1, 1)] <- list(1, 2)) #> Error in `[<-.data.frame`(`*tmp*`, #> c(1, 1), value = list(1, 2)): #> duplicate subscripts for columns with_tbl(tbl[c(1, 1)] <- list(1, 2)) #> Error: Column index 1 is used more #> than once for assignment.

If a contains NULL values, the corresponding columns are removed after updating (i.e. position indexes refer to columns before any modifications).

with_tbl(tbl[1:2] <- list(NULL, 4:1)) #> # A tibble: 4 x 2 #> c li #> #> 1 4 #> 2 3 #> 3 2 #> 4 1

NA indexes are not supported.

with_df(df[NA] <- list("x")) #> Error in `[<-.data.frame`(`*tmp*`, #> NA, value = list("x")): missing #> values are not allowed in #> subscripted assignments of data #> frames with_tbl(tbl[NA] <- list("x")) #> Error: Can't use NA as column index #> in a tibble for assignment. with_df(df[NA_integer_] <- list("x")) #> Error in `[<-.data.frame`(`*tmp*`, #> NA_integer_, value = list("x")): #> missing values are not allowed in #> subscripted assignments of data #> frames with_tbl(tbl[NA_integer_] <- list("x")) #> Error: Can't use NA as column index #> in a tibble for assignment. with_df(df[NA_character_] <- list("x")) #> Error in `[<-.data.frame`(`*tmp*`, #> NA_character_, value = list("x")): #> missing values are not allowed in #> subscripted assignments of data #> frames with_tbl(tbl[NA_character_] <- list("x")) #> Error: Can't use NA as column index #> in a tibble for assignment.

Just like column updates, [<- supports changing the type of an existing column.

with_tbl(tbl[1] <- tbl[2]) #> # A tibble: 4 x 3 #> n c li #> #> 1 e e #> 2 f f #> 3 g g #> 4 h h with_tbl(tbl[2] <- tbl[3]) #> # A tibble: 4 x 3 #> n c li #> #> 1 1 #> 2 NA #> 3 3 #> 4 NA with_tbl(tbl[3] <- tbl2[1]) #> # A tibble: 4 x 3 #> n c li$n $c $li #> #> 1 1 e 1 e #> 2 NA f NA f #> 3 3 g 3 g #> 4 NA h NA h with_tbl2(tbl2[1] <- tbl2[2]) #> # A tibble: 4 x 2 #> tb[,1] [,2] [,3] [,4] m[,1] [,2] #> #> 1 1 0 0 0 1 0 #> 2 0 1 0 0 0 1 #> 3 0 0 1 0 0 0 #> 4 0 0 0 1 0 0 #> # … with 2 more variables: [,3] , #> # [,4] with_tbl2(tbl2[2] <- tbl[1]) #> # A tibble: 4 x 2 #> tb$n $c $li m #> #> 1 1 e 1 #> 2 NA f NA #> 3 3 g 3 #> 4 NA h NA

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).

with_tbl(tbl[c("x", "y")] <- tibble("x", x = 4:1)) #> # A tibble: 4 x 5 #> n c li x y #> #> 1 1 e x 4 #> 2 NA f x 3 #> 3 3 g x 2 #> 4 NA h x 1 with_tbl(tbl[3:4] <- list("x", x = 4:1)) #> # A tibble: 4 x 4 #> n c li x #> #> 1 1 e x 4 #> 2 NA f x 3 #> 3 3 g x 2 #> 4 NA h x 1 with_df(df[4] <- list(4:1)) #> n c li V4 #> 1 1 e 9 4 #> 2 NA f 10, 11 3 #> 3 3 g 12, 13, 14 2 #> 4 NA h text 1 with_tbl(tbl[4] <- list(4:1)) #> # A tibble: 4 x 4 #> n c li ...4 #> #> 1 1 e 4 #> 2 NA f 3 #> 3 3 g 2 #> 4 NA h 1 with_df(df[5] <- list(4:1)) #> Error in `[<-.data.frame`(`*tmp*`, #> 5, value = list(4:1)): new columns #> would leave holes after existing #> columns with_tbl(tbl[5] <- list(4:1)) #> Error: Can't assign column 5 in a #> tibble with 3 columns.

Tibbles support indexing by a logical matrix, but only for a scalar RHS, and if all columns updated are compatible with the value assigned.

with_df(df[is.na(df)] <- 4) #> n c li #> 1 1 e 9 #> 2 4 f 10, 11 #> 3 3 g 12, 13, 14 #> 4 4 h text with_tbl(tbl[is.na(tbl)] <- 4) #> # A tibble: 4 x 3 #> n c li #> #> 1 1 e #> 2 4 f #> 3 3 g #> 4 4 h with_df(df[is.na(df)] <- 1:2) #> n c li #> 1 1 e 9 #> 2 1 f 10, 11 #> 3 3 g 12, 13, 14 #> 4 2 h text with_tbl(tbl[is.na(tbl)] <- 1:2) #> Error in tbl_subassign_matrix(x, j, #> value): vec_is(value, size = 1) is #> not TRUE with_df(df[matrix(c(rep(TRUE, 5), rep(FALSE, 7)), ncol = 3)] <- 4) #> n c li #> 1 4 4 9 #> 2 4 f 10, 11 #> 3 4 g 12, 13, 14 #> 4 4 h text with_tbl(tbl[matrix(c(rep(TRUE, 5), rep(FALSE, 7)), ncol = 3)] <- 4) #> Error: No common type for `value` #> and `x` .

a is another type of vector

If vec_is(a), then x[j] <- a is equivalent to x[j] <- list(a). This is primarily provided for backward compatbility.

with_tbl(tbl[1] <- 0) #> # A tibble: 4 x 3 #> n c li #> #> 1 0 e #> 2 0 f #> 3 0 g #> 4 0 h with_tbl(tbl[1] <- list(0)) #> # A tibble: 4 x 3 #> n c li #> #> 1 0 e #> 2 0 f #> 3 0 g #> 4 0 h

Matrices are vectors, so they are also wrapped in list() before assignment. This consistently creates matrix columns, unlike data frames, which creates matrix columns when assigning to one column, but treats the matrix like a data frame when assigning to more than one column.

with_tbl(tbl[1] <- matrix(1:8, ncol = 2)) #> # A tibble: 4 x 3 #> n[,1] [,2] c li #> #> 1 1 5 e #> 2 2 6 f #> 3 3 7 g #> 4 4 8 h with_df(df[c(1, 2)] <- matrix(1:8, ncol = 2)) #> n c li #> 1 1 5 9 #> 2 2 6 10, 11 #> 3 3 7 12, 13, 14 #> 4 4 8 text with_tbl(tbl[c(1, 2)] <- matrix(1:8, ncol = 2)) #> # A tibble: 4 x 3 #> n[,1] [,2] c[,1] [,2] li #> #> 1 1 5 1 5 #> 2 2 6 2 6 #> 3 3 7 3 7 #> 4 4 8 4 8

a is not a vector

Any other type for a is an error. Note that if is.list(a) is TRUE, but inherits(a, "list") is FALSE, then a is considered to be a scalar. See ?vec_is and ?vec_proxy for details.

with_df(df[1] <- mean) #> Error in rep(value, length.out = n): #> attempt to replicate an object of #> type 'closure' with_tbl(tbl[1] <- mean) #> Error in tbl_subassign(x, i, j, #> value): is_bare_list(value) is not #> TRUE with_df(df[1] <- lm(mpg ~ wt, data = mtcars)) #> Warning in `[<-.data.frame`(`*tmp*`, #> 1, value = structure(list(coefficients #> = c(`(Intercept)` = 37.285126167342, : #> replacement element 2 has 32 rows to #> replace 4 rows #> Warning in `[<-.data.frame`(`*tmp*`, #> 1, value = structure(list(coefficients #> = c(`(Intercept)` = 37.285126167342, : #> replacement element 3 has 32 rows to #> replace 4 rows #> Warning in `[<-.data.frame`(`*tmp*`, #> 1, value = structure(list(coefficients #> = c(`(Intercept)` = 37.285126167342, : #> replacement element 5 has 32 rows to #> replace 4 rows #> Warning in `[<-.data.frame`(`*tmp*`, #> 1, value = structure(list(coefficients #> = c(`(Intercept)` = 37.285126167342, : #> replacement element 7 has 5 rows to #> replace 4 rows #> Error in `[<-.data.frame`(`*tmp*`, #> 1, value = #> structure(list(coefficients = #> c(`(Intercept)` = 37.285126167342, : #> replacement element 10 has 3 rows, #> need 4 with_tbl(tbl[1] <- lm(mpg ~ wt, data = mtcars)) #> Error: Vector of length 12 cannot be #> recycled to length 1. Only vectors #> of length one can be recycled.

Row subassignment: x[i, ] <- list(...)

x[i, ] <- a is the same as vec_slice(x[[j_1]], i) <- a[[1]], vec_slice(x[[j_2]], i) <- a[[2]], … .[7]

with_tbl(tbl[2:3, ] <- tbl[1, ]) #> # A tibble: 4 x 3 #> n c li #> #> 1 1 e #> 2 1 e #> 3 1 e #> 4 NA h with_tbl(tbl[c(FALSE, TRUE, TRUE, FALSE), ] <- tbl[1, ]) #> # A tibble: 4 x 3 #> n c li #> #> 1 1 e #> 2 1 e #> 3 1 e #> 4 NA h with_tbl(tbl[0:2, ] <- tbl[1, ]) #> # A tibble: 4 x 3 #> n c li #> #> 1 1 e #> 2 1 e #> 3 3 g #> 4 NA h with_tbl(tbl[0, ] <- tbl[1, ]) #> # A tibble: 4 x 3 #> n c li #> #> 1 1 e #> 2 NA f #> 3 3 g #> 4 NA h with_tbl(tbl[-2, ] <- tbl[1, ]) #> # A tibble: 4 x 3 #> n c li #> #> 1 1 e #> 2 NA f #> 3 1 e #> 4 1 e with_df(df[-1:2, ] <- df[1, ]) #> Error in seq_len(nrows)[i]: only 0's #> may be mixed with negative #> subscripts with_tbl(tbl[-1:2, ] <- tbl[1, ]) #> Error: Can't subset with a mix of #> negative and positive indices with_df(df[NA_integer_, ] <- df[1, ]) #> Error in `[<-.data.frame`(`*tmp*`, #> NA_integer_, , value = #> structure(list(: missing values are #> not allowed in subscripted #> assignments of data frames with_tbl(tbl[NA_integer_, ] <- tbl[1, ]) #> Error: Can't use NA as row index in #> a tibble for assignment. with_df2(df2[NA_integer_, ] <- df2[1, ]) #> Error in `[<-.data.frame`(`*tmp*`, #> NA_integer_, , value = #> structure(list(: missing values are #> not allowed in subscripted #> assignments of data frames with_tbl2(tbl2[NA_integer_, ] <- tbl2[1, ]) #> Error: Can't use NA as row index in #> a tibble for assignment. with_tbl(tbl[TRUE, ] <- tbl[1, ]) #> # A tibble: 4 x 3 #> n c li #> #> 1 1 e #> 2 1 e #> 3 1 e #> 4 1 e with_tbl(tbl[FALSE, ] <- tbl[1, ]) #> # A tibble: 4 x 3 #> n c li #> #> 1 1 e #> 2 NA f #> 3 3 g #> 4 NA h with_df(df[NA, ] <- df[1, ]) #> Error in `[<-.data.frame`(`*tmp*`, #> NA, , value = structure(list(n = 1L, #> : missing values are not allowed in #> subscripted assignments of data #> frames with_tbl(tbl[NA, ] <- tbl[1, ]) #> Error: Can't use NA as row index in #> a tibble for assignment.

Only values of size one can be recycled.

with_tbl(tbl[2:3, ] <- tbl[1, ]) #> # A tibble: 4 x 3 #> n c li #> #> 1 1 e #> 2 1 e #> 3 1 e #> 4 NA h with_tbl(tbl[2:3, ] <- list(tbl$n[1], tbl$c[1:2], tbl$li[1])) #> # A tibble: 4 x 3 #> n c li #> #> 1 1 e #> 2 1 e #> 3 1 f #> 4 NA h with_df(df[2:4, ] <- df[1:2, ]) #> Error in `[<-.data.frame`(`*tmp*`, #> 2:4, , value = structure(list(n = #> c(1L, : replacement element 1 has 2 #> rows, need 3 with_tbl(tbl[2:4, ] <- tbl[1:2, ]) #> Error: Vector of length 2 cannot be #> recycled to length 3. Only vectors #> of length one can be recycled. with_df2(df2[2:4, ] <- df2[1, ]) #> Error in `[<-.data.frame`(`*tmp*`, #> 2:4, , value = structure(list(tb = #> structure(list(: replacement element #> 1 is a matrix/data frame of 1 row, #> need 3 with_tbl2(tbl2[2:4, ] <- tbl2[1, ]) #> # A tibble: 4 x 2 #> tb$n $c $li m[,1] [,2] [,3] #> #> 1 1 e 2 1 e 3 1 e 4 1 e # … with 1 more variable: [,4] with_df2(df2[2:4, ] <- df2[2:3, ]) #> Error in `[<-.data.frame`(`*tmp*`, #> 2:4, , value = structure(list(tb = #> structure(list(: replacement element #> 1 is a matrix/data frame of 2 rows, #> need 3 with_tbl2(tbl2[2:4, ] <- tbl2[2:3, ]) #> Error: Vector of length 2 cannot be #> recycled to length 3. Only vectors #> of length one can be recycled.

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.

with_tbl(tbl[5, ] <- tbl[1, ]) #> # A tibble: 5 x 3 #> n c li #> #> 1 1 e #> 2 NA f #> 3 3 g #> 4 NA h #> 5 1 e with_tbl(tbl[5:7, ] <- tbl[1, ]) #> # A tibble: 7 x 3 #> n c li #> #> 1 1 e #> 2 NA f #> 3 3 g #> 4 NA h #> 5 1 e #> 6 1 e #> 7 1 e with_df(df[6, ] <- df[1, ]) #> n c li #> 1 1 e 9 #> 2 NA f 10, 11 #> 3 3 g 12, 13, 14 #> 4 NA h text #> 5 NA NULL #> 6 1 e 9 with_tbl(tbl[6, ] <- tbl[1, ]) #> Error: Can't assign row 6 in a #> tibble with 4 rows. with_df(df[-5, ] <- df[1, ]) #> n c li #> 1 1 e 9 #> 2 1 e 9 #> 3 1 e 9 #> 4 1 e 9 with_tbl(tbl[-5, ] <- tbl[1, ]) #> Error: Must index existing elements. #> [31mx[39m Can't subset position 5. #> [34mℹ[39m There are only 4 #> elements. with_df(df[-(5:7), ] <- df[1, ]) #> n c li #> 1 1 e 9 #> 2 1 e 9 #> 3 1 e 9 #> 4 1 e 9 with_tbl(tbl[-(5:7), ] <- tbl[1, ]) #> Error: Must index existing elements. #> [31mx[39m Can't subset positions #> 5, 6 and 7. #> [34mℹ[39m There are only 4 #> elements. with_df(df[-6, ] <- df[1, ]) #> n c li #> 1 1 e 9 #> 2 1 e 9 #> 3 1 e 9 #> 4 1 e 9 with_tbl(tbl[-6, ] <- tbl[1, ]) #> Error: Must index existing elements. #> [31mx[39m Can't subset position 6. #> [34mℹ[39m There are only 4 #> elements.

For compatibility, i can also be a character vector containing positive numbers.

with_tbl(tbl[as.character(1:3), ] <- tbl[1, ]) #> # A tibble: 4 x 3 #> n c li #> #> 1 1 e #> 2 1 e #> 3 1 e #> 4 NA h with_df(df[as.character(-(1:3)), ] <- df[1, ]) #> n c li #> 1 1 e 9 #> 2 NA f 10, 11 #> 3 3 g 12, 13, 14 #> 4 NA h text #> -1 1 e 9 #> -2 1 e 9 #> -3 1 e 9 with_tbl(tbl[as.character(-(1:3)), ] <- tbl[1, ]) #> Warning: Only valid row names can be #> used for indexing. Use `NA` as row index #> to obtain a row full of `NA` values. #> Error: Can't use NA as row index in #> a tibble for assignment. with_df(df[as.character(3:5), ] <- df[1, ]) #> n c li #> 1 1 e 9 #> 2 NA f 10, 11 #> 3 1 e 9 #> 4 1 e 9 #> 5 1 e 9 with_tbl(tbl[as.character(3:5), ] <- tbl[1, ]) #> Warning: Only valid row names can be #> used for indexing. Use `NA` as row index #> to obtain a row full of `NA` values. #> Error: Can't use NA as row index in #> a tibble for assignment. with_df(df[as.character(-(3:5)), ] <- df[1, ]) #> n c li #> 1 1 e 9 #> 2 NA f 10, 11 #> 3 3 g 12, 13, 14 #> 4 NA h text #> -3 1 e 9 #> -4 1 e 9 #> -5 1 e 9 with_tbl(tbl[as.character(-(3:5)), ] <- tbl[1, ]) #> Warning: Only valid row names can be #> used for indexing. Use `NA` as row index #> to obtain a row full of `NA` values. #> Error: Can't use NA as row index in #> a tibble for assignment. with_df(df[NA_character_, ] <- df[1, ]) #> Error in `[<-.data.frame`(`*tmp*`, #> NA_character_, , value = #> structure(list(: missing values are #> not allowed in subscripted #> assignments of data frames with_tbl(tbl[NA_character_, ] <- tbl[1, ]) #> Error: Can't use NA as row index in #> a tibble for assignment.

Row and column subassignment

Definition of x[i, j] <- a

x[i, j] <- a is equivalent to x[i, ][j] <- a.[8]

Subassignment to x[i, j] is stricter for tibbles than for data frames. x[i, j] <- a can’t change the data type of existing columns.

with_df(df[2:3, 1] <- df[1:2, 2]) #> n c li #> 1 1 e 9 #> 2 e f 10, 11 #> 3 f g 12, 13, 14 #> 4 h text with_tbl(tbl[2:3, 1] <- tbl[1:2, 2]) #> Error: No common type for `value` #> and `x` . with_df(df[2:3, 2] <- df[1:2, 3]) #> Warning in `[<-.data.frame`(`*tmp*`, #> 2:3, 2, value = list(9, 10:11)): #> provided 2 variables to replace 1 #> variables #> n c li #> 1 1 e 9 #> 2 NA 9 10, 11 #> 3 3 9 12, 13, 14 #> 4 NA h text with_tbl(tbl[2:3, 2] <- tbl[1:2, 3]) #> Error: No common type for `value` #> and `x` . with_df(df[2:3, 3] <- df2[1:2, 1]) #> Warning in `[<-.data.frame`(`*tmp*`, #> 2:3, 3, value = structure(list(n = #> c(1L, : provided 3 variables to replace #> 1 variables #> n c li #> 1 1 e 9 #> 2 NA f 1 #> 3 3 g NA #> 4 NA h text with_tbl(tbl[2:3, 3] <- tbl2[1:2, 1]) #> Error: No common type for `value` #> n : integer #> c : character #> li: list #> >> and `x` . with_df2(df2[2:3, 1] <- df2[1:2, 2]) #> Warning in matrix(value, n, p): data #> length [8] is not a sub-multiple or #> multiple of the number of columns [3] #> tb.n tb.c tb.li m.1 m.2 m.3 m.4 #> 1 1 e 9 1 0 0 0 #> 2 1 0 0 0 1 0 0 #> 3 0 1 0 0 0 1 0 #> 4 NA h text 0 0 0 1 with_tbl2(tbl2[2:3, 1] <- tbl2[1:2, 2]) #> Error: No common type for `value` #> and `x` n : integer #> c : character #> li: list #> >>. with_tbl2(tbl2[2:3, 2] <- tbl[1:2, 1]) #> # A tibble: 4 x 2 #> tb$n $c $li m[,1] [,2] [,3] #> #> 1 1 e 2 NA f 3 3 g 4 NA h # … with 1 more variable: [,4]

For new columns, x[i, j] <- a fills the unassigned rows with NA.

with_df(df[2:3, "n"] <- 1) #> n c li #> 1 1 e 9 #> 2 1 f 10, 11 #> 3 1 g 12, 13, 14 #> 4 NA h text with_tbl(tbl[2:3, "n"] <- 1) #> # A tibble: 4 x 3 #> n c li #> #> 1 1 e #> 2 1 f #> 3 1 g #> 4 NA h with_tbl(tbl[2:3, "x"] <- 1) #> # A tibble: 4 x 4 #> n c li x #> #> 1 1 e NA #> 2 NA f 1 #> 3 3 g 1 #> 4 NA h NA with_df(df[2:3, "n"] <- NULL) #> Error in x[[jj]][iseq] <- vjj: #> replacement has length zero with_tbl(tbl[2:3, "n"] <- NULL) #> Error: `value` must be a vector, not #> NULL

Likewise, for new rows, x[i, j] <- a fills the unassigned columns with NA.

with_tbl(tbl[5, "n"] <- list(0L)) #> # A tibble: 5 x 3 #> n c li #> #> 1 1 e #> 2 NA f #> 3 3 g #> 4 NA h #> 5 0

Definition of x[[i, j]] <- a

i must be a numeric vector of length 1. x[[i, j]] <- a is equivalent to x[i, ][[j]] <- a.[9]

with_df(df[[1, 1]] <- 0) #> n c li #> 1 0 e 9 #> 2 NA f 10, 11 #> 3 3 g 12, 13, 14 #> 4 NA h text with_tbl(tbl[[1, 1]] <- 0) #> # A tibble: 4 x 3 #> n c li #> #> 1 0 e #> 2 NA f #> 3 3 g #> 4 NA h with_df(df[1, ][[1]] <- 0) #> n c li #> 1 0 e 9 #> 2 NA f 10, 11 #> 3 3 g 12, 13, 14 #> 4 NA h text with_tbl(tbl[1, ][[1]] <- 0) #> # A tibble: 4 x 3 #> n c li #> #> 1 0 e #> 2 NA f #> 3 3 g #> 4 NA h with_df(df[[1, 3]] <- list(NULL)) #> n c li #> 1 1 e NULL #> 2 NA f 10, 11 #> 3 3 g 12, 13, 14 #> 4 NA h text with_tbl(tbl[[1, 3]] <- list(NULL)) #> # A tibble: 4 x 3 #> n c li #> #> 1 1 e #> 2 NA f #> 3 3 g #> 4 NA h with_tbl(tbl[1, ][[3]] <- list(NULL)) #> # A tibble: 4 x 3 #> n c li #> #> 1 1 e #> 2 NA f #> 3 3 g #> 4 NA h with_df2(df2[[1, 1]] <- df[1, ]) #> Error in `[[<-.data.frame`(`*tmp*`, #> iseq, value = structure(list(n = 1L, #> : replacement has 1 row, data has 4 with_tbl2(tbl2[[1, 1]] <- tbl[1, ]) #> # A tibble: 4 x 2 #> tb$n $c $li m[,1] [,2] [,3] #> #> 1 1 e 2 NA f 3 3 g 4 NA h # … with 1 more variable: [,4] with_tbl2(tbl2[1, ][[1]] <- tbl[1, ]) #> # A tibble: 4 x 2 #> tb$n $c $li m[,1] [,2] [,3] #> #> 1 1 e 2 NA f 3 3 g 4 NA h # … with 1 more variable: [,4] with_df2(df2[[1, 2]] <- t(1:4)) #> Error in x[[jseq]][[iseq]] <- value: #> more elements supplied than there #> are to replace with_tbl2(tbl2[[1, 2]] <- t(1:4)) #> # A tibble: 4 x 2 #> tb$n $c $li m[,1] [,2] [,3] #> #> 1 1 e 2 NA f 3 3 g 4 NA h # … with 1 more variable: [,4] with_tbl2(tbl2[1, ][[2]] <- t(1:4)) #> # A tibble: 4 x 2 #> tb$n $c $li m[,1] [,2] [,3] #> #> 1 1 e 2 NA f 3 3 g 4 NA h # … with 1 more variable: [,4] df[[1:2, 1]] #> Error in col[[i, exact = exact]]: #> attempt to select more than one #> element in vectorIndex tbl[[1:2, 1]] #> Error: Must extract with a single #> index. #> [31mx[39m `i` has the wrong size #> 2. #> [34mℹ[39m This index must be size #> 1. with_df(df[[1:2, 1]] <- 0) #> Error in `[[<-.data.frame`(`*tmp*`, #> 1:2, 1, value = 0): only a single #> element should be replaced with_tbl(tbl[[1:2, 1]] <- 0) #> Error: Must extract with a single #> index. #> [31mx[39m `i` has the wrong size #> 2. #> [34mℹ[39m This index must be size #> 1.

NB: vec_size(a) must equal 1. Unlike x[i, ] <-, x[[i, ]] <- is not valid.

[1] x[j][[jj]] is equal to x[[ j[[jj]] ]], in particular x[j][[1]] is equal to x[[j]] for scalar numeric or integer j.

[2] Row subsetting x[i, ] is not defined in terms of x[[j]][i] because that definition does not generalise to matrix and data frame columns. For efficiency and backward compatibility, i is converted to an integer vector by vec_as_index(i, nrow(x)) first.

[3] x[,] is equivalent to x[] because x[, j] is equivalent to x[j].

[4] A more efficient implementation of x[i, j] would forward to x[j][i, ].

[5] Cell subsetting x[[i, j]] is not defined in terms of x[[j]][[i]] because that definition does not generalise to list, matrix and data frame columns. A more efficient implementation of x[[i, j]] would check that j is a scalar and forward to x[i, j][[1]].

[6] $ behaves almost completely symmetrically to [[ when comparing subsetting and subassignment.

[7] x[i, ] is symmetrically for subset and subassignment.

[8] x[i, j] is symmetrically for subsetting and subassignment. A more efficient implementation of x[i, j] <- a would forward to x[j][i, ] <- a.

[9] x[[i, j]] is symmetrically for subsetting and subassignment. An efficient implementation would check that i and j are scalar and forward to x[i, j][[1]] <- a.



krlmlr/tibble documentation built on Jan. 15, 2020, 7:56 a.m.