../../../data/GHE/mpn/deployment/2020-04-23/vignettes/wip/subassign.Rmd
subassign.Rmd
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 x 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 x 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 x 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]> |
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 x 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 x 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 x 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 x 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 x 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 x 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 x 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 x 3 #> a b cd #> <int> <chr> <int> #> 1 1 e 4 #> 2 2 f 3 #> 3 3 g 2 #> 4 4 h 1 |
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)
|
with_df(df$a <- 1:3)
|
with_tbl(tbl$a <- 1:3)
|
with_df(df$a <- 1:5)
|
with_tbl(tbl$a <- 1:5)
|
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)
|
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 x 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 x 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)
|
with_tbl(tbl$cd[1:2] <- 4:3) #> # A tibble: 4 x 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)
|
with_tbl(tbl$a[1:3] <- 4:3)
|
with_tbl(tbl$a[1:4] <- 4:3) #> # A tibble: 4 x 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: No common type for `x` <vctrs_vctr> and `value` <vctrs_vctr>.
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 x 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 x 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 x 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 x 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 x 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 x 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 x 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 x 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")
|
with_tbl(tbl[[5]] <- "x")
|
with_tbl(tbl[["a"]] <- 4:1) #> # A tibble: 4 x 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 x 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 x 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 x 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 x 3 #> a b cd #> <int> <chr> <chr> #> 1 1 e d #> 2 2 f c #> 3 3 g b #> 4 4 h a |
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 x 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)
|
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")
|
with_tbl(tbl[[2, "b"]] <- "x") #> # A tibble: 4 x 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")
|
with_tbl(tbl[[2, 2]] <- "x") #> # A tibble: 4 x 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")
|
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 x 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")
|
with_tbl(tbl[[2, "c"]] <- "x") #> # A tibble: 4 x 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")
|
with_tbl(tbl[[1:2, "cd"]] <- "x")
|
with_df(df[[1:2, "c"]] <- "x")
|
with_tbl(tbl[[1:2, "c"]] <- "x")
|
with_df(df[[2, c("cd", "d")]] <- "x")
|
with_tbl(tbl[[2, c("cd", "d")]] <- "x")
|
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 x 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)
|
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")
|
with_tbl(tbl[2, "b"] <- "x") #> # A tibble: 4 x 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")
|
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"))
|
with_tbl(tbl[2, "c"] <- "x") #> # A tibble: 4 x 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")
|
with_tbl(tbl[2, 2] <- "x") #> # A tibble: 4 x 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")
|
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 x 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> |
with_tbl(tbl[, "a"] <- 4:1) #> # A tibble: 4 x 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 x 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 x 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 x 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 x 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 x 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 x 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 x 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 x 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 x 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 x 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 x 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 x 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 x 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 x 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 x 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 |
with_tbl(tbl[, c("a", "b")] <- 4:1) #> # A tibble: 4 x 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 x 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 x 3 #> a b cd #> <int> <fct> <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 x 3 #> a b cd #> <int> <fct> <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 x 3 #> a b cd #> <int> <fct> <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 x 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))
|
with_tbl(tbl[, c("a", "b")] <- data.frame(a = 4:1, b = letters[4:1], c = 1:4))
|
with_tbl(tbl[, c("a", "b")] <- data.frame(4:1, 1)) #> # A tibble: 4 x 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]))
|
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]))
|
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)
|
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")
|
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"))
|
|
|
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)
|
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)
|
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"))
|
|
|
|
|
|
|
with_tbl(tbl[] <- 1) #> # A tibble: 4 x 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 x 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)
|
with_df(df[] <- 5:1)
|
with_tbl(tbl[] <- 5:1)
|
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"))
|
with_tbl(tbl[] <- data.frame(4:1, "x", 2)) #> # A tibble: 4 x 3 #> a b cd #> <int> <fct> <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 x 3 #> a b cd #> <dbl> <fct> <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))
|
with_tbl(tbl[] <- data.frame(1, "x", 2, 3))
|
with_tbl(tbl[] <- tbl) #> # A tibble: 4 x 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 x 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 x 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)
|
with_df(df[,] <- 5:1)
|
with_tbl(tbl[,] <- 5:1)
|
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"))
|
with_tbl(tbl[,] <- data.frame(4:1, "x", 2)) #> # A tibble: 4 x 3 #> a b cd #> <int> <fct> <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 x 3 #> a b cd #> <dbl> <fct> <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))
|
with_tbl(tbl[,] <- data.frame(1, "x", 2, 3))
|
with_tbl(tbl[,] <- tbl) #> # A tibble: 4 x 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]> |
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")
|