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 / 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 / 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 / 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 / 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 / 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")
}