extractors: Extract vectors by index or value occurrence

extractorsR Documentation

Extract vectors by index or value occurrence

Description

Extract front or rear of vector 'x' up and untill an index 'i', the first or last occurence of a value 'v', or, extract values based on quantile 'q', first, middle, or, last index 'j'.

Usage

x %[f% v

x %[l% v

x %[% i

x %]% i

x %f]% v

x %l]% v

x %q]% q

x %q)% q

x %[q% q

x %(q% q

x %:% j

Arguments

x

A vector

v

A value of which the first or last occurrence in x will be used as an index

i

An index or two element vector c(lo,hi) indicating a range to extract

q

A percentile value (between '0' and '1')

j

A character indicating to extract the first 'f', middle 'm' or last 'l' value of 'x'.

Value

A vector extracted from the front, rear, or, range of 'x'. Either based on an index or the first or last occurrence of a value or the first, middle, or, ;ast element of a vector.

Note

The function provided for symmetry, character lengths of x%]%i and x[1:i] are equal.

Examples


z <- letters

# Extract front by first occurrence of value
z %[f% "n"

# Extract front by index
x <- rnorm(100)
x %[% 10

# Extract rear by index
x %]% 90

# Extract rear by index
x %]% 90

# Extract by indices if a range is provided
x %]% c(4,30)
z %[% c(6,10)

# Extract last/middle value of x
x %:% "l"
z %:% "m"

# Extract by percentile
seq(1,10,.5) %(q% .5 # infix
seq(1,10,.5)[seq(1,10,.5) < quantile(seq(1,10,.5),.5)] # regular syntax

seq(1,10,.5) %q]% .5 # infix
seq(1,10,.5)[seq(1,10,.5) >= quantile(seq(1,10,.5),.5)] # regular syntax



invctr documentation built on Aug. 16, 2022, 5:18 p.m.