This provides a common interface to extracting and modifying observations for all vector types, regardless of dimensionality. It is an analog to [ that matches vec_size() instead of length().

vec_slice(x, i)

vec_slice(x, i) <- value

vec_assign(x, i, value)

Arguments

x

A vector

i

An integer, character or logical vector specifying the positions or names of the observations to get/set. Specify TRUE to index all elements (as in x[]), or NULL, FALSE or integer() to index none (as in x[NULL]).

value

Replacement values. value is cast to the type of x, but only if they have a common type. See below for examples of this rule.

Value

A vector of the same type as x.

Genericity

Support for S3 objects depends on whether the object implements a vec_proxy() method.

  • When a vec_proxy() method exists, the proxy is sliced and vec_restore() is called on the result.

  • Otherwise vec_slice() falls back to the base generic [.

Note that S3 lists are treated as scalars by default, and will cause an error if they don't implement a vec_proxy() method.

Differences with base R subsetting

  • vec_slice() only slices along one dimension. For two-dimensional types, the first dimension is subsetted.

  • vec_slice() preserves attributes by default.

  • vec_slice<-() is type-stable and always returns the same type as the LHS.

Examples

x <- sample(10) x
#> [1] 9 3 1 10 5 6 8 4 7 2
vec_slice(x, 1:3)
#> [1] 9 3 1
# You can assign with the infix variant: vec_slice(x, 2) <- 100 x
#> [1] 9 100 1 10 5 6 8 4 7 2
# Or with the regular variant that doesn't modify the original input: y <- vec_assign(x, 3, 500) y
#> [1] 9 100 500 10 5 6 8 4 7 2
x
#> [1] 9 100 1 10 5 6 8 4 7 2
# Slicing objects of higher dimension: vec_slice(mtcars, 1:3)
#> mpg cyl disp hp drat wt qsec vs am gear carb #> Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 #> Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 #> Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
# Type stability -------------------------------------------------- # The assign variant is type stable. It always returns the same # type as the input. x <- 1:5 vec_slice(x, 2) <- 20.0 # `x` is still an integer vector because the RHS was cast to the # type of the LHS: vec_ptype(x)
#> integer(0)
# Compare to `[<-`: x[2] <- 20.0 vec_ptype(x)
#> numeric(0)
# Note that the types must be coercible for the cast to happen. # For instance, you can cast a character vector to an integer: vec_cast("1", integer())
#> [1] 1
# But these types are not coercible: try(vec_ptype2("1", integer()))
#> Error : No common type for `x` <character> and `y` <integer>. #> Backtrace: #> █ #> 1. ├─base::tryCatch(...) #> 2. │ └─base:::tryCatchList(expr, classes, parentenv, handlers) #> 3. │ ├─base:::tryCatchOne(...) #> 4. │ │ └─base:::doTryCatch(return(expr), name, parentenv, handler) #> 5. │ └─base:::tryCatchList(expr, names[-nh], parentenv, handlers[-nh]) #> 6. │ └─base:::tryCatchOne(expr, names, parentenv, handlers[[1L]]) #> 7. │ └─base:::doTryCatch(return(expr), name, parentenv, handler) #> 8. ├─base::withCallingHandlers(...) #> 9. ├─base::saveRDS(...) #> 10. ├─base::do.call(...) #> 11. ├─(function (what, args, quote = FALSE, envir = parent.frame()) ... #> 12. ├─(function (..., crayon_enabled, crayon_colors, pkgdown_internet) ... #> 13. │ └─pkgdown::build_site(...) #> 14. │ └─pkgdown:::build_site_local(...) #> 15. │ └─pkgdown::build_reference(...) #> 16. │ └─purrr::map(...) #> 17. │ └─pkgdown:::.f(.x[[i]], ...) #> 18. │ └─pkgdown:::data_reference_topic(...) #> 19. │ ├─pkgdown:::as_data(...) #> 20. │ └─pkgdown:::as_data.tag_examples(...) #> 21. │ └─purrr::pmap_chr(...) #> 22. │ └─pkgdown:::.f(...) #> 23. │ ├─withr::with_options(...) #> 24. │ │ └─base::force(code) #> 25. │ └─evaluate::evaluate(code, env, new_device = TRUE) #> 26. │ └─evaluate:::evaluate_call(...) #> 27. │ ├─evaluate:::timing_fn(...) #> 28. │ ├─evaluate:::handle(...) #> 29. │ │ └─base::try(f, silent = TRUE) #> 30. │ │ └─base::tryCatch(...) #> 31. │ │ └─base:::tryCatchList(expr, classes, parentenv, handlers) #> 32. │ │ └─base:::tryCatchOne(expr, names, parentenv, handlers[[1L]]) #> 33. │ │ └─base:::doTryCatch(return(expr), name, parentenv, handler) #> 34. │ ├─base::withCallingHandlers(...) #> 35. │ ├─base::withVisible(eval(expr, envir, enclos)) #> 36. │ └─base::eval(expr, envir, enclos) #> 37. │ └─base::eval(expr, envir, enclos) #> 38. │ ├─base::try(vec_ptype2("1", integer())) #> 39. │ │ └─base::tryCatch(...) #> 40. │ │ └─base:::tryCatchList(expr, classes, parentenv, handlers) #> 41. │ │ └─base:::tryCatchOne(expr, names, parentenv, handlers[[1L]]) #> 42. │ │ └─base:::doTryCatch(return(expr), name, parentenv, handler) #> 43. │ └─vctrs::vec_ptype2("1", integer()) #> 44. ├─vctrs:::vec_type2_dispatch(x = x, y = y, x_arg = x_arg, y_arg = y_arg) #> 45. ├─vctrs::vec_ptype2.character(x = x, y = y, x_arg = x_arg, y_arg = y_arg) /home/travis/build/r-lib/vctrs/R/type2.R:46:2 #> 46. └─vctrs:::vec_ptype2.character.default(...) /home/travis/build/r-lib/vctrs/R/type-bare.R:58:24 #> 47. └─vctrs::vec_default_ptype2(x, y, x_arg = x_arg, y_arg = y_arg) /home/travis/build/r-lib/vctrs/R/type-bare.R:129:2 #> 48. └─vctrs::stop_incompatible_type(x, y, x_arg = x_arg, y_arg = y_arg) /home/travis/build/r-lib/vctrs/R/type2.R:63:2 #> 49. └─vctrs:::stop_incompatible(...) /home/travis/build/r-lib/vctrs/R/conditions.R:92:2 #> 50. └─vctrs:::stop_vctrs(...) /home/travis/build/r-lib/vctrs/R/conditions.R:48:2
# Hence you cannot assign character values to an integer or double # vector: try(vec_slice(x, 2) <- "20")
#> Error : No common type for `value` <character> and `x` <double>. #> Backtrace: #> █ #> 1. ├─base::tryCatch(...) #> 2. │ └─base:::tryCatchList(expr, classes, parentenv, handlers) #> 3. │ ├─base:::tryCatchOne(...) #> 4. │ │ └─base:::doTryCatch(return(expr), name, parentenv, handler) #> 5. │ └─base:::tryCatchList(expr, names[-nh], parentenv, handlers[-nh]) #> 6. │ └─base:::tryCatchOne(expr, names, parentenv, handlers[[1L]]) #> 7. │ └─base:::doTryCatch(return(expr), name, parentenv, handler) #> 8. ├─base::withCallingHandlers(...) #> 9. ├─base::saveRDS(...) #> 10. ├─base::do.call(...) #> 11. ├─(function (what, args, quote = FALSE, envir = parent.frame()) ... #> 12. ├─(function (..., crayon_enabled, crayon_colors, pkgdown_internet) ... #> 13. │ └─pkgdown::build_site(...) #> 14. │ └─pkgdown:::build_site_local(...) #> 15. │ └─pkgdown::build_reference(...) #> 16. │ └─purrr::map(...) #> 17. │ └─pkgdown:::.f(.x[[i]], ...) #> 18. │ └─pkgdown:::data_reference_topic(...) #> 19. │ ├─pkgdown:::as_data(...) #> 20. │ └─pkgdown:::as_data.tag_examples(...) #> 21. │ └─purrr::pmap_chr(...) #> 22. │ └─pkgdown:::.f(...) #> 23. │ ├─withr::with_options(...) #> 24. │ │ └─base::force(code) #> 25. │ └─evaluate::evaluate(code, env, new_device = TRUE) #> 26. │ └─evaluate:::evaluate_call(...) #> 27. │ ├─evaluate:::timing_fn(...) #> 28. │ ├─evaluate:::handle(...) #> 29. │ │ └─base::try(f, silent = TRUE) #> 30. │ │ └─base::tryCatch(...) #> 31. │ │ └─base:::tryCatchList(expr, classes, parentenv, handlers) #> 32. │ │ └─base:::tryCatchOne(expr, names, parentenv, handlers[[1L]]) #> 33. │ │ └─base:::doTryCatch(return(expr), name, parentenv, handler) #> 34. │ ├─base::withCallingHandlers(...) #> 35. │ ├─base::withVisible(eval(expr, envir, enclos)) #> 36. │ └─base::eval(expr, envir, enclos) #> 37. │ └─base::eval(expr, envir, enclos) #> 38. │ ├─base::try(vec_slice(x, 2) <- "20") #> 39. │ │ └─base::tryCatch(...) #> 40. │ │ └─base:::tryCatchList(expr, classes, parentenv, handlers) #> 41. │ │ └─base:::tryCatchOne(expr, names, parentenv, handlers[[1L]]) #> 42. │ │ └─base:::doTryCatch(return(expr), name, parentenv, handler) #> 43. │ └─vctrs::`vec_slice<-`(`*tmp*`, 2, value = "20") #> 44. ├─vctrs:::vec_type2_dispatch(x = x, y = y, x_arg = x_arg, y_arg = y_arg) #> 45. ├─vctrs::vec_ptype2.character(x = x, y = y, x_arg = x_arg, y_arg = y_arg) /home/travis/build/r-lib/vctrs/R/type2.R:46:2 #> 46. └─vctrs:::vec_ptype2.character.default(...) /home/travis/build/r-lib/vctrs/R/type-bare.R:58:24 #> 47. └─vctrs::vec_default_ptype2(x, y, x_arg = x_arg, y_arg = y_arg) /home/travis/build/r-lib/vctrs/R/type-bare.R:129:2 #> 48. └─vctrs::stop_incompatible_type(x, y, x_arg = x_arg, y_arg = y_arg) /home/travis/build/r-lib/vctrs/R/type2.R:63:2 #> 49. └─vctrs:::stop_incompatible(...) /home/travis/build/r-lib/vctrs/R/conditions.R:92:2 #> 50. └─vctrs:::stop_vctrs(...) /home/travis/build/r-lib/vctrs/R/conditions.R:48:2