README.md

tbTools

Tomas' personal mix of utilities

Mix of things that I missed in R. Matlab-like colon operator, stem plot (base plotting system), round2 with order, ifft etc.

seqM Matlab-like behaviour of colon operator or linspace for creating sequences, for-loop friendly.

round2 Rounds a number to the specified order. Round half away from zero (this is the difference from built-in \code{round} function.)

ifft Inverse Fast Fourier Transform (discrete FT), Matlab-like behavior.

Stem Matlab-like stem plotting function for discrete series.

isInt Returns TRUE / FALSE whether it is exactly 1 integer number (in fact, the class can be numeric but the number must be integer), non-missing

isNum Returns TRUE / FALSE whether it is exactly 1 number (numeric or integer vector of length 1, non-missing)

isString Returns TRUE / FALSE whether it is exactly 1 character string (character vector of length 1, non-missing)

isLogical Returns TRUE / FALSE whether it is exactly 1 logical value (logical vector of length 1, non-missing)

strTrim Trim leading and trailing whitespace in character string. Way faster than str_trim() or trimws().

str_contains Find string in another string (without regular expressions), returns TRUE / FALSE.

str_find Find string in another string (without regular expressions), returns indices of all occurences.

str_find1 Find string in another string (without regular expressions), returns indices of the first occurence only.

Installation

install.packages("devtools")
devtools::install_github("bbTomas/tbTools")

Documentation

Reference manual: tbTools.pdf.

Examples

Finally Matlab-like sequences in R (colon operator with by or linspace with length.out). For-loop safe. Default step is always by=+1 (no guessing) and if you do seqM(3, 1, by=+1) it produces an empty vector (so in for-loop, the statements are not proceeded) instead of error (result of classic seq in R).

library(tbTools)

seqM

seqM(1, 3)
## [1] 1 2 3
seqM(1, 3, by=.8)
## [1] 1.0 1.8 2.6
seqM(1, 3, by=5)
## [1] 1
seqM(3, 1)
## integer(0)
seqM(3, 1, by=+1)
## integer(0)
seqM(3, 1, by=-1)
## [1] 3 2 1
seqM(3, 1, by=-3)
## [1] 3
seqM(1, 3, len=5)
## [1] 1.0 1.5 2.0 2.5 3.0
seqM(1, 3, len=3)
## [1] 1 2 3
seqM(1, 3, len=2)
## [1] 1 3
seqM(1, 3, len=1)
## [1] 3
seqM(1, 3, len=0)
## Warning in seqM(1, 3, len = 0): length.out == 0, return empty vector
## integer(0)
seqM(3, 1, len=3)
## [1] 3 2 1
seqM(from=2, by=1, len=3)
## [1] 2 3 4
seqM(from=2, by=-1, len=3)
## [1] 2 1 0
seqM(to=2, by=1, len=3)
## [1] 0 1 2
seqM(to=2, by=-1, len=3)
## [1] 4 3 2
seqM(from=2, by=0, len=3)
## [1] 2 2 2

ifft

ifft(fft(1:5))
## [1] 1+0i 2+0i 3+0i 4+0i 5+0i

isSomething

isInt(2)
## [1] TRUE
isInt(2L)
## [1] TRUE
isInt(2.1)
## [1] FALSE
isInt(1:5)
## [1] FALSE
isNum(2L)
## [1] TRUE
isNum(-2.1)
## [1] TRUE
isNum("-2.1")
## [1] FALSE
isString("hello")
## [1] TRUE
isString(5)
## [1] FALSE
isLogical(FALSE)
## [1] TRUE
isLogical(0)
## [1] FALSE

round2

round2(23.5)
## [1] 24
round2(23.4)
## [1] 23
round2(24.5)
## [1] 25
round2(-23.5)
## [1] -24
round2(-23.4)
## [1] -23
round2(-24.5)
## [1] -25
round2(123.456, -1) 
## [1] 123.5
round2(123.456, -2) 
## [1] 123.46
round2(123.456, 1)
## [1] 120
round2(123.456, 2)
## [1] 100
round2(123.456, 3)
## [1] 0

Stem: Matlab-like stem plot for discrete series in R

t <- seqM(from = 0, to = 2*pi, length.out = 20)
Stem(t, sin(t))

Stem(t, sin(t), pch=21, linecol = "blue")

Character string operations

strTrim(" Hello World! ")   # much faster than str_trim() in stringr package or trimws() in R3.2.0
## [1] "Hello World!"
# easy and comfortable string operations without regular expressions
str_contains("Hello world", "wor")
## [1] TRUE
str_contains("Hello world", "WOR")
## [1] FALSE
str_contains(tolower("Hello world"), tolower("wor"))
## [1] TRUE
str_find("Hello, hello, hello world", "ell")
## [1]  2  9 16
str_find("Hello, hello, hello world", "q")
## integer(0)
str_find1("Hello, hello, hello world", "ell")
## [1] 2
str_find1("Hello, hello, hello world", "q")
## integer(0)


bbTomas/tbTools documentation built on May 11, 2019, 9:25 p.m.