groups <-
function(vector, numitems, overlapping=FALSE) {
## Emulates the following J idioms
##
## 3 ]\ i.10
## _3 ]\ i.10
##
## Try the following in your R console:
##
## R> l <- 1:15
## R> groups(l, 3) ## Same as _3 ]\ i.15
## R> groups(l, 3, overlapping=TRUE) ## Same as 3 ]\ i.15
startidx <- if (overlapping) {
1:(length(vector) - numitems + 1)
} else {
seq(1, length(vector), by=numitems)
}
endidx <- startidx + numitems - 1
t(mapply(function(x,y) vector[x:y], startidx, endidx))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.