-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathREADME.Rmd
143 lines (103 loc) · 3.53 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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
set.seed(42)
```
# mark
<!-- badges: start -->
[![CRAN status](https://www.r-pkg.org/badges/version/mark)](https://CRAN.R-project.org/package=mark)
[![R-CMD-check](https://github.com/jmbarbone/mark/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/jmbarbone/mark/actions/workflows/R-CMD-check.yaml)
[![Codecov test coverage](https://codecov.io/gh/jmbarbone/mark/graph/badge.svg)](https://app.codecov.io/gh/jmbarbone/mark)
<!-- badges: end -->
Miscellaneous, Analytic R Kernels
An R package with a set of general use functions for data analytics.
This is developed mostly for personal use and has no real _goal_ other than to limit the time I spend searching where I did that thing that I think I could use again because it worked well but this problem might be slightly different and I know I had to change it before.
Some parts happily ripped from and (hopefully) credited to others.
## Installation
You can download the current CRAN version with:
```r
install.packages("mark")
```
You can the development version from [GitHub](https://github.com/jmbarbone/mark) with:
```r
remotes::install_github("jmbarbone/mark")
```
## Select examples
This package contains a many variety of functions, some useful, some not so much.
Below are a selection of a few functions that could potential be useful for others:
```{r library}
library(mark)
```
Get dates from sloppy entries:
```{r dates}
bad_dates <- c("2020 Dec 8th", "1970 May", "??", "1984 UNK UN")
date_from_partial(bad_dates)
date_from_partial(bad_dates, method = "max")
date_from_partial(c("May 2000", "08Dec2020"), format = "dmy")
```
Slice strings:
```{r strings}
x <- stringi::stri_rand_lipsum(1)
str_slice(x, n = 50L)
str_slice_by_word(x)
```
Read in bibliographies:
```{r bibs}
file <- system.file("extdata", "example-bib.txt", package = "mark")
bib <- read_bib(file)
tibble::as_tibble(bib)
```
More matching:
```{r matching}
1:10 %out% c(1, 3, 5, 9) # opposite of %in%
letters[1:5] %wo% letters[3:7]
letters[1:5] %wi% letters[3:7]
```
Small functions for working with data.frames:
```{r dataframes}
x <- list(a = 1:5, b = letters[1:5])
quick_df(x)
vector2df(x[["b"]], name = NULL)
quick_dfl(a = 1:3, b = list(1:5, 6:10, 11:15))
```
Counts and proportions:
```{r counts}
set.seed(42)
x <- sample(1:5, 20, TRUE, 5:1/2)
counts(x)
props(x)
df <- as.data.frame(matrix(sample(1:2, 60, TRUE), byrow = TRUE, ncol = 3))
counts(df, c("V1", "V2"))
props(df, 1:3)
```
Date time differences:
```{r diff_time}
x <- as.POSIXlt("2021-02-13 05:02:30", tz = "America/New_York") + c(0, -1, 2) * 3600 * 24
y <- as.POSIXlt("2020-02-13 05:02:30", tz = "America/New_York") + c(0, -2, 4) * 3600 * 24
# comparison with base::difftime() (note the order of x and y)
difftime(y, x, units = "days")
diff_time_days(x, y)
difftime(y, x, units = "secs")
diff_time_secs(x, y)
# Year (by days, months, etc)
diff_time_years(x, y)
diff_time_myears(x, y)
# Set time zones
diff_time_hours(x, y, "GMT", "America/New_York")
diff_time_hours(x, x, "GMT", c("America/Los_Angeles", "America/New_York", "Europe/London")) # note x, x
diff_time_days(x, y, NULL, 31536000)
```
Simple factors:
```{r}
fact(c("a", "c", NA, "a", "b", NA, "a", "c")) # no sorting
fact(c(-1, 5, 2, NA, 3)) # sorting
fact(c(NA, FALSE, TRUE, FALSE, TRUE, NA)) # fixed
```