Skip to content

Instantly share code, notes, and snippets.

View hadley's full-sized avatar

Hadley Wickham hadley

View GitHub Profile
library(httr)
library(jsonlite)
find_endorsements <- function(x) {
r <- GET("https://www.linkedin.com/ta/skill" ,query = list(query = x))
stop_for_status(r)
json <- content(r, "parsed")
vapply(json$resultList, "[[", "displayName", FUN.VALUE = character(1))
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
std::string escape_one(std::string x) {
int n = x.size();
std::string out = "\"";
out.reserve(n + 2);
for (int i = 0; i < n; ++i) {
iclass <- function(x) {
c(
if (is.matrix(x)) "matrix",
if (is.array(x) && !is.matrix(x)) "array",
if (is.double(x)) "double",
if (is.integer(x)) "integer",
mode(x)
)
}
`[.my_df` <- function (x, i, j) {
if (missing(i) && missing(j)) return(x)
# First, subset columns
if (!missing(j)) {
x <- .subset(x, j)
}
# Next, subset rows
if (!missing(i)) {
@hadley
hadley / date-parser.R
Last active August 29, 2015 14:10
Go style date formats for R
library("stringi")
ref <- ISOdatetime(2006, 01, 02, 15, 4, 0, "MST")
formats <- c("%H", "%Y", "%y", "%b", "%B", "%m", "%d", "%e", "%a",
"%A", "%I", "%l", "%p", "%Z", "%z", "%M", "%S")
# Must manually respecify TZ because strftime doesn't use the time zone
# stored in ref
conv <- vapply(formats, function(x) strftime(ref, x, tz = "MST"), character(1))
library(stringi)
locales <- stri_locale_list()
main <- locales[!stri_detect_fixed(locales, "_")]
main_col <- lapply(main, stri_opts_collator)
sorted <- lapply(main_col, function(x) stri_sort(letters, opts_collator = x))
names(sorted) <- main
noquote(simplify2array(Filter(function(x) !identical(x, letters), sorted)))
@hadley
hadley / rxive.md
Last active August 29, 2015 14:11
  • Only accepted if R CMD CHECK passes with no WARNINGS or ERRORs
  • Long-term archive, and difficult to remove
  • Provides registry of R packages
  • Zenodo DOIs
library(class)
library(mass)
library(mva)
tst <- read.table("e:/uni/stats766/puktest.txt", header = TRUE)
tst.v <- tst[,1:7]
tst.g <- tst[,8]
trn <- read.table("e:/uni/stats766/puktrain.txt", header = TRUE)
trn.v <- trn[,1:7]
trn.g <- trn[,8]
data_sort <- function() {
desc <- FALSE;
my_sort <- function (data) {
sort(data, decreasing = desc)
}
attr(my_sort, "desc") <- function(x) {
desc <<- x
return(my_sort)
# http://stackoverflow.com/a/3407254/16632
Rcpp::cppFunction("double ceil_any(double x, double prec) {
if (fabs(prec / x) < DBL_MIN)
return x;
double r = fmod(fabs(x), prec);
if (r == 0)
return x;
return (x > 0) ? x + prec - r : x + r;