-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
263 lines (210 loc) · 7.43 KB
/
README.Rmd
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
---
title: "Util_Global.R"
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE}
knitr::opts_chunk$set(collapse = TRUE, comment = "##", fig.retina = 2, fig.path = "README_figs/README-")
```
![GitHub top language](https://img.shields.io/github/languages/top/shringi/util_Global)
![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/shringi/util_Global)
![GitHub](https://img.shields.io/github/license/shringi/util_Global)
![GitHub commit activity](https://img.shields.io/github/commit-activity/y/shringi/util_Global)
![GitHub last commit](https://img.shields.io/github/last-commit/shringi/util_Global)
![GitHub issues](https://img.shields.io/github/issues/shringi/util_Global)
# Motivation
R and its package repository are great. I could find almost all kinds of functions, various tricks and tips online. However, as I worked on R and R studio long enough. I happen to create custom functions which I find it convenient to use for my purpose. I slowly began to realize that I should share some of them with others in case they find it helpful.
# Functions
The following functions are implemented (broad category wise)
### Calculations
- `interpolate`
- `max.adv`
- `min.adv`
- `sum.adv`
- `mov.avg`
- `mov.sd`
- `rm.na`
### Graphics
### Info
### Management
#### Objects
- `clr` : Remove variables from the environment
- `reset`
### Manipulation
### Operators
### Print
- `catn` : cat() function with new line already attached in the end.
- `hr`
- `print.warn`
- `str.list`
### Productivity
### Summary
- `get.empty.columns`: Get empty columns (NA or blank) in a data.frame
- `stat.summary`
- `summary.adv`
- `summary.non.num`
- `summary.num`
- `summaryLM`
# Installation
Just download the **util_Global.R** file.
```{r}
source("util_Global.R")
install("tidyverse")
```
Source it locally and you are good to go.
# Usage
### clr()
clr() is motivated from Matlab function `clr`
Its an advance and short way of writing `rm(list = ls())`
#### syntax
```
clr(mode = "notall", except = NULL)
```
#### usage
```{r}
# To delete all the variables except functions!
clr()
# To delete all the variables except functions!
temp = 1
clr( except = "temp")
```
```{r , eval = FALSE}
# To clean everything except function clr(), and clc()
clr("all")
```
### drop_unfit_cols(), drop_unfit_rows()
Select out columns/rows based on unfit values
#### syntax
```{r, eval=FALSE}
drop_unfit_cols(data, ..., contains = "", na.rm = TRUE)
```
#### Usage
```{r}
df <- tibble(c1 = c("a", "", "c", "d", NA, "f", "g", "h"),
c2 = c(NA, NA, 3, 4, NA, 6, 7, 8),
c3 = c("A", "", "C", "D", NA, "#Value!", "G", "H"),
c4 = c(NA, NA, NA, NA, NA, NA, NA, NA),
c5 = c(0.001, NA, -99, 1, NA, NA, 1000, NA),
c6 = c("", "", "", "", "", "", "", NA),
c7 = c("#Value!", "", "", "#Value!", NA, "#Value!", "", "#Value!"),
c8 = c(-99, NA, -99, NA, NA, NA, -99, NA))
df
# Filterning blank or NA filled columns
df %>% drop_unfit_cols()
# Filterning blank or NA filled rows
df %>% drop_unfit_rows()
# Filterning blank or NA filled columns from a column range
df %>% drop_unfit_cols(c3:c8)
# Filterning blank or NA filled rows from a column range
df %>% drop_unfit_rows(c3:c8)
# Filtering selected or NA filled columns from a column range
df %>% drop_unfit_cols(c3:c8, contains = list("", "#Value!", -99))
# Filtering selected or NA filled rows from a column range
df %>% drop_unfit_rows(c3:c8, contains = list("", "#Value!", -99))
```
### install()
loading packages
every now or then you require a package to run before you can use some R code you found online. Its typically starts with loading a `library()` or `require()`. For example
If "package_name" is already install then you are good to go, however if it is not installed (which is the case sometimes), then you will get an error as following-
```pseudocode
Error in library("package_name") :
there is no package called ‘package_name’
```
Then one needs install it manually either by using RStudio GUI or writing a command such as
```R
install.packages("package_name")
```
#### syntax
```{r, eval = FALSE}
install("tidyverse")
```
### catn()
cat() command is used to display a message on console or in a file. catn() adds a new line in the end for convenience. Also this function adds the functionality of adding the color and an argument to when or when not to print.
#### syntax
```
catn(..., file = "", sep = " ", fill = FALSE, labels = NULL, append = FALSE, console = TRUE, color = NULL)
```
#### usage
```{r}
# Color will only work on console!
catn("A message in blue!", color = "blue")
```
### get.empty.columns()
Get empty columns (NA or blank) in a data.frame
In multiple data set sometimes it happens that a critical column is there in the header name but misses any of the data. Or We might be intrested that which columns are completely empty once we group it. This functions provides a visual summary of such sort. (In the outputs `X` represents an empty column)
#### Usage
```{r}
df
df %>% get.empty.columns()
rbind(df, df) %>% get.empty.columns(data = ., group.cols = c1)
```
### get.source.file.name()
get current active source file name
#### syntax
```
get.source.file.name()
```
#### usage
```{r}
get.source.file.name()
```
### str.list()
str.list() command is used to display an internal structure of a list in a visual tree form on the console.
#### syntax
```
str.list(X, prefix1 = "", prefix2 = "", prefix3 = "", prefix4 = "", a =1)
```
#### usage
```{r}
l = list()
df <- data.frame(a = 1:10, b = 11:20)
l$data$df1 <- df
l$data$df2 <- df
l$plots$figure <- ggplot(df, aes(a, b)) + geom_point()
str.list(l)
```
### store.table()
Command to save csv file and .Rdata with semi-automatic name in a predefined folder.
#### syntax
```
store.table(filename, data, lt, check = T, subfolder = "03-Tables", console = FALSE, fun_family = "csv", envir = rlang::caller_env(), ...)
```
#### usage
```{r}
l = list()
df <- data.frame(a = 1:10, b = 11:20)
l$data$df1 <- df
l$data$df2 <- df
store.table(filename = "test", data = df, lt = l)
# Following files will be stored in the folder named as `03-Folder`
# `01_test_[R].csv` `util_Global.Rdata`
```
### plot.colors()
There are more than 650 color names in R, plot.color() shows which color name appears how.
We can even filter out the colors which has some partial string
#### syntax
```
plot.colors(name = NULL)
```
#### usage
```{r}
plot.colors()
plot.colors("dark")
plot.colors("red")
```
### pad_decimal()
Pads a number or string with characters on left or right side.
#### syntax
```
pad_decimal(3.14, n_left = 3, n_right = 4, char_left = " ", char_right = "0")
```
# Scripts
## **util_Global.R**
This is the main scripts which hosts all the custom functions. A list of function and their usage are mentioned below.
## util_Global_Archived.R
All the functions, which I no longer use, or found a way around are shifted here.
## Snippets.txt
A file containing plain snippets. Copy these snippets, modify and add in your RStudio settings (Tools > Global Options > Code > Editing > Snippets > Edit Snippets button) for handy use. You can find more information about RStudio Snippets from [here](https://support.rstudio.com/hc/en-us/articles/204463668-Code-Snippets).
# Feedback
- Please feel free to raise a issue on github if you are find a bug, typo or suggest a feature.
- If you come across functions which supersedes any of functions presented here then please let me know by raising an issue.