sqlanalogs: SQL Analog to left & right substring functions

Description Usage Arguments right left Examples

Description

Vectorized wrapper for taking the left/right N characters from a string, number, factor, etc., just like in SQL. I find myself constantly trimming strings and grabbing only the first/last few characters so this saves some typing.

Usage

1
2
3
right(vec, n, trimws = TRUE, sameclass = FALSE)

left(vec, n, trimws = TRUE, sameclass = FALSE)

Arguments

vec

A vector of any datatype: character, numeric, logical, factor, etc.

n

Integer scalar. The number of characters you want to keep.

trimws

Should the applicable leading/trailing whitespace be removed first? Default is TRUE.

sameclass

Should the output be the same class as the input? Defaults to FALSE (returns a character vector no matter the input). Generally, you don't want this (expecially for POSIX classes which is sketchy at best).

right

R Analog to SQL's RIGHT() function. Defaults to trim leading whitespace.

left

R Analog to SQL's LEFT() function. Defaults to trim trailing whitespace.

Examples

1
2
3
4
5
6
7
8
9
right(vec = 'SomethingLong', n = 4)
right(vec = 425575.4, n = 5)
right(vec = 'AnotherThing  ', n = 7, trimws = FALSE)
right(vec = 401.98, 4, sameclass = TRUE)

left(vec = 'SomethingLong', n = 4)
left(vec = 40000.00, n = 2)
left(vec = '  AnotherThing', n = 4, trimws = FALSE)
left(vec = 400, 1, sameclass = TRUE)

Paul-James/pjames documentation built on Aug. 9, 2019, 12:18 p.m.