-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
181 lines (134 loc) · 4.38 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
---
output:
github_document:
html_preview: false
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
# fig.path = "README-"
)
options(tibble.print_min = 5, tibble.print_max = 5)
```
### Introduction
This is a collection of useful R code bits I'm tired of looking up.
### Contents
- [Data reading and writing](#data-reading-and-writing)
- [Data manipulation](#data-manipulation)
- [Time series data](#time-series-data)
- [Plots](#plots)
- [Rmarkdown](#rmarkdown)
- [Extra miscellaneous](#extra-misc)
### Data reading and writing
### Data manipulation
Drop columns from data frame by name:
```{r eval=FALSE}
iris[!colnames(iris) %in% c("Sepal.Length","Sepal.Width")]
```
Add `NA` padding to end of a vector:
```{r}
a <- c(1,2,3)
length(a) <- 5 #add 2 NA elements
```
### Time series data
#### Linear interpolation for irregular intervals using `zoo`
```{r eval=FALSE}
library(zoo)
# dataset with irregular time intervals
t_irr <- as.POSIXct(c('2017-01-01T00:00:00S','2017-01-01T00:02:01S','2017-01-01T00:07:32S',
'2017-01-01T00:10:00S'), format = '%Y-%m-%dT%H:%M:%SS')
vals <- c(0,5,9,10)
df1 <- data.frame(t_irr,vals)
# time sequence with regular intervals
t_seq <- zoo(order.by=(as.POSIXct(seq(min(df1$t_irr), max(df1$t_irr), by = '1 min'))))
# merge irregular time series with t_seq
t_merged <- zoo::merge.zoo(zoo(x=df1[,c('vals')], order.by = df1$t_irr), t_seq)
# linear interpolation
t_interpolated <- zoo::na.approx(t_merged)
# discard rows where timestamp is not from t_seq
t_interpolated_2 <- t_interpolated[index(t_interpolated) %in% index(t_seq)]
# convert zoo object to data frame
t_interpolated_3 <- zoo::fortify.zoo(t_interpolated_2)
```
### Plotting
#### Plots with `ggplot2`
Add the following blurb to an R script on Windows to avoid jagged plot lines ([github issue](https://github.com/rstudio/rstudio/issues/2142)):
```{r eval=FALSE}
trace(grDevices:::png, quote({
if (missing(type) && missing(antialias)) {
type <- "cairo-png"
antialias <- "subpixel"
}
}), print = FALSE)
```
#### Plots with base graphics
Four plots in one graph:
```{r eval=FALSE}
par(mfrow=c(2,2))
```
Two series in one graph (same scale):
```{r eval=FALSE, echo=FALSE}
#create dummy time series for plotting
ts_dat <- t_interpolated_3
names(ts_dat) <- c('time','f1')
ts_dat$f2 <- runif(NROW(ts_dat) * (ts_dat$f1)^2)
```
```{r eval=FALSE}
plot(ts_dat$time, ts_dat$f1, col = 'blue', type = 'o', cex = 0.3,
xlab = 'time', ylab = 'f1', cex.lab = 0.8, cex.axis = 0.7)
points(ts_dat$time, ts_dat$f2, col = 'black', type = 'o', cex = 0.3,
title('Plot Title',cex.main = 0.9))
```
Two series in one graph (different scales):
```{r eval=FALSE}
plot(ts_dat$time, ts_dat$f1, col = 'blue', pch = 20, cex = 0.3,
xlab = 'time', ylab = 'f1', cex.lab = 0.8, cex.axis = 0.7)
par(new=TRUE)
plot(ts_dat$time, ts_dat$f2, col = 'red', pch = 19, cex = 0.3,
yaxt = 'n', ylab = NA, xlab = NA, lwd = 1, title('Plot Title',
cex.main = 0.9), cex.lab = 0.7, cex.axis = 0.7)
axis(4, cex.axis = 0.7)
mtext("right y axis", side=4, cex = 0.8)
```
### Rmarkdown
#### template
```{text eval=FALSE}
# ---
# title: "Document Title"
# output:
# html_document:
# theme: cerulean
# highlight: textmate
# fig_width: 8
# fig_height: 5
# toc: true
# ---
#
# ```{r setup, include=FALSE}
# knitr::opts_chunk$set(echo = TRUE)
# knitr::opts_knit$set(root.dir = "some/directory")
# ```
```
#### `theme`
Themes: "default", "cerulean", "journal", "flatly", "readable", "spacelab", "united", "cosmo", "lumen", "paper", "sandstone", "simplex", "yeti". Pass null for no theme (in this case you can use the css parameter to add your own styles).
#### `highlight`
Styles: "default", "tango", "pygments", "kate", "monochrome", "espresso", "zenburn", "haddock", "textmate". Pass null to prevent syntax highlighting.
[More info](https://rmarkdown.rstudio.com/html_document_format.html)
#### LaTeX symbols
- [List of LaTeX mathematical symbols](https://oeis.org/wiki/List_of_LaTeX_mathematical_symbols)
- [R markdown math symbols](https://francoisbirgand.github.io/RMarkdown_instructions.html)
### Extra misc
Set language to English:
```{r eval=FALSE}
Sys.setenv(LANG='en')
```
Wait for user input:
```{r}
readline(prompt = 'press [enter]...')
```
Print more digits for a number:
```{r}
sprintf("%.12f",1.234343)
```