-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME.Rmd
More file actions
134 lines (103 loc) · 3.46 KB
/
Copy pathREADME.Rmd
File metadata and controls
134 lines (103 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
---
output:
html_document:
keep_md: yes
---
# 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
```{r eval=FALSE}
install.packages("devtools")
devtools::install_github("bbTomas/tbTools")
```
### Documentation
Reference manual: [tbTools.pdf](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).
```{r}
library(tbTools)
```
#### seqM
```{r}
seqM(1, 3)
seqM(1, 3, by=.8)
seqM(1, 3, by=5)
seqM(3, 1)
seqM(3, 1, by=+1)
seqM(3, 1, by=-1)
seqM(3, 1, by=-3)
seqM(1, 3, len=5)
seqM(1, 3, len=3)
seqM(1, 3, len=2)
seqM(1, 3, len=1)
seqM(1, 3, len=0)
seqM(3, 1, len=3)
seqM(from=2, by=1, len=3)
seqM(from=2, by=-1, len=3)
seqM(to=2, by=1, len=3)
seqM(to=2, by=-1, len=3)
seqM(from=2, by=0, len=3)
```
#### ifft
```{r}
ifft(fft(1:5))
```
#### isSomething
```{r}
isInt(2)
isInt(2L)
isInt(2.1)
isInt(1:5)
isNum(2L)
isNum(-2.1)
isNum("-2.1")
isString("hello")
isString(5)
isLogical(FALSE)
isLogical(0)
```
#### round2
```{r}
round2(23.5)
round2(23.4)
round2(24.5)
round2(-23.5)
round2(-23.4)
round2(-24.5)
round2(123.456, -1)
round2(123.456, -2)
round2(123.456, 1)
round2(123.456, 2)
round2(123.456, 3)
```
#### Stem: Matlab-like stem plot for discrete series in R
```{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
```{r}
strTrim(" Hello World! ") # much faster than str_trim() in stringr package or trimws() in R3.2.0
# easy and comfortable string operations without regular expressions
str_contains("Hello world", "wor")
str_contains("Hello world", "WOR")
str_contains(tolower("Hello world"), tolower("wor"))
str_find("Hello, hello, hello world", "ell")
str_find("Hello, hello, hello world", "q")
str_find1("Hello, hello, hello world", "ell")
str_find1("Hello, hello, hello world", "q")
```