Skip to content

Instantly share code, notes, and snippets.

View garyfeng's full-sized avatar

Gary Feng garyfeng

  • Educational Testing Service
  • United States
View GitHub Profile
@garyfeng
garyfeng / R PackageInstall.r
Last active August 29, 2015 13:56
r code to test and install libraries if not already installed.
# r code to test and install libraries if not already installed.
# code taking from stackoverflow
pkgInstall <- function(x)
{
if (!require(x,character.only = TRUE))
{
install.packages(x,dep=TRUE)
if(!require(x,character.only = TRUE)) stop("Package not found")
}
@garyfeng
garyfeng / working with CKEditor Bookmarks.js
Last active August 29, 2015 14:21
collection of code to work with bookmarks in CKEditor
// see range.js and selection.js for the source
// so we have 3 things to work with:
// CKEditor.dom selections, ranges, and bookmarks
// first, to get the CKeditor instance, use
var editor = CKEDITOR.instances.editor1;
// selection something in the editor, and get the selection:
var sel = editor.getSelection();
@garyfeng
garyfeng / regexSubStrings.r
Created May 30, 2015 04:00
R function to extract a vector of substrings from a list or vector of strings, based on a regexpr
# A function that takes a vector or a list of strings and a regex pattern,
# and returns a vector of strings that matches the regex patterns. Don't
# have parameter checking, error handling, etc.
# The key lesson learned is that regmatches() is badly designed
# as it silently drops any non-matched elements. As a result, the length
# of the returned vector may nor may not be the same as the input.
# had to use the good-o substring() trick.
# The second lesson is that when the regex pattern contains several (), such
@garyfeng
garyfeng / importData2TraMineR.r
Created May 30, 2015 04:07
TraMineR seqdef() is picky. Sometimes you need to define a new data frame just to get it recognized.
# I am mystified by how Traminer handles the data format.
# Here's the data from the 2015 NAEP Reading tryout study, SC block
# where I transformed the data to simplify the structure.
# Then I try to use seqformat() or seqdef() to turn the existing data frame
# in to a TraMineR object.
sc %>% filter(readingTime>0) %>% group_by(student) %>% arrange(readingTime) %>%
mutate(c1=1:n(), c2=2:(n()+1), t0=c(1, readingTime[1:n()-1])) %>%
select(student, c1, c2, state2, t0, readingTime, event2) %>% na.omit() ->scSPELL
@garyfeng
garyfeng / backFillNA.r
Last active January 13, 2016 17:10
A function to back fill NAs in a vector with the last non-NA value
# A function to back fill NAs in a vector with the last non-NA value
# https://gist.github.com/garyfeng/27e7f8e406192a8cb33a
backFillNA<- function (x) {
nas<- which(is.na(x))
# trick from http://stackoverflow.com/questions/24837401/find-consecutive-values-in-vector-in-r
naList<-split(nas, cumsum(c(1, diff(nas) != 1)))
# get the backfill values
valueList<-lapply(naList, function(nalist) {
prev<- nalist[1]-1
@garyfeng
garyfeng / interp.r
Created May 30, 2015 20:27
R tricks with lazyeval::interp():
# taken from
# http://cran.rstudio.com/web/packages/dplyr/vignettes/nse.html
# What if you need to mingle constants and variables? Use the handy lazyeval::interp():
library(lazyeval)
# Interp works with formulas, quoted calls and strings (but formulas are best)
interp(~ x + y, x = 10)
#> ~10 + y
interp(quote(x + y), x = 10)
#> 10 + y
@garyfeng
garyfeng / getListAttribute.r
Last active September 23, 2015 14:12
Creating an operator to extract a named member of a list that is in a list. Why? Read on. [Now part of the "pdata" library at github.com/garyfeng/pdata.
# To create an operator to extract a named member of a list that is in a list.
# This may sound confusing, but imagine you have a data.frame where a variable/column is a list of lists,
# and the lists have named members, e.g.,
# df$itinary <- list(list(from="NYC", to="LA", via="train"), list(from="LA", to="SF"), ...)
# You want to get the "from" value of itinary as a vector. You can do
# df$itinary@"from" or `@`(df$itinary, "via")
# Typically you'd use ```sapply(x, function(m) {m[["from"]]})```. The following is an extention to the idea in 2 ways:
# 1). We define a in-fix operator `@` that does so in a way that is syntactically more natural
# 2). We added error handling, in the case of bad indecies, etc.
require(testthat)
@garyfeng
garyfeng / Keystroke logging implementation.markdown
Created September 23, 2015 15:25
Keystroke logging implementation
<style>
/*
Taken from http://moderndata.plot.ly/custom-styling-for-ipython-notebooks-with-3-lines-of-code/
Modified for Gary Feng's Jupyter notebooks
*/
/*
html {
font-size: 62.5% !important; }
@garyfeng
garyfeng / HeatmapWithTransparentBackground.svg
Created June 2, 2016 13:40
Adding a transparent background image to Plot.ly's SVG output. Two changes: (a) set opacity in `<g class="subplot xy" style = "opacity:0.5">`, and (b) adding an image in the "layer-below >> imagelayer" group.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.