[Experimental]

cutter() cut vectors into pieces by cutting points/indexes.

cutter(x, index, between = NULL, rm_start = FALSE, rm_end = FALSE)

Arguments

x

An atomic vector (e.g., character, integer, numeric, factor, POSIXct).

index

An integerish numeric object or an integer object with the indexes/cutting points.

between

(optional) A string object indicating the direction of the cut (choices: "left", "right"). This argument only need to be assigned if the cut must be performed between the indexes values (default: NULL).

rm_start

(optional) a logical value indicating if the start element of the cut must be removed (default: FALSE).

rm_end

(optional) a logical value indicating if the end element of the cut must be removed (default: FALSE).

Value

A list object with the cut pieces as elements.

Details

cutter() can perform different kinds of cuts. Here are some examples.

Cutting by index values

cutter(seq(10), c(3, 9))

   cut         cut
    |           |
1 2 3 4 5 6 7 8 9 10

Element 1: 1, 2
Element 2: 4, 5, 6, 7, 8
Element 3: 10

Cutting between index values

cutter(seq(10), c(3, 9), between = "left")

  cut         cut
   |           |
1 2 3 4 5 6 7 8 9 10

Element 1: 1, 2
Element 2: 3, 4, 5, 6, 7, 8
Element 3: 9, 10

cutter(seq(10), c(3, 9), between = "right")

    cut         cut
     |           |
1 2 3 4 5 6 7 8 9 10

Element 1: 1, 2, 3
Element 2: 4, 5, 6, 7, 8, 9
Element 3: 10

Removing tips

cutter(seq(20), c(7, 16), rm_start = TRUE, rm_end = TRUE)

           cut                      cut
            |                        |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|---------|                            |---------|
 start tip                               end tip

Element 1: 8, 9, 10, 11, 12, 13, 14, 15

See also

Other string functions: replace_pattern(), split_by_pattern()

Examples

## Cutting by index values

cutter(seq(10), c(3, 9))
#> [[1]]
#> [1] 1 2
#> 
#> [[2]]
#> [1] 4 5 6 7 8
#> 
#> [[3]]
#> [1] 10
#> 

## Cutting between index values

cutter(seq(10), c(3, 9), between = "left")
#> [[1]]
#> [1] 1 2
#> 
#> [[2]]
#> [1] 3 4 5 6 7 8
#> 
#> [[3]]
#> [1]  9 10
#> 

cutter(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

cutter(seq(10), c(3, 9), rm_start = TRUE)
#> [[1]]
#> [1] 4 5 6 7 8
#> 
#> [[2]]
#> [1] 10
#> 

cutter(seq(10), c(3, 9), rm_end = TRUE)
#> [[1]]
#> [1] 1 2
#> 
#> [[2]]
#> [1] 4 5 6 7 8
#>