Skip to content

Instantly share code, notes, and snippets.

View maelle's full-sized avatar

Maëlle Salmon maelle

View GitHub Profile
#' Convert a list of vectors to a data frame.
#'
#' This function will convert a list of vectors to a data frame. This function
#' will handle three different types of lists of vectors. First, if all the elements
#' in the list are named vectors, the resulting data frame will have have a number
#' of columns equal to the number of unique names across all vectors. In cases
#' where some vectors do not have names in other vectors, those values will be
#' filled with \code{NA}.
#'
#' The second case is when all the vectors are of the same length. In this case,
@stephenhardy
stephenhardy / git-clearHistory
Created April 26, 2013 22:14
Steps to clear out the history of a git/github repository
-- Remove the history from
rm -rf .git
-- recreate the repos from the current content only
git init
git add .
git commit -m "Initial commit"
-- push to the github remote repos ensuring you overwrite history
git remote add origin git@github.com:<YOUR ACCOUNT>/<YOUR REPOS>.git
@jennybc
jennybc / yaml_frontmatter_r_github_document.yaml
Last active February 9, 2022 21:36
YAML frontmatter for R Markdown to cause rmarkdown::render() to retain intermediate Markdown file for .R and .Rmd files, respectively
#' ---
#' title: "Something fascinating"
#' author: "Jenny Bryan"
#' date: "`r format(Sys.Date())`"
#' output: github_document
#' ---
@jennybc
jennybc / 2014-09-18_verbatim-r-chunks-in rmd.rmd
Created September 19, 2014 05:57
How to get verbatim R chunks in R markdown. Again. Writing it down now.
---
title: "Get verbatim R chunks in R Markdown"
author: "Jenny Bryan"
date: "18 September, 2014"
output:
html_document:
keep_md: TRUE
---
My periodic revisitation of "how can I include a verbatim R chunk in `.rmd`"? This time I am writing it down! Various proposed solutions:
@jennybc
jennybc / 2014-12-01_finessing-excel-line-endings.md
Last active November 17, 2022 13:04
Finessing Excel's stupid line endings

Finessing Excel's stupid line endings

I am sheepish to admit a certain type of routine Microsoft Excel use.

Current example: I am marking for STAT 545. I use R to create a comma delimited marking sheet, by joining the official class list and peer reviews. The sheet contains variables, initially set to NA, where the TAs and I enter official marks and optional comments.

This is where Excel comes in. I like its visual organization of this comma delimited file much more than, say, using a plain text editor. I use the ability to hide columns, resize columns, wrap text, and (gasp!) even fill rows with grey to indicate I am done.

I keep saving the file as comma delimited and I put up with Excel's incessant freak out about "losing features". This is not a one time thing. I need to save and commit this file many times before it is considered done.

@hrbrmstr
hrbrmstr / bom.R
Last active July 16, 2019 09:40
test for and/or remove byte order mark in R
#' Tests whether a raw httr response or character vector has a byte order mark (BOM)
#'
#' \href{https://en.wikipedia.org/wiki/Byte_order_mark}{What is a byte order mark?}
#'
#' @param resp \code{httr} response object or a character vector
#' @param encoding can be one of \code{UTF-8}, \code{UTF-16} or \code{UTF-16BE}.
#' Although a BOM could be used with UTF-32 and other encodings, such
#' encodings are rarely used for data transmission and the three supported
#' encodings are the most likely ones folks in R will be working with from
#' web APIs.\cr\cr
@hrbrmstr
hrbrmstr / README.md
Last active January 7, 2024 22:35
French towns, surnames and suffixes

The best way to grab these files is to git clone the gist or download it as a ZIP.

If I knew more about French noun constructs I would have tried to do the suffix extraction myself, but it would have probably ended up doing nothing more than proving I'm an uneducated American ;-)

fr_names.csv

These are all the "place names" in France. The first column is the UTF-8 encoding of the name. The second column is the ASCII encoding of the name. This is from geonames

You'll want to get the full geonames file as it has the long/lat of the place name center (which can be used to replicate the example).

We can't make this file beautiful and searchable because it's too large.
"x","y","name"
8.0056049,48.7521656,"Greffern"
7.9490308,49.0383877,"Wissembourg"
7.7507127,48.584614,"Strasbourg"
7.3399355,47.7494188,"Mulhouse"
7.3625104,48.7420277,"Saverne"
7.24682,47.8,"Wittelsheim"
7.3386856,47.7923002,"Kingersheim"
7.7102193,48.5425109,"Ostwald"
7.1767542,47.8071991,"Cernay"
@jimhester
jimhester / decode_bitwise.R
Created April 29, 2016 11:29
Decoing bitwise field
decode_field <- function(x) {
nms <- c("No alarm", "Tape Break", "Beta Count Failure", "High Tape Delta Pressure (Tape Advance)", "Pressure Sensor Failure", "Flow Failure", "Nozzle Failure", "Internal Hardware (SPI bus) Failure", "Low Battery", "Delta Temperature Setpoint Exceeded", "Pump Over Temp 48C")
nms[bitwAnd(x, c(0, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512)) > 0]
}
decode_field(8)
#> [1] "Pressure Sensor Failure"
decode_field(64 + 2)
#> [1] "Beta Count Failure"
#> [2] "Internal Hardware (SPI bus) Failure"
decode_field(1 + 32 + 128)
@jonocarroll
jonocarroll / images_as_xaxis_labels_updated.R
Last active September 7, 2021 05:43
Implements @baptiste's much better method for making this work
library(ggplot2) ## devtools::install_github("hadley/ggplot2)
library(grid) ## rasterGrob
library(EBImage) ## readImage (alternatively: magick::image_read)
library(ggthemes) ## theme_minimal
## ##########
## INDEPENDENT CODE TO BE SOURCED:
## ##########
# user-level interface to the element grob