1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
|
---
title: "Subassignment"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{subassign}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
<style type="text/css">
.dftbl {
width: 100%;
table-layout: fixed;
display: inline-table;
}
.error pre code {
color: red;
}
.warning pre code {
color: violet;
}
</style>
```{r, include = FALSE}
knitr::opts_chunk$set(
error = TRUE,
collapse = TRUE,
comment = "#>"
)
tibble:::set_dftbl_hooks()
options(
lifecycle_disable_warnings = FALSE,
lifecycle_verbose_soft_deprecation = TRUE,
lifecycle_repeat_warnings = TRUE
)
```
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.
```{r setup}
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:
```{r show, dftbl = TRUE, dftbl_always = TRUE}
new_df()
```
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.
```{r show-compare, dftbl = TRUE}
new_df()
```
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):
```{r with-df-def}
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)
}
```
```{r with-tbl-def, include = FALSE}
with_tbl <- function(code, verbose = FALSE) {
code <- rlang::enexpr(code)
full_code <- rlang::quo({
tbl <- new_tbl()
!!code
tbl
})
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.
```{r with-demo, dftbl = TRUE}
with_df(df$a <- rev(df$a), verbose = TRUE)
```
## $<-
### 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:
```{r dollar-assign-scalar, dftbl = TRUE}
with_df(df$a <- 1)
with_df(df$b <- 1)
with_df(df$c <- 1)
with_df(df$cd <- 1)
```
```{r dollar-assign-full, dftbl = TRUE}
with_df(df$a <- 4:1)
with_df(df$b <- 4:1)
with_df(df$c <- 4:1)
with_df(df$cd <- 4:1)
```
### Recycling
Tibbles allow recycling only for vectors of length 1 or of the same length as the data.
```{r dollar-assign-recycle, dftbl = TRUE}
with_df(df$a <- 1:2)
with_df(df$a <- 1:3)
with_df(df$a <- 1:5)
with_df(df$c <- 1:2)
```
### 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.
```{r dollar-assign-subset, dftbl = TRUE}
with_df(df$a[1:2] <- 4:3)
with_df(df$b[1:2] <- 4:3)
with_df(df$c[1:2] <- 4:3)
with_df(df$cd[1:2] <- 4:3)
with_df(df$a[1:3] <- 4:3)
with_df(df$a[1:4] <- 4:3)
```
For columns of the stricter `"vctrs_vctr"` class, this class implements the check, which then works identically for data frames and tibbles:
```{r vctrs}
with_df({ df$v = vctrs::new_vctr(1:4); df$v[1:2] <- vctrs::new_vctr(4:3)})
with_df({ df$v = vctrs::new_vctr(1:4); df$v[1:2] <- vctrs::new_vctr(letters[4:3])})
```
## [[<-
### 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.
```{r double-bracket-assign-col-scalar, dftbl = TRUE}
with_df(df[["a"]] <- 1)
with_df(df[["a"]] <- "x")
with_df(df[["b"]] <- "x")
with_df(df[["c"]] <- "x")
with_df(df[["cd"]] <- "x")
with_df(df[[1]] <- "x")
with_df(df[[2]] <- "x")
with_df(df[[4]] <- "x")
with_df(df[[5]] <- "x")
```
```{r double-bracket-assign-col-full, dftbl = TRUE}
with_df(df[["a"]] <- 4:1)
with_df(df[["a"]] <- letters[4:1])
with_df(df[["b"]] <- letters[4:1])
with_df(df[["c"]] <- letters[4:1])
with_df(df[["cd"]] <- letters[4:1])
```
### 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:
```{r double-bracket-assign-cell, dftbl = TRUE}
with_df(df[[2, "a"]] <- 1)
with_df(df[[2, "a"]] <- 1.5)
with_df(df[[2, "a"]] <- "x")
with_df(df[[2, "b"]] <- "x")
with_df(df[[2, 1]] <- "x")
with_df(df[[2, 2]] <- "x")
with_df(df[[2, "cd"]] <- "x")
with_df(df[[2, "cd"]] <- list("x"))
with_df(df[[2, "c"]] <- "x")
with_df(df[[1:2, "cd"]] <- "x")
with_df(df[[1:2, "c"]] <- "x")
with_df(df[[2, c("cd", "d")]] <- "x")
```
## [<-
### Scalars
```{r bracket-assign-scalar-col, dftbl = TRUE}
with_df(df[2, "a"] <- 1)
with_df(df[2, "a"] <- 1.5)
with_df(df[2, "a"] <- "x")
with_df(df[2, "b"] <- "x")
with_df(df[2, "cd"] <- "x")
with_df(df[2, "cd"] <- list("x"))
with_df(df[2, "c"] <- "x")
with_df(df[2, 1] <- "x")
with_df(df[2, 2] <- "x")
with_df(df[2, 3] <- "x")
with_df(df[2, 4] <- "x")
```
### Full length columns
```{r bracket-assign-full-col, dftbl = TRUE}
with_df(df[, "a"] <- 4:1)
with_df(df[, "b"] <- 4:1)
with_df(df[, "c"] <- 4:1)
with_df(df[, "cd"] <- 4:1)
with_df(df[, 1] <- 4:1)
with_df(df[, 2] <- 4:1)
with_df(df[, 3] <- 4:1)
with_df(df[, 4] <- 4:1)
with_df(df[, "a"] <- 1)
with_df(df[, "b"] <- 1)
with_df(df[, "c"] <- 1)
with_df(df[, "cd"] <- 1)
with_df(df[, 1] <- 1)
with_df(df[, 2] <- 1)
with_df(df[, 3] <- 1)
with_df(df[, 4] <- 1)
```
### Multiple full length columns
```{r bracket-assign-full-multicol, dftbl = TRUE}
with_df(df[, c("a", "b")] <- 4:1)
with_df(df[, c("a", "b")] <- 1)
with_df(df[, c("a", "b")] <- data.frame(a = 4:1, b = letters[4:1]))
with_df(df[, c("a", "b")] <- data.frame(b = 4:1, a = letters[4:1]))
with_df(df[, c("a", "b")] <- data.frame(c = 4:1, d = letters[4:1]))
with_df(df[, c("a", "b")] <- data.frame(a = 4:1))
with_df(df[, c("a", "b")] <- data.frame(a = 4:1, b = letters[4:1], c = 1:4))
with_df(df[, c("a", "b")] <- data.frame(4:1, 1))
with_df(df[, 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]))
```
### Full length rows
```{r bracket-assign-full-row, dftbl = TRUE}
with_df(df[2, ] <- 1)
with_df(df[2, ] <- "x")
with_df(df[2, ] <- tibble(a = 1, b = "x"))
with_df(df[2, ] <- tibble(a = 1, b = "x", c = list("y")))
with_df(df[2, ] <- tibble(a = 1, b = "x", c = list("y"), d = "z"))
with_df(df[0, ] <- tibble(a = 1, b = "x", c = list("y")))
with_df(df[5, ] <- tibble(a = 1, b = "x", c = list("y")))
```
### Multiple full length rows
```{r bracket-assign-full-multirow, dftbl = TRUE}
with_df(df[2:3, ] <- 1)
with_df(df[2:3, ] <- 1:2)
with_df(df[2:3, ] <- c("x", "y"))
with_df(df[2:3, ] <- tibble(a = 1:2, b = c("x", "y")))
with_df(df[2:3, ] <- tibble(a = 1, b = "x", c = list("y")))
with_df(df[2:3, ] <- tibble(a = 1:2, b = "x", c = list("y")))
with_df(df[2:3, ] <- tibble(a = 1, b = "x", c = list("y"), d = "z"))
with_df(df[-(1:2), ] <- tibble(a = 1:2, b = "x", c = list("y")))
with_df(df[0:1, ] <- tibble(a = 1:2, b = "x", c = list("y")))
with_df(df[4:5, ] <- tibble(a = 1:2, b = "x", c = list("y")))
```
### Unspecified
```{r bracket-assign-unspecified, dftbl = TRUE}
with_df(df[] <- 1)
with_df(df[] <- 4:1)
with_df(df[] <- 3:1)
with_df(df[] <- 5:1)
with_df(df[] <- data.frame(1, "x"))
with_df(df[] <- data.frame(4:1, "x", 2))
with_df(df[] <- data.frame(1, "x", 2))
with_df(df[] <- data.frame(1, "x", 2, 3))
with_df(df[] <- df)
with_df(df[,] <- 1)
with_df(df[,] <- 4:1)
with_df(df[,] <- 3:1)
with_df(df[,] <- 5:1)
with_df(df[,] <- data.frame(1, "x"))
with_df(df[,] <- data.frame(4:1, "x", 2))
with_df(df[,] <- data.frame(1, "x", 2))
with_df(df[,] <- data.frame(1, "x", 2, 3))
with_df(df[,] <- df)
```
### 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.
```{r bracket-assign-subset, dftbl = TRUE}
with_df(df["a"][1, ] <- "b")
```
|