An open API service indexing awesome lists of open source software.

https://github.com/dbolotov/r-miscellany

Miscellaneous R code bits.
https://github.com/dbolotov/r-miscellany

data-science r

Last synced: over 1 year ago
JSON representation

Miscellaneous R code bits.

Awesome Lists containing this project

README

          

---
output:
github_document:
html_preview: false
---

```{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)
```