{"id":16735150,"url":"https://github.com/dbolotov/r-miscellany","last_synced_at":"2025-03-15T20:45:15.099Z","repository":{"id":147042164,"uuid":"123683304","full_name":"dbolotov/r-miscellany","owner":"dbolotov","description":"Miscellaneous R code bits.","archived":false,"fork":false,"pushed_at":"2020-06-02T02:00:39.000Z","size":26,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-22T09:52:46.913Z","etag":null,"topics":["data-science","r"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"cc-by-4.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dbolotov.png","metadata":{"files":{"readme":"README.Rmd","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-03-03T10:50:51.000Z","updated_at":"2020-06-02T02:00:41.000Z","dependencies_parsed_at":"2023-06-04T03:45:10.437Z","dependency_job_id":null,"html_url":"https://github.com/dbolotov/r-miscellany","commit_stats":{"total_commits":25,"total_committers":1,"mean_commits":25.0,"dds":0.0,"last_synced_commit":"6e73473d54b206ffaf84587d30a0d0ffe8fdd7d8"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dbolotov%2Fr-miscellany","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dbolotov%2Fr-miscellany/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dbolotov%2Fr-miscellany/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dbolotov%2Fr-miscellany/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dbolotov","download_url":"https://codeload.github.com/dbolotov/r-miscellany/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243790944,"owners_count":20348378,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["data-science","r"],"created_at":"2024-10-13T00:05:06.844Z","updated_at":"2025-03-15T20:45:15.073Z","avatar_url":"https://github.com/dbolotov.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"---\noutput:\n  github_document:\n    html_preview: false\n---\n\n\u003c!-- README.md is generated from README.Rmd. Please edit that file --\u003e\n\n\n```{r, echo = FALSE}\nknitr::opts_chunk$set(\n  collapse = TRUE,\n  comment = \"#\u003e\"\n  # fig.path = \"README-\"\n)\noptions(tibble.print_min = 5, tibble.print_max = 5)\n```\n\n\n### Introduction\n\nThis is a collection of useful R code bits I'm tired of looking up.\n\n### Contents\n- [Data reading and writing](#data-reading-and-writing)\n- [Data manipulation](#data-manipulation)\n- [Time series data](#time-series-data)\n- [Plots](#plots)\n- [Rmarkdown](#rmarkdown)\n- [Extra miscellaneous](#extra-misc)\n\n### Data reading and writing\n\n### Data manipulation\n\nDrop columns from data frame by name:\n```{r eval=FALSE}\niris[!colnames(iris) %in% c(\"Sepal.Length\",\"Sepal.Width\")]\n```\n\nAdd `NA` padding to end of a vector:\n```{r}\na \u003c- c(1,2,3)\nlength(a) \u003c- 5 #add 2 NA elements\n```\n\n\n\n### Time series data\n\n#### Linear interpolation for irregular intervals using `zoo`\n```{r eval=FALSE}\nlibrary(zoo)\n\n# dataset with irregular time intervals\nt_irr \u003c- as.POSIXct(c('2017-01-01T00:00:00S','2017-01-01T00:02:01S','2017-01-01T00:07:32S',\n                      '2017-01-01T00:10:00S'), format = '%Y-%m-%dT%H:%M:%SS')\nvals \u003c- c(0,5,9,10)\ndf1 \u003c- data.frame(t_irr,vals)\n\n# time sequence with regular intervals\nt_seq \u003c- zoo(order.by=(as.POSIXct(seq(min(df1$t_irr), max(df1$t_irr), by = '1 min'))))\n\n# merge irregular time series with t_seq\nt_merged \u003c- zoo::merge.zoo(zoo(x=df1[,c('vals')], order.by = df1$t_irr), t_seq)\n\n# linear interpolation\nt_interpolated \u003c- zoo::na.approx(t_merged)\n\n# discard rows where timestamp is not from t_seq\nt_interpolated_2 \u003c- t_interpolated[index(t_interpolated) %in% index(t_seq)]\n\n# convert zoo object to data frame\nt_interpolated_3 \u003c- zoo::fortify.zoo(t_interpolated_2)\n```\n\n\n### Plotting\n\n#### Plots with `ggplot2`\n\nAdd the following blurb to an R script on Windows to avoid jagged plot lines ([github issue](https://github.com/rstudio/rstudio/issues/2142)):\n\n```{r eval=FALSE}\ntrace(grDevices:::png, quote({\n  if (missing(type) \u0026\u0026 missing(antialias)) {\n    type \u003c- \"cairo-png\"\n    antialias \u003c- \"subpixel\"\n  }\n}), print = FALSE)\n```\n\n#### Plots with base graphics\n\nFour plots in one graph:\n```{r eval=FALSE}\npar(mfrow=c(2,2))\n```\n\nTwo series in one graph (same scale):\n```{r eval=FALSE, echo=FALSE}\n#create dummy time series for plotting\nts_dat \u003c- t_interpolated_3\nnames(ts_dat) \u003c- c('time','f1')\nts_dat$f2 \u003c- runif(NROW(ts_dat) * (ts_dat$f1)^2)\n```\n\n```{r eval=FALSE}\nplot(ts_dat$time, ts_dat$f1, col = 'blue', type = 'o', cex = 0.3,\n     xlab = 'time', ylab = 'f1', cex.lab = 0.8, cex.axis = 0.7)\npoints(ts_dat$time, ts_dat$f2, col = 'black', type = 'o', cex = 0.3,\n     title('Plot Title',cex.main = 0.9))\n```\nTwo series in one graph (different scales):\n```{r eval=FALSE}\nplot(ts_dat$time, ts_dat$f1, col = 'blue', pch = 20, cex = 0.3,\n     xlab = 'time', ylab = 'f1', cex.lab = 0.8, cex.axis = 0.7)\npar(new=TRUE)\nplot(ts_dat$time, ts_dat$f2, col = 'red', pch = 19, cex = 0.3,\n     yaxt = 'n', ylab = NA, xlab = NA, lwd = 1, title('Plot Title',\n     cex.main = 0.9), cex.lab = 0.7, cex.axis = 0.7)\naxis(4, cex.axis = 0.7)\nmtext(\"right y axis\", side=4, cex = 0.8)\n```\n\n\n### Rmarkdown\n\n#### template\n\n```{text eval=FALSE}\n# ---\n# title: \"Document Title\"\n# output: \n#   html_document:\n#     theme: cerulean\n#     highlight: textmate\n#     fig_width: 8\n#     fig_height: 5\n#     toc: true\n# ---\n# \n# ```{r setup, include=FALSE}\n# knitr::opts_chunk$set(echo = TRUE)\n# knitr::opts_knit$set(root.dir = \"some/directory\")\n# ```\n\n```\n\n#### `theme`\n\nThemes: \"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).\n\n#### `highlight`\n\nStyles: \"default\", \"tango\", \"pygments\", \"kate\", \"monochrome\", \"espresso\", \"zenburn\", \"haddock\", \"textmate\". Pass null to prevent syntax highlighting.\n\n[More info](https://rmarkdown.rstudio.com/html_document_format.html)\n\n#### LaTeX symbols\n- [List of LaTeX mathematical symbols](https://oeis.org/wiki/List_of_LaTeX_mathematical_symbols)\n- [R markdown math symbols](https://francoisbirgand.github.io/RMarkdown_instructions.html)\n\n### Extra misc\n\nSet language to English:\n```{r eval=FALSE}\nSys.setenv(LANG='en')\n```\n\nWait for user input:\n```{r}\nreadline(prompt = 'press [enter]...')\n```\n\nPrint more digits for a number:\n```{r}\nsprintf(\"%.12f\",1.234343)\n```\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdbolotov%2Fr-miscellany","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdbolotov%2Fr-miscellany","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdbolotov%2Fr-miscellany/lists"}