get_bracket <- function(x, i, value) {
x[i]
}
get_vec_slice <- function(x, i, value) {
vec_slice(x, i)
}
assign_bracket <- function(x, i, value) {
x[i] <- value
x
}
assign_vec_slice <- function(x, i, value) {
vec_slice(x, i) <- value
x
}NAx <- 1:3
get_bracket(x, 2)
#> [1] 2
get_vec_slice(x, 2)
#> [1] 2
get_bracket(x, 0)
#> integer(0)
get_vec_slice(x, 0)
#> integer(0)
get_bracket(x, 4)
#> [1] NA
get_vec_slice(x, 4)
#> Error: Can't index beyond the end of a vector.
#> The vector has length 3 and you've tried to subset element 4.
get_bracket(x, NA_integer_)
#> [1] NA
get_vec_slice(x, NA_integer_)
#> [1] NA
assign_bracket(x, 2, 4L)
#> [1] 1 4 3
assign_vec_slice(x, 2, 4L)
#> [1] 1 4 3
assign_bracket(x, 0, 4L)
#> [1] 1 2 3
assign_vec_slice(x, 0, 4L)
#> [1] 1 2 3
assign_bracket(x, 4, 4L)
#> [1] 1 2 3 4
assign_vec_slice(x, 4, 4L)
#> Error: Can't index beyond the end of a vector.
#> The vector has length 3 and you've tried to subset element 4.
assign_bracket(x, NA_integer_, 4L)
#> [1] 1 2 3
assign_vec_slice(x, NA_integer_, 4L)
#> [1] 1 2 3NAx <- setNames(1:3, c("one", "two", "three"))
get_bracket(x, "two")
#> two
#> 2
get_vec_slice(x, "two")
#> two
#> 2
get_bracket(x, "zero")
#> <NA>
#> NA
get_vec_slice(x, "zero")
#> Error: Can't index non-existing elements.
get_bracket(x, "th")
#> <NA>
#> NA
get_vec_slice(x, "th")
#> Error: Can't index non-existing elements.
get_bracket(x, "t")
#> <NA>
#> NA
get_vec_slice(x, "t")
#> Error: Can't index non-existing elements.
get_bracket(x, NA_character_)
#> <NA>
#> NA
get_vec_slice(x, NA_character_)
#> Error: Can't index non-existing elements.
assign_bracket(x, "two", 4L)
#> one two three
#> 1 4 3
assign_vec_slice(x, "two", 4L)
#> one two three
#> 1 4 3
assign_bracket(x, "zero", 4L)
#> one two three zero
#> 1 2 3 4
assign_vec_slice(x, "zero", 4L)
#> Error: Can't index non-existing elements.
assign_bracket(x, "th", 4L)
#> one two three th
#> 1 2 3 4
assign_vec_slice(x, "th", 4L)
#> Error: Can't index non-existing elements.
assign_bracket(x, "t", 4L)
#> one two three t
#> 1 2 3 4
assign_vec_slice(x, "t", 4L)
#> Error: Can't index non-existing elements.
assign_bracket(x, NA_character_, 4L)
#> one two three <NA>
#> 1 2 3 4
assign_vec_slice(x, NA_character_, 4L)
#> Error: Can't index non-existing elements.NAx <- 1:3
get_bracket(x, c(FALSE, TRUE, FALSE, TRUE))
#> [1] 2 NA
get_vec_slice(x, c(FALSE, TRUE, FALSE, TRUE))
#> Error: Logical indices must have length 1 or be as long as the indexed vector.
#> The vector has size 4 whereas the index has size 3.
get_bracket(x, c(FALSE, TRUE))
#> [1] 2
get_vec_slice(x, c(FALSE, TRUE))
#> Error: Logical indices must have length 1 or be as long as the indexed vector.
#> The vector has size 2 whereas the index has size 3.
get_bracket(x, c(NA, NA, NA))
#> [1] NA NA NA
get_vec_slice(x, c(NA, NA, NA))
#> [1] NA NA NA
get_bracket(x, NA)
#> [1] NA NA NA
get_vec_slice(x, NA)
#> [1] NA NA NA
assign_bracket(x, c(FALSE, TRUE, FALSE, TRUE), 4L)
#> [1] 1 4 3 4
assign_vec_slice(x, c(FALSE, TRUE, FALSE, TRUE), 4L)
#> Error: Logical indices must have length 1 or be as long as the indexed vector.
#> The vector has size 4 whereas the index has size 3.
assign_bracket(x, c(FALSE, TRUE), 4L)
#> [1] 1 4 3
assign_vec_slice(x, c(FALSE, TRUE), 4L)
#> Error: Logical indices must have length 1 or be as long as the indexed vector.
#> The vector has size 2 whereas the index has size 3.
assign_bracket(x, c(NA, NA, NA), 4L)
#> [1] 1 2 3
assign_vec_slice(x, c(NA, NA, NA), 4L)
#> [1] 1 2 3
assign_bracket(x, NA, 4L)
#> [1] 1 2 3
assign_vec_slice(x, NA, 4L)
#> [1] 1 2 3