cut_vector()
splits a vector into segments at specified cutting points
or indexes. You can define the cuts either by providing explicit index
positions or by specifying the number of segments.
Additional options allow you to control whether cuts occur between or at index values, and whether to remove elements at the start or end of the resulting segments.
Usage
cut_vector(
x,
index = NULL,
n = NULL,
between = NULL,
rm_start = FALSE,
rm_end = FALSE,
round_fun = floor
)
Arguments
- x
An atomic vector.
- index
(optional) An integerish
numeric
object with the indexes/cutting points (default:NULL
).- n
(optional) An integerish number specifying the number of segments to cut the vector into. If
n
is specified, theindex
argument is ignored (default:NULL
).- between
(optional) A string specifying the direction of the cut:
"left"
or"right"
. Use this argument only when the cut should be performed between the index values (default:NULL
).- rm_start
(optional) a
logical
flag indicating if the start element of the cut must be removed (default:FALSE
).- rm_end
(optional) a
logical
flag indicating if the end element of the cut must be removed (default:FALSE
).- round_fun
(optional) A function to round the number of elements in when cutting using
n
(default:floor
).
Value
A list
object with the cut pieces as elements.
Details
cut_vector()
can perform different kinds of cuts. Here are some examples.
Cutting between index values
See also
Other vector functions:
remove_caps()
,
split_by_pattern()
Examples
## Cutting by index values
cut_vector(seq(10), c(3, 9))
#> [[1]]
#> [1] 1 2
#>
#> [[2]]
#> [1] 4 5 6 7 8
#>
#> [[3]]
#> [1] 10
#>
## Cutting between index values
cut_vector(seq(10), c(3, 9), between = "left")
#> [[1]]
#> [1] 1 2
#>
#> [[2]]
#> [1] 3 4 5 6 7 8
#>
#> [[3]]
#> [1] 9 10
#>
cut_vector(seq(10), c(3, 9), between = "right")
#> [[1]]
#> [1] 1 2 3
#>
#> [[2]]
#> [1] 4 5 6 7 8 9
#>
#> [[3]]
#> [1] 10
#>
## Removing start or end tips
cut_vector(seq(10), c(3, 9), rm_start = TRUE)
#> [[1]]
#> [1] 4 5 6 7 8
#>
#> [[2]]
#> [1] 10
#>
cut_vector(seq(10), c(3, 9), rm_end = TRUE)
#> [[1]]
#> [1] 1 2
#>
#> [[2]]
#> [1] 4 5 6 7 8
#>