Movatterモバイル変換


[0]ホーム

URL:


Invariants: Comparing behavior with dataframes

This vignette defines invariants for subsetting and subset-assignmentfor tibbles, and illustrates where their behaviour differs from dataframes. The goal is to define a small set of invariants thatconsistently define how behaviors interact. Some behaviors are definedusing functions of the vctrs package, e.g. vec_slice(),vec_recycle() andvec_as_index(). Refer totheir documentation for more details about the invariants that theyfollow.

The subsetting and subassignment operators for data frames andtibbles are particularly tricky, because they support both row andcolumn indexes, both of which are optionally missing. We resolve this byfirst defining column access with[[ and$,then column-wise subsetting with[, then row-wisesubsetting, then the composition of both.

Conventions

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

library(tibble)library(vctrs)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 presentedside 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 × 3#>       n c     li#>   <int> <chr> <list>#> 1     1 e     <dbl [1]>#> 2    NA f     <int [2]>#> 3     3 g     <int [3]>#> 4    NA h     <chr [1]>

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

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

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

Where needed, we also show examples with hierarchical columnscontaining 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 × 3#>       n c     li#>   <int> <chr> <list>#> 1     1 e     <dbl [1]>#> 2    NA f     <int [2]>#> 3     3 g     <int [3]>#> 4    NA h     <chr [1]>

For subset assignment (subassignment, for short), we need a freshcopy of the data for each test. Thewith_*() functions(omitted here for brevity) allow for a more concise notation. Thesefunctions take an assignment expression, execute it on a fresh copy ofthe data, and return the data for printing. The first example printswhat’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 × 3#>       n c     li#>   <int> <chr> <list>#> 1    NA e     <dbl [1]>#> 2     3 f     <int [2]>#> 3    NA g     <int [3]>#> 4     1 h     <chr [1]>

Column extraction

Definition ofx[[j]]

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

tbl[[1]]#> [1]  1 NA  3 NA
.subset2(tbl,1)#> [1]  1 NA  3 NA

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

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

df[[1:2]]#> [1] NA
tbl[[1:2]]#> Error:#> ! The `j` argument of#>   `[[.tbl_df()` can't be a vector#>   of length 2 as of tibble 3.0.0.#> ℹ Recursive subsetting is#>   deprecated for tibbles.
df[[c("n","c")]]#> Error in .subset2(x, i, exact = exact): subscript out of bounds
tbl[[c("n","c")]]#> Error in `tbl[[c("n", "c")]]`:#> ! Can't extract column with `c("n", "c")`.#> ✖ Subscript `c("n", "c")` must be size 1, not 2.
df[[TRUE]]#> [1]  1 NA  3 NA
tbl[[TRUE]]#> Error in `tbl[[TRUE]]`:#> ! Can't extract column with `TRUE`.#> ✖ `TRUE` must be numeric or character, not `TRUE`.
df[[mean]]#> Error in .subset2(x, i, exact = exact): invalid subscript type 'closure'
tbl[[mean]]#> Error in `tbl[[mean]]`:#> ! Can't extract column with `mean`.#> ✖ `mean` must be numeric or character, not a function.

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

df[[NA]]#> NULL
tbl[[NA]]#> Error in `tbl[[NA]]`:#> ! Can't extract column with `NA`.#> ✖ Subscript `NA` must be a location, not an integer `NA`.
df[[NA_character_]]#> NULL
tbl[[NA_character_]]#> Error in `tbl[[NA_character_]]`:#> ! Can't extract column with `NA_character_`.#> ✖ Subscript `NA_character_` must be a location, not a character `NA`.
df[[NA_integer_]]#> NULL
tbl[[NA_integer_]]#> Error in `tbl[[NA_integer_]]`:#> ! Can't extract column with `NA_integer_`.#> ✖ Subscript `NA_integer_` must be a location, not an integer `NA`.
df[[-1]]#> Error in .subset2(x, i, exact = exact): invalid negative subscript in get1index <real>
tbl[[-1]]#> Error in `tbl[[-1]]`:#> ! Can't extract column with `-1`.#> ✖ Subscript `-1` must be a positive location, not -1.
df[[4]]#> Error in .subset2(x, i, exact = exact): subscript out of bounds
tbl[[4]]#> Error in `tbl[[4]]`:#> ! Can't extract columns past the end.#> ℹ Location 4 doesn't exist.#> ℹ There are only 3 columns.
df[[1.5]]#> [1]  1 NA  3 NA
tbl[[1.5]]#> Error in `tbl[[1.5]]`:#> ! Can't extract column with `1.5`.#> ✖ Can't convert from `j` <double> to <integer> due to loss of precision.
df[[Inf]]#> NULL
tbl[[Inf]]#> Error in `tbl[[Inf]]`:#> ! Can't extract column with `Inf`.#> ✖ Can't convert from `j` <double> to <integer> due to loss of precision.

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

tbl[["x"]]#> NULL

Definition ofx$name

x$name andx$"name" are equal tox[["name"]].

tbl$n#> [1]  1 NA  3 NA
tbl$"n"#> [1]  1 NA  3 NA
tbl[["n"]]#> [1]  1 NA  3 NA

Unlike data frames, tibbles do not partially match names. Becausedf$x is rarely used in packages, it can raise awarning:

df$l#> [[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 ofx[j]

j is converted to an integer vector byvec_as_index(j, ncol(x), names = names(x)). Thenx[c(j_1, j_2, ..., j_n)] is equivalent totibble(x[[j_1]], x[[j_2]], ..., x[[j_n]]), keeping thecorresponding column names. This implies thatj must be anumeric or character vector, or a logical vector with length 1 orncol(x).1

tbl[1:2]#> # A tibble: 4 × 2#>       n c#>   <int> <chr>#> 1     1 e#> 2    NA f#> 3     3 g#> 4    NA h

When subsetting repeated indexes, the resulting column names areundefined, 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 × 2#>       n     n#>   <int> <int>#> 1     1     1#> 2    NA    NA#> 3     3     3#> 4    NA    NA

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

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

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

Tibbles support indexing by a logical matrix, but only if all valuesin 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 in `vec_c()`:#> ! Can't combine `n` <integer> and `c` <character>.

Definition ofx[, j]

x[, j] is equal tox[j]. Tibbles do notperform column extraction ifx[j] would yield a singlecolumn.

df[,1]#> [1]  1 NA  3 NA
tbl[,1]#> # A tibble: 4 × 1#>       n#>   <int>#> 1     1#> 2    NA#> 3     3#> 4    NA
tbl[,1:2]#> # A tibble: 4 × 2#>       n c#>   <int> <chr>#> 1     1 e#> 2    NA f#> 3     3 g#> 4    NA h

Definition ofx[, j, drop = TRUE]

For backward compatiblity,x[, j, drop = TRUE] performscolumnextraction, returningx[j][[1]]whenncol(x[j]) is 1.

tbl[,1, drop=TRUE]#> [1]  1 NA  3 NA

Row subsetting

Definition ofx[i, ]

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

tbl[3, ]#> # A tibble: 1 × 3#>       n c     li#>   <int> <chr> <list>#> 1     3 g     <int [3]>

This means thati must be a numeric vector, or a logicalvector of lengthnrow(x) or 1. For compatibility,i can also be a character vector containing positivenumbers.

df[mean, ]#> Error in xj[i]: invalid subscript type 'closure'
tbl[mean, ]#> Error in `tbl[mean, ]`:#> ! Can't subset rows with `mean`.#> ✖ `mean` must be logical, numeric, or character, not a function.
df[list(1), ]#> Error in xj[i]: invalid subscript type 'list'
tbl[list(1), ]#> Error in `tbl[list(1), ]`:#> ! Can't subset rows with `list(1)`.#> ✖ `list(1)` must be logical, numeric, or character, not a list.
tbl["1", ]#> # A tibble: 1 × 3#>       n c     li#>   <int> <chr> <list>#> 1     1 e     <dbl [1]>

Exception: OOB values generate warnings instead of errors:

tbl[10, ]#> # A tibble: 1 × 3#>       n c     li#>   <int> <chr> <list>#> 1    NA <NA>  <NULL>
tbl["x", ]#> # A tibble: 1 × 3#>       n c     li#>   <int> <chr> <list>#> 1    NA <NA>  <NULL>

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 in `tbl[c(TRUE, FALSE), ]`:#> ! Can't subset rows with `c(TRUE, FALSE)`.#> ✖ Logical subscript `c(TRUE, FALSE)` must be size 1 or 4, not 2.

NB: scalar logicals are recycled, but scalar numerics are not. Thatmakes thex[NA, ] andx[NA_integer_, ] returndifferent results.

tbl[NA, ]#> # A tibble: 4 × 3#>       n c     li#>   <int> <chr> <list>#> 1    NA <NA>  <NULL>#> 2    NA <NA>  <NULL>#> 3    NA <NA>  <NULL>#> 4    NA <NA>  <NULL>
tbl[NA_integer_, ]#> # A tibble: 1 × 3#>       n c     li#>   <int> <chr> <list>#> 1    NA <NA>  <NULL>

Definition ofx[i, , drop = TRUE]

drop = TRUE has no effect when not selecting a singlerow:

df[1, , drop=TRUE]#> $n#> [1] 1#>#> $c#> [1] "e"#>#> $li#> $li[[1]]#> [1] 9
tbl[1, , drop=TRUE]#> # A tibble: 1 × 3#>       n c     li#>   <int> <chr> <list>#> 1     1 e     <dbl [1]>

Row and column subsetting

Definition ofx[] andx[,]

x[] andx[,] are equivalent tox.3

Definition ofx[i, j]

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

Definition ofx[[i, j]]

i must be a numeric vector of length 1.x[[i, j]] is equal tox[i, ][[j]], orvctrs::vec_slice(x[[j]], i).5

df[[1,1]]#> [1] 1df[[1,3]]#> [1] 9

This implies thatj must be a numeric or charactervector of length 1.

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

Column update

Definition ofx[[j]] <- a

Ifa is a vector thenx[[j]] <- areplaces thejth column with valuea.

a is recycled to the same size asx so musthave sizenrow(x) or 1. (The only exception is whena isNULL, as described below.) Recycling alsoworks for list, data frame, and matrix columns.

with_tbl(tbl[["li"]]<-list(0))#> # A tibble: 4 × 3#>       n c     li#>   <int> <chr> <list>#> 1     1 e     <dbl [1]>#> 2    NA f     <dbl [1]>#> 3     3 g     <dbl [1]>#> 4    NA h     <dbl [1]>
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 × 2#>    tb$n $c    $li       m[,1]  [,2]#>   <int> <chr> <list>    <dbl> <dbl>#> 1     1 e     <dbl [1]>     1     0#> 2     1 e     <dbl [1]>     0     1#> 3     1 e     <dbl [1]>     0     0#> 4     1 e     <dbl [1]>     0     0#> # ℹ 1 more variable: m[3:4] <dbl>
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 × 2#>    tb$n $c    $li       m[,1]  [,2]#>   <int> <chr> <list>    <dbl> <dbl>#> 1     1 e     <dbl [1]>     1     0#> 2    NA f     <int [2]>     1     0#> 3     3 g     <int [3]>     1     0#> 4    NA h     <chr [1]>     1     0#> # ℹ 1 more variable: m[3:4] <dbl>

j must be a scalar numeric or a string, and cannot beNA. Ifj is OOB, a new column is added on theright hand side, with name repair if needed.

with_tbl(tbl[["x"]]<-0)#> # A tibble: 4 × 4#>       n c     li            x#>   <int> <chr> <list>    <dbl>#> 1     1 e     <dbl [1]>     0#> 2    NA f     <int [2]>     0#> 3     3 g     <int [3]>     0#> 4    NA h     <chr [1]>     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 × 4#>       n c     li         ...4#>   <int> <chr> <list>    <dbl>#> 1     1 e     <dbl [1]>     0#> 2    NA f     <int [2]>     0#> 3     3 g     <int [3]>     0#> 4    NA h     <chr [1]>     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 <NA>  0#> 3  3 g 12, 13, 14 <NA>  0#> 4 NA h       text <NA>  0
with_tbl(tbl[[5]]<-0)#> 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.

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

[[<- supports removing a column by assigningNULL to it.

Removing a nonexistent column is a no-op.

Definition ofx$name <- a

x$name <- a andx$"name" <- a areequivalent tox[["name"]] <- a.6

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

$<- does not perform partial matching.

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

Column subassignment:x[j] <- a

a is a list or data frame

Ifinherits(a, "list") orinherits(a, "data.frame") isTRUE, thenx[j] <- a is equivalent tox[[j[[1]]] <- a[[1]],x[[j[[2]]]] <- a[[2]], …

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

Iflength(a) equals 1, then it is recycled to the samelength asj.

with_tbl(tbl[1:2]<-list(1))#> # A tibble: 4 × 3#>       n     c li#>   <dbl> <dbl> <list>#> 1     1     1 <dbl [1]>#> 2     1     1 <int [2]>#> 3     1     1 <int [3]>#> 4     1     1 <chr [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 in `[<-`:#> ! Can't recycle `list(0, 0, 0)` (size 3) to size 2.
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 in `[<-`:#> ! Can't recycle `list(0, 0)` (size 2) to size 3.

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 in `[<-`:#> ! Column index 1 is used#>   more than once for assignment.

Ifa containsNULL values, thecorresponding columns are removedafter updating (i.e. positionindexes refer to columns before any modifications).

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

NA indexes are not supported.

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

Appending columns at the end (without gaps) is supported. The name ofnew columns is determined by the LHS, the RHS, or by name repair (inthat order of precedence).

with_tbl(tbl[c("x","y")]<-tibble("x",x =4:1))#> # A tibble: 4 × 5#>       n c     li        x         y#>   <int> <chr> <list>    <chr> <int>#> 1     1 e     <dbl [1]> x         4#> 2    NA f     <int [2]> x         3#> 3     3 g     <int [3]> x         2#> 4    NA h     <chr [1]> x         1
with_tbl(tbl[3:4]<-list("x",x =4:1))#> # A tibble: 4 × 4#>       n c     li        x#>   <int> <chr> <chr> <int>#> 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 × 4#>       n c     li         ...4#>   <int> <chr> <list>    <int>#> 1     1 e     <dbl [1]>     4#> 2    NA f     <int [2]>     3#> 3     3 g     <int [3]>     2#> 4    NA h     <chr [1]>     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 in `[<-`:#> ! Can't assign to columns beyond the end with non-consecutive locations.#> ℹ Input has size 3.#> ✖ Subscript `5` contains non-consecutive location 5.

Tibbles support indexing by a logical matrix, but only for a scalarRHS, and if all columns updated are compatible with the valueassigned.

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 × 3#>       n c     li#>   <int> <chr> <list>#> 1     1 e     <dbl [1]>#> 2     4 f     <int [2]>#> 3     3 g     <int [3]>#> 4     4 h     <chr [1]>
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 `[<-`:#> ! Subscript `is.na(tbl)` is#>   a matrix, the data `1:2` must#>   have size 1.
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 in `[<-`:#> ! Assigned data `4` must be#>   compatible with existing data.#> ℹ Error occurred for column `c`.#> Caused by error in `vec_assign()`:#> ! Can't convert <double> to <character>.

a is a matrix or array

Ifis.matrix(a), thena is coerced to adata frame withas.data.frame() before assigning. If rowsare assigned, the matrix type must be compatible with all columns. Ifis.array(a) andany(dim(a)[-1:-2] != 1), anerror is thrown.

with_tbl(tbl[1:2]<-matrix(8:1,ncol =2))#> # A tibble: 4 × 3#>       n     c li#>   <int> <int> <list>#> 1     8     4 <dbl [1]>#> 2     7     3 <int [2]>#> 3     6     2 <int [3]>#> 4     5     1 <chr [1]>
with_df(df[1:3,1:2]<-matrix(6:1,ncol =2))#>    n c         li#> 1  6 3          9#> 2  5 2     10, 11#> 3  4 1 12, 13, 14#> 4 NA h       text
with_tbl(tbl[1:3,1:2]<-matrix(6:1,ncol =2))#> Error in `[<-`:#> ! Assigned data `matrix(6:1,#>   ncol = 2)` must be compatible#>   with existing data.#> ℹ Error occurred for column `c`.#> Caused by error in `vec_assign()`:#> ! Can't convert <integer> to <character>.
with_tbl(tbl[1:2]<-array(4:1,dim =c(4,1,1)))#> # A tibble: 4 × 3#>       n     c li#>   <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[1:2]<-array(8:1,dim =c(4,2,1)))#> # A tibble: 4 × 3#>       n     c li#>   <int> <int> <list>#> 1     8     4 <dbl [1]>#> 2     7     3 <int [2]>#> 3     6     2 <int [3]>#> 4     5     1 <chr [1]>
with_df(df[1:2]<-array(8:1,dim =c(2,1,4)))#>   n c         li#> 1 8 4          9#> 2 7 3     10, 11#> 3 6 2 12, 13, 14#> 4 5 1       text
with_tbl(tbl[1:2]<-array(8:1,dim =c(2,1,4)))#> Error in `[<-`:#> ! `array(8:1, dim = c(2, 1,#>   4))` must be a vector, a bare#>   list, a data frame, a matrix, or#>   NULL.
with_df(df[1:2]<-array(8:1,dim =c(4,1,2)))#>   n c         li#> 1 8 4          9#> 2 7 3     10, 11#> 3 6 2 12, 13, 14#> 4 5 1       text
with_tbl(tbl[1:2]<-array(8:1,dim =c(4,1,2)))#> Error in `[<-`:#> ! `array(8:1, dim = c(4, 1,#>   2))` must be a vector, a bare#>   list, a data frame, a matrix, or#>   NULL.

a is another type of vector

Ifvec_is(a), thenx[j] <- a isequivalent tox[j] <- list(a). This is primarilyprovided for backward compatibility.

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

Matrices must be wrapped inlist() before assignment tocreate a matrix column.

with_tbl(tbl[1]<-list(matrix(1:8,ncol =2)))#> # A tibble: 4 × 3#>   n[,1]  [,2] c     li#>   <int> <int> <chr> <list>#> 1     1     5 e     <dbl [1]>#> 2     2     6 f     <int [2]>#> 3     3     7 g     <int [3]>#> 4     4     8 h     <chr [1]>
with_tbl(tbl[1:2]<-list(matrix(1:8,ncol =2)))#> # A tibble: 4 × 3#>   n[,1]  [,2] c[,1]  [,2] li#>   <int> <int> <int> <int> <list>#> 1     1     5     1     5 <dbl [1]>#> 2     2     6     2     6 <int [2]>#> 3     3     7     3     7 <int [3]>#> 4     4     8     4     8 <chr [1]>

a isNULL

Entire columns can be removed. Specifyingi is anerror.

with_tbl(tbl[1]<-NULL)#> # A tibble: 4 × 2#>   c     li#>   <chr> <list>#> 1 e     <dbl [1]>#> 2 f     <int [2]>#> 3 g     <int [3]>#> 4 h     <chr [1]>
with_tbl(tbl[,2:3]<-NULL)#> # A tibble: 4 × 1#>       n#>   <int>#> 1     1#> 2    NA#> 3     3#> 4    NA
with_df(df[1,2:3]<-NULL)#> Error in x[[jj]][iseq] <- vjj: replacement has length zero
with_tbl(tbl[1,2:3]<-NULL)#> Error in `[<-`:#> ! `NULL` must be a vector, a#>   bare list, a data frame or a#>   matrix.

a is not a vector

Any other type fora is an error. Note that ifis.list(a) isTRUE, butinherits(a, "list") isFALSE, thena is considered to be a scalar. See?vec_isand?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 `[<-`:#> ! `mean` must be a vector, a#>   bare list, a data frame, a#>   matrix, or NULL.
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 in `[<-`:#> ! `lm(mpg ~ wt, data =#>   mtcars)` must be a vector, a bare#>   list, a data frame, a matrix, or#>   NULL.

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

x[i, ] <- a is the same asvec_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 × 3#>       n c     li#>   <int> <chr> <list>#> 1     1 e     <dbl [1]>#> 2     1 e     <dbl [1]>#> 3     1 e     <dbl [1]>#> 4    NA h     <chr [1]>
with_tbl(tbl[c(FALSE,TRUE,TRUE,FALSE), ]<- tbl[1, ])#> # A tibble: 4 × 3#>       n c     li#>   <int> <chr> <list>#> 1     1 e     <dbl [1]>#> 2     1 e     <dbl [1]>#> 3     1 e     <dbl [1]>#> 4    NA h     <chr [1]>

Only values of size one can be recycled.

with_tbl(tbl[2:3, ]<- tbl[1, ])#> # A tibble: 4 × 3#>       n c     li#>   <int> <chr> <list>#> 1     1 e     <dbl [1]>#> 2     1 e     <dbl [1]>#> 3     1 e     <dbl [1]>#> 4    NA h     <chr [1]>
with_tbl(tbl[2:3, ]<-list(tbl$n[1], tbl$c[1:2], tbl$li[1]))#> # A tibble: 4 × 3#>       n c     li#>   <int> <chr> <list>#> 1     1 e     <dbl [1]>#> 2     1 e     <dbl [1]>#> 3     1 f     <dbl [1]>#> 4    NA h     <chr [1]>
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 in `[<-`:#> ! Assigned data `tbl[1:2, ]`#>   must be compatible with row#>   subscript `2:4`.#> ✖ 3 rows must be assigned.#> ✖ Element 1 of assigned data has 2#>   rows.#> ℹ Only vectors of size 1 are#>   recycled.#> Caused by error in `vectbl_recycle_rhs_rows()`:#> ! Can't recycle input of size 2 to size 3.

For compatibility, only a warning is issued for indexing beyond thenumber of rows. Appending rows right at the end of the existing data issupported, without warning.

with_tbl(tbl[5, ]<- tbl[1, ])#> # A tibble: 5 × 3#>       n c     li#>   <int> <chr> <list>#> 1     1 e     <dbl [1]>#> 2    NA f     <int [2]>#> 3     3 g     <int [3]>#> 4    NA h     <chr [1]>#> 5     1 e     <dbl [1]>
with_tbl(tbl[5:7, ]<- tbl[1, ])#> # A tibble: 7 × 3#>       n c     li#>   <int> <chr> <list>#> 1     1 e     <dbl [1]>#> 2    NA f     <int [2]>#> 3     3 g     <int [3]>#> 4    NA h     <chr [1]>#> 5     1 e     <dbl [1]>#> 6     1 e     <dbl [1]>#> 7     1 e     <dbl [1]>
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 <NA>       NULL#> 6  1    e          9
with_tbl(tbl[6, ]<- tbl[1, ])#> Error in `[<-`:#> ! Can't assign to rows beyond the end with non-consecutive locations.#> ℹ Input has size 4.#> ✖ Subscript `6` contains non-consecutive location 6.
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 in `[<-`:#> ! Can't negate rows past the end.#> ℹ Location 5 doesn't exist.#> ℹ There are only 4 rows.
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 in `[<-`:#> ! Can't negate rows past the end.#> ℹ Locations 5, 6, and 7#>   don't exist.#> ℹ There are only 4 rows.
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 in `[<-`:#> ! Can't negate rows past the end.#> ℹ Location 6 doesn't exist.#> ℹ There are only 4 rows.

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

with_tbl(tbl[as.character(1:3), ]<- tbl[1, ])#> # A tibble: 4 × 3#>       n c     li#>   <int> <chr> <list>#> 1     1 e     <dbl [1]>#> 2     1 e     <dbl [1]>#> 3     1 e     <dbl [1]>#> 4    NA h     <chr [1]>

Row and column subassignment

Definition ofx[i, j] <- a

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

Subassignment tox[i, j] is stricter for tibbles thanfor data frames.x[i, j] <- a can’t change the data typeof 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 <NA> h       text
with_tbl(tbl[2:3,1]<- tbl[1:2,2])#> Error in `[<-`:#> ! Assigned data `tbl[1:2,#>   2]` must be compatible with#>   existing data.#> ℹ Error occurred for column `n`.#> Caused by error in `vec_assign()`:#> ! Can't convert <character> to <integer>.
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 in `[<-`:#> ! Assigned data `tbl[1:2,#>   3]` must be compatible with#>   existing data.#> ℹ Error occurred for column `c`.#> Caused by error in `vec_assign()`:#> ! Can't convert <list> to <character>.
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 in `[<-`:#> ! Assigned data `tbl2[1:2,#>   1]` must be compatible with#>   existing data.#> ℹ Error occurred for column `li`.#> Caused by error in `vec_assign()`:#> ! Can't convert <tbl_df> to <list>.
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 in `[<-`:#> ! Assigned data `tbl2[1:2,#>   2]` must be compatible with#>   existing data.#> ℹ Error occurred for column `tb`.#> Caused by error in `vec_assign()`:#> ! Can't convert <double[,4]> to <tbl_df>.
with_tbl2(tbl2[2:3,2]<- tbl[1:2,1])#> # A tibble: 4 × 2#>    tb$n $c    $li       m[,1]  [,2]#>   <int> <chr> <list>    <dbl> <dbl>#> 1     1 e     <dbl [1]>     1     0#> 2    NA f     <int [2]>     1     1#> 3     3 g     <int [3]>    NA    NA#> 4    NA h     <chr [1]>     0     0#> # ℹ 1 more variable: m[3:4] <dbl>

A notable exception is the population of a column full ofNA (which is of typelogical), or the use ofNA on the right-hand side of the assignment.

with_tbl({tbl$x<-NA; tbl[2:3,"x"]<-3:2})#> # A tibble: 4 × 4#>       n c     li            x#>   <int> <chr> <list>    <int>#> 1     1 e     <dbl [1]>    NA#> 2    NA f     <int [2]>     3#> 3     3 g     <int [3]>     2#> 4    NA h     <chr [1]>    NA
with_df({df[2:3,2:3]<-NA})#>    n    c   li#> 1  1    e    9#> 2 NA <NA>   NA#> 3  3 <NA>   NA#> 4 NA    h text
with_tbl({tbl[2:3,2:3]<-NA})#> # A tibble: 4 × 3#>       n c     li#>   <int> <chr> <list>#> 1     1 e     <dbl [1]>#> 2    NA <NA>  <NULL>#> 3     3 <NA>  <NULL>#> 4    NA h     <chr [1]>

For programming, it is always safer (and faster) to use the correcttype ofNA to initialize columns.

with_tbl({tbl$x<-NA_integer_; tbl[2:3,"x"]<-3:2})#> # A tibble: 4 × 4#>       n c     li            x#>   <int> <chr> <list>    <int>#> 1     1 e     <dbl [1]>    NA#> 2    NA f     <int [2]>     3#> 3     3 g     <int [3]>     2#> 4    NA h     <chr [1]>    NA

For new columns,x[i, j] <- a fills the unassignedrows withNA.

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 × 3#>       n c     li#>   <int> <chr> <list>#> 1     1 e     <dbl [1]>#> 2     1 f     <int [2]>#> 3     1 g     <int [3]>#> 4    NA h     <chr [1]>
with_tbl(tbl[2:3,"x"]<-1)#> # A tibble: 4 × 4#>       n c     li            x#>   <int> <chr> <list>    <dbl>#> 1     1 e     <dbl [1]>    NA#> 2    NA f     <int [2]>     1#> 3     3 g     <int [3]>     1#> 4    NA h     <chr [1]>    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 in `[<-`:#> ! `NULL` must be a vector, a#>   bare list, a data frame or a#>   matrix.

Likewise, for new rows,x[i, j] <- a fills theunassigned columns withNA.

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

Definition ofx[[i, j]] <- a

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

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


  1. x[j][[jj]] is equal tox[[ j[[jj]] ]], in particularx[j][[1]] isequal tox[[j]] for scalar numeric or integerj.↩︎

  2. Row subsettingx[i, ] is not defined interms ofx[[j]][i] because that definition does notgeneralise to matrix and data frame columns. For efficiency and backwardcompatibility,i is converted to an integer vector byvec_as_index(i, nrow(x)) first.↩︎

  3. x[,] is equivalent tox[]becausex[, j] is equivalent tox[j].↩︎

  4. A more efficient implementation ofx[i, j]would forward tox[j][i, ].↩︎

  5. Cell subsettingx[[i, j]] is not defined interms ofx[[j]][[i]] because that definition does notgeneralise to list, matrix and data frame columns. A more efficientimplementation ofx[[i, j]] would check thatjis a scalar and forward tox[i, j][[1]].↩︎

  6. $ behaves almost completely symmetricallyto[[ when comparing subsetting and subassignment.↩︎

  7. x[i, ] is symmetrical for subset andsubassignment.↩︎

  8. x[i, j] is symmetrical for subsetting andsubassignment. A more efficient implementation ofx[i, j] <- a would forward tox[j][i, ] <- a.↩︎

  9. x[[i, j]] is symmetrical for subsetting andsubassignment. An efficient implementation would check thati andj are scalar and forward tox[i, j][[1]] <- a.↩︎


[8]ページ先頭

©2009-2025 Movatter.jp