Skip to content

dbolotov/r-miscellany

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Introduction

This is a collection of useful R code bits I’m tired of looking up.

Contents

Data reading and writing

Data manipulation

Drop columns from data frame by name:

iris[!colnames(iris) %in% c("Sepal.Length","Sepal.Width")]

Add NA padding to end of a vector:

a <- c(1,2,3)
length(a) <- 5 #add 2 NA elements

Time series data

Linear interpolation for irregular intervals using zoo

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):

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:

par(mfrow=c(2,2))

Two series in one graph (same scale):

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):

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

# ---
# 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

LaTeX symbols

Extra misc

Set language to English:

Sys.setenv(LANG='en')

Wait for user input:

readline(prompt = 'press [enter]...')
#> press [enter]...
#> [1] ""

Print more digits for a number:

sprintf("%.12f",1.234343)
#> [1] "1.234343000000"

About

Miscellaneous R code bits.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published