bc.str

Broadcasted String Operations

Description

The bc.str() function performs broadcasted string operations on pairs of arrays.

Usage

bc.str(x, y, op)

Arguments

x, y conformable atomic arrays of type character.
op a single string, giving the operator.
Supported concatenation operators: +.
Supported relational operators: ==, !=.
Supported distance operators: levenshtein.

Value

For concatenation operation:
A character array as a result of the broadcasted concatenation operation.

For relational operation:
A logical array as a result of the broadcasted relational comparison.

For distance operation:
An integer array as a result of the broadcasted distance measurement.

References

The ‘C++’ code for the Levenshtein edit string distance is based on the code found in https://rosettacode.org/wiki/Levenshtein_distance#C++

Examples

library("broadcast")


# string concatenation:
x <- array(letters, c(10, 2, 1))
y <- array(letters, c(10,1,1))
bc.str(x, y, "+")
## , , 1
## 
##       [,1] [,2]
##  [1,] "aa" "ka"
##  [2,] "bb" "lb"
##  [3,] "cc" "mc"
##  [4,] "dd" "nd"
##  [5,] "ee" "oe"
##  [6,] "ff" "pf"
##  [7,] "gg" "qg"
##  [8,] "hh" "rh"
##  [9,] "ii" "si"
## [10,] "jj" "tj"


# string (in)equality:
bc.str(array(letters), array(letters), "==")
##  [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [16] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
bc.str(array(letters), array(letters), "!=")
##  [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [25] FALSE FALSE


# string distance (Levenshtein):
x <- array(month.name, c(12, 1))
y <- array(month.abb, c(1, 12))
out <- bc.str(x, y, "levenshtein")
dimnames(out) <- list(month.name, month.abb)
print(out)
##           Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
## January     4   7   5   6   5   5   5   6   7   7   7   7
## February    7   5   6   7   6   7   7   7   7   8   8   7
## March       4   5   2   4   3   5   5   5   5   4   5   4
## April       5   5   4   2   5   5   4   4   5   5   5   5
## May         2   3   1   3   0   3   3   3   3   3   3   3
## June        2   4   4   4   4   1   2   3   4   4   4   4
## July        3   4   4   4   3   2   1   3   4   4   4   4
## August      6   6   6   5   6   5   5   3   6   5   6   6
## September   9   7   8   7   9   9   9   9   6   8   9   8
## October     7   6   6   6   7   7   7   7   6   4   6   6
## November    8   6   7   7   8   8   8   8   7   8   5   7
## December    8   6   7   7   8   8   8   8   7   7   8   5