Skip to content

This vignette is an attempt to provide a comprehensive overview over all subassignment operations, highlighting where the tibble implementation differs from the data frame implementation.

library(tibble)
new_df <- function() {
  df <- data.frame(a = 1:4)
  df$b <- letters[5:8]
  df$cd <- 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()
#>   a b         cd
#> 1 1 e          9
#> 2 2 f     10, 11
#> 3 3 g 12, 13, 14
#> 4 4 h       text
new_tbl()
#> # A tibble: 4 × 3
#>       a b     cd       
#>   <int> <chr> <list>   
#> 1     1 e     <dbl [1]>
#> 2     2 f     <int [2]>
#> 3     3 g     <int [3]>
#> 4     4 h     <chr [1]>

In the following, if the results are identical (after converting to a data frame if necessary), only the tibble result is shown, as in the example below. This allows to spot differences easier.

new_tbl()
#> # A tibble: 4 × 3
#>       a b     cd       
#>   <int> <chr> <list>   
#> 1     1 e     <dbl [1]>
#> 2     2 f     <int [2]>
#> 3     3 g     <int [3]>
#> 4     4 h     <chr [1]>

For subassignment, we need a fresh copy of the data for each test. The with_*() functions allow for a more concise notation (with_tbl() omitted here for brevity):

with_df <- function(code, verbose = FALSE) {
  code <- rlang::enexpr(code)

  full_code <- rlang::quo({
    df <- new_df()
    !!code
    df
  })
  if (verbose) rlang::expr_print(rlang::quo_get_expr(full_code))
  rlang::eval_tidy(full_code)
}

This function takes an assignment expression and executes it on a fresh copy of the data. The first example prints what’s really executed, further examples omit this output.

with_df(df$a <- rev(df$a), verbose = TRUE)
#> {
#>   df <- new_df()
#>   df$a <- rev(df$a)
#>   df
#> }
#>   a b         cd
#> 1 4 e          9
#> 2 3 f     10, 11
#> 3 2 g 12, 13, 14
#> 4 1 h       text
with_tbl(tbl$a <- rev(tbl$a), verbose = TRUE)
#> {
#>   tbl <- new_tbl()
#>   tbl$a <- rev(tbl$a)
#>   tbl
#> }
#> # A tibble: 4 × 3
#>       a b     cd       
#>   <int> <chr> <list>   
#> 1     4 e     <dbl [1]>
#> 2     3 f     <int [2]>
#> 3     2 g     <int [3]>
#> 4     1 h     <chr [1]>

$<-

Scalars and full length

Assigning a scalar or a full-length vector to a column consistently overwrites existing data or appends a new column at the end. Partial matching doesn’t happen:

with_tbl(tbl$a <- 1)
#> # A tibble: 4 × 3
#>       a b     cd       
#>   <dbl> <chr> <list>   
#> 1     1 e     <dbl [1]>
#> 2     1 f     <int [2]>
#> 3     1 g     <int [3]>
#> 4     1 h     <chr [1]>
with_tbl(tbl$b <- 1)
#> # A tibble: 4 × 3
#>       a     b cd       
#>   <int> <dbl> <list>   
#> 1     1     1 <dbl [1]>
#> 2     2     1 <int [2]>
#> 3     3     1 <int [3]>
#> 4     4     1 <chr [1]>
with_tbl(tbl$c <- 1)
#> # A tibble: 4 × 4
#>       a b     cd            c
#>   <int> <chr> <list>    <dbl>
#> 1     1 e     <dbl [1]>     1
#> 2     2 f     <int [2]>     1
#> 3     3 g     <int [3]>     1
#> 4     4 h     <chr [1]>     1
with_tbl(tbl$cd <- 1)
#> # A tibble: 4 × 3
#>       a b        cd
#>   <int> <chr> <dbl>
#> 1     1 e         1
#> 2     2 f         1
#> 3     3 g         1
#> 4     4 h         1
with_tbl(tbl$a <- 4:1)
#> # A tibble: 4 × 3
#>       a b     cd       
#>   <int> <chr> <list>   
#> 1     4 e     <dbl [1]>
#> 2     3 f     <int [2]>
#> 3     2 g     <int [3]>
#> 4     1 h     <chr [1]>
with_tbl(tbl$b <- 4:1)
#> # A tibble: 4 × 3
#>       a     b cd       
#>   <int> <int> <list>   
#> 1     1     4 <dbl [1]>
#> 2     2     3 <int [2]>
#> 3     3     2 <int [3]>
#> 4     4     1 <chr [1]>
with_tbl(tbl$c <- 4:1)
#> # A tibble: 4 × 4
#>       a b     cd            c
#>   <int> <chr> <list>    <int>
#> 1     1 e     <dbl [1]>     4
#> 2     2 f     <int [2]>     3
#> 3     3 g     <int [3]>     2
#> 4     4 h     <chr [1]>     1
with_tbl(tbl$cd <- 4:1)
#> # A tibble: 4 × 3
#>       a b        cd
#>   <int> <chr> <int>
#> 1     1 e         4
#> 2     2 f         3
#> 3     3 g         2
#> 4     4 h         1

Recycling

Tibbles allow recycling only for vectors of length 1 or of the same length as the data.

with_df(df$a <- 1:2)
#>   a b         cd
#> 1 1 e          9
#> 2 2 f     10, 11
#> 3 1 g 12, 13, 14
#> 4 2 h       text
with_tbl(tbl$a <- 1:2)
#> Error in `$<-`:
#> ! Assigned data `1:2` must
#>   be compatible with existing data.
#>  Existing data has 4 rows.
#>  Assigned data has 2 rows.
#>  Only vectors of size 1 are
#>   recycled.
#> Caused by error in `vectbl_recycle_rhs_rows()` at tibble/R/subassign-backend.R:35:5:
#> ! Can't recycle input of size 2 to size 4.
with_df(df$a <- 1:3)
#> Error in `$<-.data.frame`(`*tmp*`, a, value = 1:3): replacement has 3 rows, data has 4
with_tbl(tbl$a <- 1:3)
#> Error in `$<-`:
#> ! Assigned data `1:3` must
#>   be compatible with existing data.
#>  Existing data has 4 rows.
#>  Assigned data has 3 rows.
#>  Only vectors of size 1 are
#>   recycled.
#> Caused by error in `vectbl_recycle_rhs_rows()` at tibble/R/subassign-backend.R:35:5:
#> ! Can't recycle input of size 3 to size 4.
with_df(df$a <- 1:5)
#> Error in `$<-.data.frame`(`*tmp*`, a, value = 1:5): replacement has 5 rows, data has 4
with_tbl(tbl$a <- 1:5)
#> Error in `$<-`:
#> ! Assigned data `1:5` must
#>   be compatible with existing data.
#>  Existing data has 4 rows.
#>  Assigned data has 5 rows.
#>  Only vectors of size 1 are
#>   recycled.
#> Caused by error in `vectbl_recycle_rhs_rows()` at tibble/R/subassign-backend.R:35:5:
#> ! Can't recycle input of size 5 to size 4.
with_df(df$c <- 1:2)
#>   a b         cd c
#> 1 1 e          9 1
#> 2 2 f     10, 11 2
#> 3 3 g 12, 13, 14 1
#> 4 4 h       text 2
with_tbl(tbl$c <- 1:2)
#> Error in `$<-`:
#> ! Assigned data `1:2` must
#>   be compatible with existing data.
#>  Existing data has 4 rows.
#>  Assigned data has 2 rows.
#>  Only vectors of size 1 are
#>   recycled.
#> Caused by error in `vectbl_recycle_rhs_rows()` at tibble/R/subassign-backend.R:35:5:
#> ! Can't recycle input of size 2 to size 4.

Subset assignment

Updating parts of a column extracted by $ is the responsibility of the column vector. Tibbles can’t control what happens after $ has returned.

with_tbl(tbl$a[1:2] <- 4:3)
#> # A tibble: 4 × 3
#>       a b     cd       
#>   <int> <chr> <list>   
#> 1     4 e     <dbl [1]>
#> 2     3 f     <int [2]>
#> 3     3 g     <int [3]>
#> 4     4 h     <chr [1]>
with_tbl(tbl$b[1:2] <- 4:3)
#> # A tibble: 4 × 3
#>       a b     cd       
#>   <int> <chr> <list>   
#> 1     1 4     <dbl [1]>
#> 2     2 3     <int [2]>
#> 3     3 g     <int [3]>
#> 4     4 h     <chr [1]>
with_df(df$c[1:2] <- 4:3)
#>   a b         cd          c
#> 1 1 e          9          4
#> 2 2 f     10, 11          3
#> 3 3 g 12, 13, 14 12, 13, 14
#> 4 4 h       text       text
with_tbl(tbl$c[1:2] <- 4:3)
#> Warning: Unknown or uninitialised
#> column: `c`.
#> Error in `$<-`:
#> ! Assigned data `<int>` must
#>   be compatible with existing data.
#>  Existing data has 4 rows.
#>  Assigned data has 2 rows.
#>  Only vectors of size 1 are
#>   recycled.
#> Caused by error in `vectbl_recycle_rhs_rows()` at tibble/R/subassign-backend.R:35:5:
#> ! Can't recycle input of size 2 to size 4.
with_tbl(tbl$cd[1:2] <- 4:3)
#> # A tibble: 4 × 3
#>       a b     cd       
#>   <int> <chr> <list>   
#> 1     1 e     <int [1]>
#> 2     2 f     <int [1]>
#> 3     3 g     <int [3]>
#> 4     4 h     <chr [1]>
with_df(df$a[1:3] <- 4:3)
#> Warning in df$a[1:3] <- 4:3: number
#> of items to replace is not a
#> multiple of replacement length
#>   a b         cd
#> 1 4 e          9
#> 2 3 f     10, 11
#> 3 4 g 12, 13, 14
#> 4 4 h       text
with_tbl(tbl$a[1:3] <- 4:3)
#> Warning in tbl$a[1:3] <- 4:3:
#> number of items to replace is not a
#> multiple of replacement length
#> # A tibble: 4 × 3
#>       a b     cd       
#>   <int> <chr> <list>   
#> 1     4 e     <dbl [1]>
#> 2     3 f     <int [2]>
#> 3     4 g     <int [3]>
#> 4     4 h     <chr [1]>
with_tbl(tbl$a[1:4] <- 4:3)
#> # A tibble: 4 × 3
#>       a b     cd       
#>   <int> <chr> <list>   
#> 1     4 e     <dbl [1]>
#> 2     3 f     <int [2]>
#> 3     4 g     <int [3]>
#> 4     3 h     <chr [1]>

For columns of the stricter "vctrs_vctr" class, this class implements the check, which then works identically for data frames and tibbles:

with_df({ df$v = vctrs::new_vctr(1:4); df$v[1:2] <- vctrs::new_vctr(4:3)})
#>   a b         cd v
#> 1 1 e          9 4
#> 2 2 f     10, 11 3
#> 3 3 g 12, 13, 14 3
#> 4 4 h       text 4
with_df({ df$v = vctrs::new_vctr(1:4); df$v[1:2] <- vctrs::new_vctr(letters[4:3])})
#> Error in `[<-`:
#> ! Can't convert `value` <vctrs_vctr> to <vctrs_vctr>.

[[<-

Scalars and full length

As with $ subsetting, columns are consistently overwritten, and partial matching doesn’t occur. Numeric indexing is supported, but tibbles don’t support creation of new numbered columns for a good reason.

with_tbl(tbl[["a"]] <- 1)
#> # A tibble: 4 × 3
#>       a b     cd       
#>   <dbl> <chr> <list>   
#> 1     1 e     <dbl [1]>
#> 2     1 f     <int [2]>
#> 3     1 g     <int [3]>
#> 4     1 h     <chr [1]>
with_tbl(tbl[["a"]] <- "x")
#> # A tibble: 4 × 3
#>   a     b     cd       
#>   <chr> <chr> <list>   
#> 1 x     e     <dbl [1]>
#> 2 x     f     <int [2]>
#> 3 x     g     <int [3]>
#> 4 x     h     <chr [1]>
with_tbl(tbl[["b"]] <- "x")
#> # A tibble: 4 × 3
#>       a b     cd       
#>   <int> <chr> <list>   
#> 1     1 x     <dbl [1]>
#> 2     2 x     <int [2]>
#> 3     3 x     <int [3]>
#> 4     4 x     <chr [1]>
with_tbl(tbl[["c"]] <- "x")
#> # A tibble: 4 × 4
#>       a b     cd        c    
#>   <int> <chr> <list>    <chr>
#> 1     1 e     <dbl [1]> x    
#> 2     2 f     <int [2]> x    
#> 3     3 g     <int [3]> x    
#> 4     4 h     <chr [1]> x
with_tbl(tbl[["cd"]] <- "x")
#> # A tibble: 4 × 3
#>       a b     cd   
#>   <int> <chr> <chr>
#> 1     1 e     x    
#> 2     2 f     x    
#> 3     3 g     x    
#> 4     4 h     x
with_tbl(tbl[[1]] <- "x")
#> # A tibble: 4 × 3
#>   a     b     cd       
#>   <chr> <chr> <list>   
#> 1 x     e     <dbl [1]>
#> 2 x     f     <int [2]>
#> 3 x     g     <int [3]>
#> 4 x     h     <chr [1]>
with_tbl(tbl[[2]] <- "x")
#> # A tibble: 4 × 3
#>       a b     cd       
#>   <int> <chr> <list>   
#> 1     1 x     <dbl [1]>
#> 2     2 x     <int [2]>
#> 3     3 x     <int [3]>
#> 4     4 x     <chr [1]>
with_df(df[[4]] <- "x")
#>   a b         cd V4
#> 1 1 e          9  x
#> 2 2 f     10, 11  x
#> 3 3 g 12, 13, 14  x
#> 4 4 h       text  x
with_tbl(tbl[[4]] <- "x")
#> # A tibble: 4 × 4
#>       a b     cd        ...4 
#>   <int> <chr> <list>    <chr>
#> 1     1 e     <dbl [1]> x    
#> 2     2 f     <int [2]> x    
#> 3     3 g     <int [3]> x    
#> 4     4 h     <chr [1]> x
with_df(df[[5]] <- "x")
#> 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
#>   a b         cd      V5
#> 1 1 e          9 NULL  x
#> 2 2 f     10, 11 <NA>  x
#> 3 3 g 12, 13, 14 <NA>  x
#> 4 4 h       text <NA>  x
with_tbl(tbl[[5]] <- "x")
#> Error in `[[<-`:
#> ! Can't assign to columns beyond the end with non-consecutive locations.
#>  Input has size 3.
#>  Subscript `5` contains non-consecutive location 5.
with_tbl(tbl[["a"]] <- 4:1)
#> # A tibble: 4 × 3
#>       a b     cd       
#>   <int> <chr> <list>   
#> 1     4 e     <dbl [1]>
#> 2     3 f     <int [2]>
#> 3     2 g     <int [3]>
#> 4     1 h     <chr [1]>
with_tbl(tbl[["a"]] <- letters[4:1])
#> # A tibble: 4 × 3
#>   a     b     cd       
#>   <chr> <chr> <list>   
#> 1 d     e     <dbl [1]>
#> 2 c     f     <int [2]>
#> 3 b     g     <int [3]>
#> 4 a     h     <chr [1]>
with_tbl(tbl[["b"]] <- letters[4:1])
#> # A tibble: 4 × 3
#>       a b     cd       
#>   <int> <chr> <list>   
#> 1     1 d     <dbl [1]>
#> 2     2 c     <int [2]>
#> 3     3 b     <int [3]>
#> 4     4 a     <chr [1]>
with_tbl(tbl[["c"]] <- letters[4:1])
#> # A tibble: 4 × 4
#>       a b     cd        c    
#>   <int> <chr> <list>    <chr>
#> 1     1 e     <dbl [1]> d    
#> 2     2 f     <int [2]> c    
#> 3     3 g     <int [3]> b    
#> 4     4 h     <chr [1]> a
with_tbl(tbl[["cd"]] <- letters[4:1])
#> # A tibble: 4 × 3
#>       a b     cd   
#>   <int> <chr> <chr>
#> 1     1 e     d    
#> 2     2 f     c    
#> 3     3 g     b    
#> 4     4 h     a

Cells

Tibbles are stricter when updating single cells, the value must be coercible to the existing contents. Updating a list column requires the contents to be wrapped in a list, consistently with [[ subsetting which returns a list if a cell in a list column is accessed:

with_df(df[[2, "a"]] <- 1)
#>   a b         cd
#> 1 1 e          9
#> 2 1 f     10, 11
#> 3 3 g 12, 13, 14
#> 4 4 h       text
with_tbl(tbl[[2, "a"]] <- 1)
#> # A tibble: 4 × 3
#>       a b     cd       
#>   <int> <chr> <list>   
#> 1     1 e     <dbl [1]>
#> 2     1 f     <int [2]>
#> 3     3 g     <int [3]>
#> 4     4 h     <chr [1]>
with_df(df[[2, "a"]] <- 1.5)
#>     a b         cd
#> 1 1.0 e          9
#> 2 1.5 f     10, 11
#> 3 3.0 g 12, 13, 14
#> 4 4.0 h       text
with_tbl(tbl[[2, "a"]] <- 1.5)
#> Error in `[[<-`:
#> ! Assigned data `1.5` must
#>   be compatible with existing data.
#>  Error occurred for column `a`.
#> Caused by error in `vec_assign()` at tibble/R/subassign-backend.R:193:3:
#> ! Can't convert from <double> to <integer> due to loss of precision.
#>  Locations: 1
with_df(df[[2, "a"]] <- "x")
#>   a b         cd
#> 1 1 e          9
#> 2 x f     10, 11
#> 3 3 g 12, 13, 14
#> 4 4 h       text
with_tbl(tbl[[2, "a"]] <- "x")
#> Error in `[[<-`:
#> ! Assigned data `"x"` must
#>   be compatible with existing data.
#>  Error occurred for column `a`.
#> Caused by error in `vec_assign()` at tibble/R/subassign-backend.R:193:3:
#> ! Can't convert <character> to <integer>.
with_tbl(tbl[[2, "b"]] <- "x")
#> # A tibble: 4 × 3
#>       a b     cd       
#>   <int> <chr> <list>   
#> 1     1 e     <dbl [1]>
#> 2     2 x     <int [2]>
#> 3     3 g     <int [3]>
#> 4     4 h     <chr [1]>
with_df(df[[2, 1]] <- "x")
#>   a b         cd
#> 1 1 e          9
#> 2 x f     10, 11
#> 3 3 g 12, 13, 14
#> 4 4 h       text
with_tbl(tbl[[2, 1]] <- "x")
#> Error in `[[<-`:
#> ! Assigned data `"x"` must
#>   be compatible with existing data.
#>  Error occurred for column `a`.
#> Caused by error in `vec_assign()` at tibble/R/subassign-backend.R:193:3:
#> ! Can't convert <character> to <integer>.
with_tbl(tbl[[2, 2]] <- "x")
#> # A tibble: 4 × 3
#>       a b     cd       
#>   <int> <chr> <list>   
#> 1     1 e     <dbl [1]>
#> 2     2 x     <int [2]>
#> 3     3 g     <int [3]>
#> 4     4 h     <chr [1]>
with_df(df[[2, "cd"]] <- "x")
#>   a b         cd
#> 1 1 e          9
#> 2 2 f          x
#> 3 3 g 12, 13, 14
#> 4 4 h       text
with_tbl(tbl[[2, "cd"]] <- "x")
#> Error in `[[<-`:
#> ! Assigned data `"x"` must
#>   be compatible with existing data.
#>  Error occurred for column `cd`.
#> Caused by error in `vec_assign()` at tibble/R/subassign-backend.R:193:3:
#> ! Can't convert <character> to <list>.
with_df(df[[2, "cd"]] <- list("x"))
#>   a b         cd
#> 1 1 e          9
#> 2 2 f          x
#> 3 3 g 12, 13, 14
#> 4 4 h       text
with_tbl(tbl[[2, "cd"]] <- list("x"))
#> # A tibble: 4 × 3
#>       a b     cd       
#>   <int> <chr> <list>   
#> 1     1 e     <dbl [1]>
#> 2     2 f     <chr [1]>
#> 3     3 g     <int [3]>
#> 4     4 h     <chr [1]>
with_df(df[[2, "c"]] <- "x")
#> Error in `[[<-.data.frame`(`*tmp*`, 2, "c", value = "x"): replacing element in non-existent column: c
with_tbl(tbl[[2, "c"]] <- "x")
#> # A tibble: 4 × 4
#>       a b     cd        c    
#>   <int> <chr> <list>    <chr>
#> 1     1 e     <dbl [1]> NA   
#> 2     2 f     <int [2]> x    
#> 3     3 g     <int [3]> NA   
#> 4     4 h     <chr [1]> NA
with_df(df[[1:2, "cd"]] <- "x")
#> Error in `[[<-.data.frame`(`*tmp*`, 1:2, "cd", value = "x"): only a single element should be replaced
with_tbl(tbl[[1:2, "cd"]] <- "x")
#> Error in `[[<-`:
#> ! Can't assign row with `1:2`.
#>  Subscript `1:2` must be size 1, not 2.
with_df(df[[1:2, "c"]] <- "x")
#> Error in `[[<-.data.frame`(`*tmp*`, 1:2, "c", value = "x"): replacing element in non-existent column: c
with_tbl(tbl[[1:2, "c"]] <- "x")
#> Error in `[[<-`:
#> ! Can't assign row with `1:2`.
#>  Subscript `1:2` must be size 1, not 2.
with_df(df[[2, c("cd", "d")]] <- "x")
#> Error in `[[<-.data.frame`(`*tmp*`, 2, c("cd", "d"), value = "x"): replacing element in non-existent column: d
with_tbl(tbl[[2, c("cd", "d")]] <- "x")
#> Error in `[[<-`:
#> ! Can't assign column with `c("cd", "d")`.
#>  Subscript `c("cd", "d")` must be size 1, not 2.

[<-

Scalars

with_df(df[2, "a"] <- 1)
#>   a b         cd
#> 1 1 e          9
#> 2 1 f     10, 11
#> 3 3 g 12, 13, 14
#> 4 4 h       text
with_tbl(tbl[2, "a"] <- 1)
#> # A tibble: 4 × 3
#>       a b     cd       
#>   <int> <chr> <list>   
#> 1     1 e     <dbl [1]>
#> 2     1 f     <int [2]>
#> 3     3 g     <int [3]>
#> 4     4 h     <chr [1]>
with_df(df[2, "a"] <- 1.5)
#>     a b         cd
#> 1 1.0 e          9
#> 2 1.5 f     10, 11
#> 3 3.0 g 12, 13, 14
#> 4 4.0 h       text
with_tbl(tbl[2, "a"] <- 1.5)
#> Error in `[<-`:
#> ! Assigned data `1.5` must
#>   be compatible with existing data.
#>  Error occurred for column `a`.
#> Caused by error in `vec_assign()` at tibble/R/subassign-backend.R:193:3:
#> ! Can't convert from <double> to <integer> due to loss of precision.
#>  Locations: 1
with_df(df[2, "a"] <- "x")
#>   a b         cd
#> 1 1 e          9
#> 2 x f     10, 11
#> 3 3 g 12, 13, 14
#> 4 4 h       text
with_tbl(tbl[2, "a"] <- "x")
#> Error in `[<-`:
#> ! Assigned data `"x"` must
#>   be compatible with existing data.
#>  Error occurred for column `a`.
#> Caused by error in `vec_assign()` at tibble/R/subassign-backend.R:193:3:
#> ! Can't convert <character> to <integer>.
with_tbl(tbl[2, "b"] <- "x")
#> # A tibble: 4 × 3
#>       a b     cd       
#>   <int> <chr> <list>   
#> 1     1 e     <dbl [1]>
#> 2     2 x     <int [2]>
#> 3     3 g     <int [3]>
#> 4     4 h     <chr [1]>
with_df(df[2, "cd"] <- "x")
#>   a b         cd
#> 1 1 e          9
#> 2 2 f          x
#> 3 3 g 12, 13, 14
#> 4 4 h       text
with_tbl(tbl[2, "cd"] <- "x")
#> Error in `[<-`:
#> ! Assigned data `"x"` must
#>   be compatible with existing data.
#>  Error occurred for column `cd`.
#> Caused by error in `vec_assign()` at tibble/R/subassign-backend.R:193:3:
#> ! Can't convert <character> to <list>.
with_df(df[2, "cd"] <- list("x"))
#>   a b         cd
#> 1 1 e          9
#> 2 2 f          x
#> 3 3 g 12, 13, 14
#> 4 4 h       text
with_tbl(tbl[2, "cd"] <- list("x"))
#> Error in `[<-`:
#> ! Assigned data `list("x")`
#>   must be compatible with existing
#>   data.
#>  Error occurred for column `cd`.
#> Caused by error in `vec_assign()` at tibble/R/subassign-backend.R:193:3:
#> ! Can't convert <character> to <list>.
with_tbl(tbl[2, "c"] <- "x")
#> # A tibble: 4 × 4
#>       a b     cd        c    
#>   <int> <chr> <list>    <chr>
#> 1     1 e     <dbl [1]> NA   
#> 2     2 f     <int [2]> x    
#> 3     3 g     <int [3]> NA   
#> 4     4 h     <chr [1]> NA
with_df(df[2, 1] <- "x")
#>   a b         cd
#> 1 1 e          9
#> 2 x f     10, 11
#> 3 3 g 12, 13, 14
#> 4 4 h       text
with_tbl(tbl[2, 1] <- "x")
#> Error in `[<-`:
#> ! Assigned data `"x"` must
#>   be compatible with existing data.
#>  Error occurred for column `a`.
#> Caused by error in `vec_assign()` at tibble/R/subassign-backend.R:193:3:
#> ! Can't convert <character> to <integer>.
with_tbl(tbl[2, 2] <- "x")
#> # A tibble: 4 × 3
#>       a b     cd       
#>   <int> <chr> <list>   
#> 1     1 e     <dbl [1]>
#> 2     2 x     <int [2]>
#> 3     3 g     <int [3]>
#> 4     4 h     <chr [1]>
with_df(df[2, 3] <- "x")
#>   a b         cd
#> 1 1 e          9
#> 2 2 f          x
#> 3 3 g 12, 13, 14
#> 4 4 h       text
with_tbl(tbl[2, 3] <- "x")
#> Error in `[<-`:
#> ! Assigned data `"x"` must
#>   be compatible with existing data.
#>  Error occurred for column `cd`.
#> Caused by error in `vec_assign()` at tibble/R/subassign-backend.R:193:3:
#> ! Can't convert <character> to <list>.
with_df(df[2, 4] <- "x")
#>   a b         cd   V4
#> 1 1 e          9 <NA>
#> 2 2 f     10, 11    x
#> 3 3 g 12, 13, 14 <NA>
#> 4 4 h       text <NA>
with_tbl(tbl[2, 4] <- "x")
#> # A tibble: 4 × 4
#>       a b     cd        ...4 
#>   <int> <chr> <list>    <chr>
#> 1     1 e     <dbl [1]> NA   
#> 2     2 f     <int [2]> x    
#> 3     3 g     <int [3]> NA   
#> 4     4 h     <chr [1]> NA

Full length columns

with_tbl(tbl[, "a"] <- 4:1)
#> # A tibble: 4 × 3
#>       a b     cd       
#>   <int> <chr> <list>   
#> 1     4 e     <dbl [1]>
#> 2     3 f     <int [2]>
#> 3     2 g     <int [3]>
#> 4     1 h     <chr [1]>
with_tbl(tbl[, "b"] <- 4:1)
#> # A tibble: 4 × 3
#>       a     b cd       
#>   <int> <int> <list>   
#> 1     1     4 <dbl [1]>
#> 2     2     3 <int [2]>
#> 3     3     2 <int [3]>
#> 4     4     1 <chr [1]>
with_tbl(tbl[, "c"] <- 4:1)
#> # A tibble: 4 × 4
#>       a b     cd            c
#>   <int> <chr> <list>    <int>
#> 1     1 e     <dbl [1]>     4
#> 2     2 f     <int [2]>     3
#> 3     3 g     <int [3]>     2
#> 4     4 h     <chr [1]>     1
with_tbl(tbl[, "cd"] <- 4:1)
#> # A tibble: 4 × 3
#>       a b        cd
#>   <int> <chr> <int>
#> 1     1 e         4
#> 2     2 f         3
#> 3     3 g         2
#> 4     4 h         1
with_tbl(tbl[, 1] <- 4:1)
#> # A tibble: 4 × 3
#>       a b     cd       
#>   <int> <chr> <list>   
#> 1     4 e     <dbl [1]>
#> 2     3 f     <int [2]>
#> 3     2 g     <int [3]>
#> 4     1 h     <chr [1]>
with_tbl(tbl[, 2] <- 4:1)
#> # A tibble: 4 × 3
#>       a     b cd       
#>   <int> <int> <list>   
#> 1     1     4 <dbl [1]>
#> 2     2     3 <int [2]>
#> 3     3     2 <int [3]>
#> 4     4     1 <chr [1]>
with_tbl(tbl[, 3] <- 4:1)
#> # A tibble: 4 × 3
#>       a b        cd
#>   <int> <chr> <int>
#> 1     1 e         4
#> 2     2 f         3
#> 3     3 g         2
#> 4     4 h         1
with_df(df[, 4] <- 4:1)
#>   a b         cd V4
#> 1 1 e          9  4
#> 2 2 f     10, 11  3
#> 3 3 g 12, 13, 14  2
#> 4 4 h       text  1
with_tbl(tbl[, 4] <- 4:1)
#> # A tibble: 4 × 4
#>       a b     cd         ...4
#>   <int> <chr> <list>    <int>
#> 1     1 e     <dbl [1]>     4
#> 2     2 f     <int [2]>     3
#> 3     3 g     <int [3]>     2
#> 4     4 h     <chr [1]>     1
with_tbl(tbl[, "a"] <- 1)
#> # A tibble: 4 × 3
#>       a b     cd       
#>   <dbl> <chr> <list>   
#> 1     1 e     <dbl [1]>
#> 2     1 f     <int [2]>
#> 3     1 g     <int [3]>
#> 4     1 h     <chr [1]>
with_tbl(tbl[, "b"] <- 1)
#> # A tibble: 4 × 3
#>       a     b cd       
#>   <int> <dbl> <list>   
#> 1     1     1 <dbl [1]>
#> 2     2     1 <int [2]>
#> 3     3     1 <int [3]>
#> 4     4     1 <chr [1]>
with_tbl(tbl[, "c"] <- 1)
#> # A tibble: 4 × 4
#>       a b     cd            c
#>   <int> <chr> <list>    <dbl>
#> 1     1 e     <dbl [1]>     1
#> 2     2 f     <int [2]>     1
#> 3     3 g     <int [3]>     1
#> 4     4 h     <chr [1]>     1
with_tbl(tbl[, "cd"] <- 1)
#> # A tibble: 4 × 3
#>       a b        cd
#>   <int> <chr> <dbl>
#> 1     1 e         1
#> 2     2 f         1
#> 3     3 g         1
#> 4     4 h         1
with_tbl(tbl[, 1] <- 1)
#> # A tibble: 4 × 3
#>       a b     cd       
#>   <dbl> <chr> <list>   
#> 1     1 e     <dbl [1]>
#> 2     1 f     <int [2]>
#> 3     1 g     <int [3]>
#> 4     1 h     <chr [1]>
with_tbl(tbl[, 2] <- 1)
#> # A tibble: 4 × 3
#>       a     b cd       
#>   <int> <dbl> <list>   
#> 1     1     1 <dbl [1]>
#> 2     2     1 <int [2]>
#> 3     3     1 <int [3]>
#> 4     4     1 <chr [1]>
with_tbl(tbl[, 3] <- 1)
#> # A tibble: 4 × 3
#>       a b        cd
#>   <int> <chr> <dbl>
#> 1     1 e         1
#> 2     2 f         1
#> 3     3 g         1
#> 4     4 h         1
with_df(df[, 4] <- 1)
#>   a b         cd V4
#> 1 1 e          9  1
#> 2 2 f     10, 11  1
#> 3 3 g 12, 13, 14  1
#> 4 4 h       text  1
with_tbl(tbl[, 4] <- 1)
#> # A tibble: 4 × 4
#>       a b     cd         ...4
#>   <int> <chr> <list>    <dbl>
#> 1     1 e     <dbl [1]>     1
#> 2     2 f     <int [2]>     1
#> 3     3 g     <int [3]>     1
#> 4     4 h     <chr [1]>     1

Multiple full length columns

with_tbl(tbl[, c("a", "b")] <- 4:1)
#> # A tibble: 4 × 3
#>       a     b cd       
#>   <int> <int> <list>   
#> 1     4     4 <dbl [1]>
#> 2     3     3 <int [2]>
#> 3     2     2 <int [3]>
#> 4     1     1 <chr [1]>
with_tbl(tbl[, c("a", "b")] <- 1)
#> # A tibble: 4 × 3
#>       a     b cd       
#>   <dbl> <dbl> <list>   
#> 1     1     1 <dbl [1]>
#> 2     1     1 <int [2]>
#> 3     1     1 <int [3]>
#> 4     1     1 <chr [1]>
with_tbl(tbl[, c("a", "b")] <- data.frame(a = 4:1, b = letters[4:1]))
#> # A tibble: 4 × 3
#>       a b     cd       
#>   <int> <chr> <list>   
#> 1     4 d     <dbl [1]>
#> 2     3 c     <int [2]>
#> 3     2 b     <int [3]>
#> 4     1 a     <chr [1]>
with_tbl(tbl[, c("a", "b")] <- data.frame(b = 4:1, a = letters[4:1]))
#> # A tibble: 4 × 3
#>       a b     cd       
#>   <int> <chr> <list>   
#> 1     4 d     <dbl [1]>
#> 2     3 c     <int [2]>
#> 3     2 b     <int [3]>
#> 4     1 a     <chr [1]>
with_tbl(tbl[, c("a", "b")] <- data.frame(c = 4:1, d = letters[4:1]))
#> # A tibble: 4 × 3
#>       a b     cd       
#>   <int> <chr> <list>   
#> 1     4 d     <dbl [1]>
#> 2     3 c     <int [2]>
#> 3     2 b     <int [3]>
#> 4     1 a     <chr [1]>
with_tbl(tbl[, c("a", "b")] <- data.frame(a = 4:1))
#> # A tibble: 4 × 3
#>       a     b cd       
#>   <int> <int> <list>   
#> 1     4     4 <dbl [1]>
#> 2     3     3 <int [2]>
#> 3     2     2 <int [3]>
#> 4     1     1 <chr [1]>
with_df(df[, c("a", "b")] <- data.frame(a = 4:1, b = letters[4:1], c = 1:4))
#> Warning in
#> `[<-.data.frame`(`*tmp*`, , c("a",
#> "b"), value = structure(list(:
#> provided 3 variables to replace 2
#> variables
#>   a b         cd
#> 1 4 d          9
#> 2 3 c     10, 11
#> 3 2 b 12, 13, 14
#> 4 1 a       text
with_tbl(tbl[, c("a", "b")] <- data.frame(a = 4:1, b = letters[4:1], c = 1:4))
#> Error in `[<-`:
#> ! Can't recycle input of size 3 to size 2.
with_tbl(tbl[, c("a", "b")] <- data.frame(4:1, 1))
#> # A tibble: 4 × 3
#>       a     b cd       
#>   <int> <dbl> <list>   
#> 1     4     1 <dbl [1]>
#> 2     3     1 <int [2]>
#> 3     2     1 <int [3]>
#> 4     1     1 <chr [1]>
with_df(df[, c("a", "b", "c")] <- data.frame(4:1, letters[4:1]))
#>   a b         cd c
#> 1 4 d          9 4
#> 2 3 c     10, 11 3
#> 3 2 b 12, 13, 14 2
#> 4 1 a       text 1
with_tbl(tbl[, c("a", "b", "c")] <- data.frame(4:1, letters[4:1]))
#> Error in `[<-`:
#> ! Can't recycle input of size 2 to size 3.
with_df(df[, c("a", "b", "cd")] <- data.frame(4:1, letters[4:1]))
#>   a b cd
#> 1 4 d  4
#> 2 3 c  3
#> 3 2 b  2
#> 4 1 a  1
with_tbl(tbl[, c("a", "b", "cd")] <- data.frame(4:1, letters[4:1]))
#> Error in `[<-`:
#> ! Can't recycle input of size 2 to size 3.

Full length rows

with_df(df[2, ] <- 1)
#>   a b         cd
#> 1 1 e          9
#> 2 1 1          1
#> 3 3 g 12, 13, 14
#> 4 4 h       text
with_tbl(tbl[2, ] <- 1)
#> Error in `[<-`:
#> ! Assigned data `1` must be
#>   compatible with existing data.
#>  Error occurred for column `b`.
#> Caused by error in `vec_assign()` at tibble/R/subassign-backend.R:193:3:
#> ! Can't convert <double> to <character>.
with_df(df[2, ] <- "x")
#>   a b         cd
#> 1 1 e          9
#> 2 x x          x
#> 3 3 g 12, 13, 14
#> 4 4 h       text
with_tbl(tbl[2, ] <- "x")
#> Error in `[<-`:
#> ! Assigned data `"x"` must
#>   be compatible with existing data.
#>  Error occurred for column `a`.
#> Caused by error in `vec_assign()` at tibble/R/subassign-backend.R:193:3:
#> ! Can't convert <character> to <integer>.
with_df(df[2, ] <- tibble(a = 1, b = "x"))
#>   a b         cd
#> 1 1 e          9
#> 2 1 x          1
#> 3 3 g 12, 13, 14
#> 4 4 h       text
with_tbl(tbl[2, ] <- tibble(a = 1, b = "x"))
#> Error in `[<-`:
#> ! Can't recycle input of size 2 to size 3.
with_df(df[2, ] <- tibble(a = 1, b = "x", c = list("y")))
#>   a b         cd
#> 1 1 e          9
#> 2 1 x          y
#> 3 3 g 12, 13, 14
#> 4 4 h       text
with_tbl(tbl[2, ] <- tibble(a = 1, b = "x", c = list("y")))
#> # A tibble: 4 × 3
#>       a b     cd       
#>   <int> <chr> <list>   
#> 1     1 e     <dbl [1]>
#> 2     1 x     <chr [1]>
#> 3     3 g     <int [3]>
#> 4     4 h     <chr [1]>
with_df(df[2, ] <- tibble(a = 1, b = "x", c = list("y"), d = "z"))
#> Warning in
#> `[<-.data.frame`(`*tmp*`, 2, ,
#> value = structure(list(a = 1, :
#> provided 4 variables to replace 3
#> variables
#>   a b         cd
#> 1 1 e          9
#> 2 1 x          y
#> 3 3 g 12, 13, 14
#> 4 4 h       text
with_tbl(tbl[2, ] <- tibble(a = 1, b = "x", c = list("y"), d = "z"))
#> Error in `[<-`:
#> ! Can't recycle input of size 4 to size 3.
with_df(df[0, ] <- tibble(a = 1, b = "x", c = list("y")))
#>   a b         cd
#> 1 1 e          9
#> 2 2 f     10, 11
#> 3 3 g 12, 13, 14
#> 4 4 h       text
with_tbl(tbl[0, ] <- tibble(a = 1, b = "x", c = list("y")))
#> Error in `[<-`:
#> ! Can't assign rows with `0`.
#>  Subscript `0` can't contain `0` values.
#>  It has a `0` value at location 1.
with_df(df[5, ] <- tibble(a = 1, b = "x", c = list("y")))
#>   a b         cd
#> 1 1 e          9
#> 2 2 f     10, 11
#> 3 3 g 12, 13, 14
#> 4 4 h       text
#> 5 1 x          y
with_tbl(tbl[5, ] <- tibble(a = 1, b = "x", c = list("y")))
#> # A tibble: 5 × 3
#>       a b     cd       
#>   <int> <chr> <list>   
#> 1     1 e     <dbl [1]>
#> 2     2 f     <int [2]>
#> 3     3 g     <int [3]>
#> 4     4 h     <chr [1]>
#> 5     1 x     <chr [1]>

Multiple full length rows

with_df(df[2:3, ] <- 1)
#>   a b   cd
#> 1 1 e    9
#> 2 1 1    1
#> 3 1 1    1
#> 4 4 h text
with_tbl(tbl[2:3, ] <- 1)
#> Error in `[<-`:
#> ! Assigned data `1` must be
#>   compatible with existing data.
#>  Error occurred for column `b`.
#> Caused by error in `vec_assign()` at tibble/R/subassign-backend.R:193:3:
#> ! Can't convert <double> to <character>.
with_df(df[2:3, ] <- 1:2)
#>   a b   cd
#> 1 1 e    9
#> 2 1 1    1
#> 3 2 2    2
#> 4 4 h text
with_tbl(tbl[2:3, ] <- 1:2)
#> Error in `[<-`:
#> ! Assigned data `1:2` must
#>   be compatible with existing data.
#>  Error occurred for column `b`.
#> Caused by error in `vec_assign()` at tibble/R/subassign-backend.R:193:3:
#> ! Can't convert <integer> to <character>.
with_df(df[2:3, ] <- c("x", "y"))
#>   a b   cd
#> 1 1 e    9
#> 2 x x    x
#> 3 y y    y
#> 4 4 h text
with_tbl(tbl[2:3, ] <- c("x", "y"))
#> Error in `[<-`:
#> ! Assigned data `c("x",
#>   "y")` must be compatible with
#>   existing data.
#>  Error occurred for column `a`.
#> Caused by error in `vec_assign()` at tibble/R/subassign-backend.R:193:3:
#> ! Can't convert <character> to <integer>.
with_df(df[2:3, ] <- tibble(a = 1:2, b = c("x", "y")))
#>   a b   cd
#> 1 1 e    9
#> 2 1 x    1
#> 3 2 y    2
#> 4 4 h text
with_tbl(tbl[2:3, ] <- tibble(a = 1:2, b = c("x", "y")))
#> Error in `[<-`:
#> ! Can't recycle input of size 2 to size 3.
with_df(df[2:3, ] <- tibble(a = 1, b = "x", c = list("y")))
#>   a b   cd
#> 1 1 e    9
#> 2 1 x    y
#> 3 1 x    y
#> 4 4 h text
with_tbl(tbl[2:3, ] <- tibble(a = 1, b = "x", c = list("y")))
#> # A tibble: 4 × 3
#>       a b     cd       
#>   <int> <chr> <list>   
#> 1     1 e     <dbl [1]>
#> 2     1 x     <chr [1]>
#> 3     1 x     <chr [1]>
#> 4     4 h     <chr [1]>
with_tbl(tbl[2:3, ] <- tibble(a = 1:2, b = "x", c = list("y")))
#> # A tibble: 4 × 3
#>       a b     cd       
#>   <int> <chr> <list>   
#> 1     1 e     <dbl [1]>
#> 2     1 x     <chr [1]>
#> 3     2 x     <chr [1]>
#> 4     4 h     <chr [1]>
with_df(df[2:3, ] <- tibble(a = 1, b = "x", c = list("y"), d = "z"))
#> Warning in
#> `[<-.data.frame`(`*tmp*`, 2:3, ,
#> value = structure(list(a = 1, :
#> provided 4 variables to replace 3
#> variables
#>   a b   cd
#> 1 1 e    9
#> 2 1 x    y
#> 3 1 x    y
#> 4 4 h text
with_tbl(tbl[2:3, ] <- tibble(a = 1, b = "x", c = list("y"), d = "z"))
#> Error in `[<-`:
#> ! Can't recycle input of size 4 to size 3.
with_tbl(tbl[-(1:2), ] <- tibble(a = 1:2, b = "x", c = list("y")))
#> # A tibble: 4 × 3
#>       a b     cd       
#>   <int> <chr> <list>   
#> 1     1 e     <dbl [1]>
#> 2     2 f     <int [2]>
#> 3     1 x     <chr [1]>
#> 4     2 x     <chr [1]>
with_df(df[0:1, ] <- tibble(a = 1:2, b = "x", c = list("y")))
#> Warning in
#> `[<-.data.frame`(`*tmp*`, 0:1, ,
#> value = structure(list(a = 1:2, :
#> replacement element 1 has 2 rows to
#> replace 1 rows
#> Warning in
#> `[<-.data.frame`(`*tmp*`, 0:1, ,
#> value = structure(list(a = 1:2, :
#> replacement element 2 has 2 rows to
#> replace 1 rows
#> Warning in
#> `[<-.data.frame`(`*tmp*`, 0:1, ,
#> value = structure(list(a = 1:2, :
#> replacement element 3 has 2 rows to
#> replace 1 rows
#>   a b         cd
#> 1 1 x          y
#> 2 2 f     10, 11
#> 3 3 g 12, 13, 14
#> 4 4 h       text
with_tbl(tbl[0:1, ] <- tibble(a = 1:2, b = "x", c = list("y")))
#> Error in `[<-`:
#> ! Can't assign rows with `0:1`.
#>  Subscript `0:1` can't contain `0` values.
#>  It has a `0` value at location 1.
with_tbl(tbl[4:5, ] <- tibble(a = 1:2, b = "x", c = list("y")))
#> # A tibble: 5 × 3
#>       a b     cd       
#>   <int> <chr> <list>   
#> 1     1 e     <dbl [1]>
#> 2     2 f     <int [2]>
#> 3     3 g     <int [3]>
#> 4     1 x     <chr [1]>
#> 5     2 x     <chr [1]>

Unspecified

with_tbl(tbl[] <- 1)
#> # A tibble: 4 × 3
#>       a     b    cd
#>   <dbl> <dbl> <dbl>
#> 1     1     1     1
#> 2     1     1     1
#> 3     1     1     1
#> 4     1     1     1
with_tbl(tbl[] <- 4:1)
#> # A tibble: 4 × 3
#>       a     b    cd
#>   <int> <int> <int>
#> 1     4     4     4
#> 2     3     3     3
#> 3     2     2     2
#> 4     1     1     1
with_df(df[] <- 3:1)
#>   a b cd
#> 1 3 2  1
#> 2 2 1  3
#> 3 1 3  2
#> 4 3 2  1
with_tbl(tbl[] <- 3:1)
#> Error in `[<-`:
#> ! Assigned data `3:1` must
#>   be compatible with existing data.
#>  Existing data has 4 rows.
#>  Assigned data has 3 rows.
#>  Only vectors of size 1 are
#>   recycled.
#> Caused by error in `vectbl_recycle_rhs_rows()` at tibble/R/subassign-backend.R:35:5:
#> ! Can't recycle input of size 3 to size 4.
with_df(df[] <- 5:1)
#> Error in `[<-.data.frame`(`*tmp*`, , value = 5:1): replacement has 5 items, need 12
with_tbl(tbl[] <- 5:1)
#> Error in `[<-`:
#> ! Assigned data `5:1` must
#>   be compatible with existing data.
#>  Existing data has 4 rows.
#>  Assigned data has 5 rows.
#>  Only vectors of size 1 are
#>   recycled.
#> Caused by error in `vectbl_recycle_rhs_rows()` at tibble/R/subassign-backend.R:35:5:
#> ! Can't recycle input of size 5 to size 4.
with_df(df[] <- data.frame(1, "x"))
#>   a b cd
#> 1 1 x  1
#> 2 1 x  1
#> 3 1 x  1
#> 4 1 x  1
with_tbl(tbl[] <- data.frame(1, "x"))
#> Error in `[<-`:
#> ! Can't recycle input of size 2 to size 3.
with_tbl(tbl[] <- data.frame(4:1, "x", 2))
#> # A tibble: 4 × 3
#>       a b        cd
#>   <int> <chr> <dbl>
#> 1     4 x         2
#> 2     3 x         2
#> 3     2 x         2
#> 4     1 x         2
with_tbl(tbl[] <- data.frame(1, "x", 2))
#> # A tibble: 4 × 3
#>       a b        cd
#>   <dbl> <chr> <dbl>
#> 1     1 x         2
#> 2     1 x         2
#> 3     1 x         2
#> 4     1 x         2
with_df(df[] <- data.frame(1, "x", 2, 3))
#> Warning in
#> `[<-.data.frame`(`*tmp*`, , value =
#> structure(list(X1 = 1, X.x. = "x",
#> : provided 4 variables to replace 3
#> variables
#>   a b cd
#> 1 1 x  2
#> 2 1 x  2
#> 3 1 x  2
#> 4 1 x  2
with_tbl(tbl[] <- data.frame(1, "x", 2, 3))
#> Error in `[<-`:
#> ! Can't recycle input of size 4 to size 3.
with_tbl(tbl[] <- tbl)
#> # A tibble: 4 × 3
#>       a b     cd       
#>   <int> <chr> <list>   
#> 1     1 e     <dbl [1]>
#> 2     2 f     <int [2]>
#> 3     3 g     <int [3]>
#> 4     4 h     <chr [1]>
with_tbl(tbl[,] <- 1)
#> # A tibble: 4 × 3
#>       a     b    cd
#>   <dbl> <dbl> <dbl>
#> 1     1     1     1
#> 2     1     1     1
#> 3     1     1     1
#> 4     1     1     1
with_tbl(tbl[,] <- 4:1)
#> # A tibble: 4 × 3
#>       a     b    cd
#>   <int> <int> <int>
#> 1     4     4     4
#> 2     3     3     3
#> 3     2     2     2
#> 4     1     1     1
with_df(df[,] <- 3:1)
#>   a b cd
#> 1 3 2  1
#> 2 2 1  3
#> 3 1 3  2
#> 4 3 2  1
with_tbl(tbl[,] <- 3:1)
#> Error in `[<-`:
#> ! Assigned data `3:1` must
#>   be compatible with existing data.
#>  Existing data has 4 rows.
#>  Assigned data has 3 rows.
#>  Only vectors of size 1 are
#>   recycled.
#> Caused by error in `vectbl_recycle_rhs_rows()` at tibble/R/subassign-backend.R:35:5:
#> ! Can't recycle input of size 3 to size 4.
with_df(df[,] <- 5:1)
#> Error in `[<-.data.frame`(`*tmp*`, , , value = 5:1): replacement has 5 items, need 12
with_tbl(tbl[,] <- 5:1)
#> Error in `[<-`:
#> ! Assigned data `5:1` must
#>   be compatible with existing data.
#>  Existing data has 4 rows.
#>  Assigned data has 5 rows.
#>  Only vectors of size 1 are
#>   recycled.
#> Caused by error in `vectbl_recycle_rhs_rows()` at tibble/R/subassign-backend.R:35:5:
#> ! Can't recycle input of size 5 to size 4.
with_df(df[,] <- data.frame(1, "x"))
#>   a b cd
#> 1 1 x  1
#> 2 1 x  1
#> 3 1 x  1
#> 4 1 x  1
with_tbl(tbl[,] <- data.frame(1, "x"))
#> Error in `[<-`:
#> ! Can't recycle input of size 2 to size 3.
with_tbl(tbl[,] <- data.frame(4:1, "x", 2))
#> # A tibble: 4 × 3
#>       a b        cd
#>   <int> <chr> <dbl>
#> 1     4 x         2
#> 2     3 x         2
#> 3     2 x         2
#> 4     1 x         2
with_tbl(tbl[,] <- data.frame(1, "x", 2))
#> # A tibble: 4 × 3
#>       a b        cd
#>   <dbl> <chr> <dbl>
#> 1     1 x         2
#> 2     1 x         2
#> 3     1 x         2
#> 4     1 x         2
with_df(df[,] <- data.frame(1, "x", 2, 3))
#> Warning in
#> `[<-.data.frame`(`*tmp*`, , , value
#> = structure(list(X1 = 1, : provided
#> 4 variables to replace 3 variables
#>   a b cd
#> 1 1 x  2
#> 2 1 x  2
#> 3 1 x  2
#> 4 1 x  2
with_tbl(tbl[,] <- data.frame(1, "x", 2, 3))
#> Error in `[<-`:
#> ! Can't recycle input of size 4 to size 3.
with_tbl(tbl[,] <- tbl)
#> # A tibble: 4 × 3
#>       a b     cd       
#>   <int> <chr> <list>   
#> 1     1 e     <dbl [1]>
#> 2     2 f     <int [2]>
#> 3     3 g     <int [3]>
#> 4     4 h     <chr [1]>

Subset assignment

Due to tibble’s default of drop = FALSE, updating a portion of a [ subset is still safe, because tibble is still in control. Only one example is given here.

with_df(df["a"][1, ] <- "b")
#>   a b         cd
#> 1 b e          9
#> 2 2 f     10, 11
#> 3 3 g 12, 13, 14
#> 4 4 h       text
with_tbl(tbl["a"][1, ] <- "b")
#> Error in `[<-`:
#> ! Assigned data `"b"` must
#>   be compatible with existing data.
#>  Error occurred for column `a`.
#> Caused by error in `vec_assign()` at tibble/R/subassign-backend.R:193:3:
#> ! Can't convert <character> to <integer>.