Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Matthew Routley http://matt.routleynet.org

« Newer Snippets
Older Snippets »
Showing 1-7 of 7 total  RSS 

Create data frame from variable number of vectors

// Create a data frame from a varied number of vectors
// Taken from http://article.gmane.org/gmane.comp.lang.r.general/56501/match=create+data+frame

   1  
   2  N1 <-c(1,2,3,4)
   3  N2 <-c(1,2,3,4)
   4  N3 <-c(1,2,3,4)
   5  abc <-c(1,2,3)
   6  lab <- paste("N", seq(along=abc), sep="")
   7  D <- data.frame(lapply(lab, get))
   8  names(D) <- lab

Call Growl from R

// Send an alert to growl with the R.app icon.
// Useful for indicating the end of a long simulation.

   1  
   2  system(paste("growlnotify -a R -t \"Message\" -m", message, sep=""))

Run R from the command line

// This runs R from the command line in batch mode.
// The log file is output to track R's progress

   1  
   2  R CMD BATCH --vanilla path/to/file.R file.log &

Iteratively create objects

// Iteratively create objects from a list of names.
// Especially useful for creating consistent model objects for a list of data sources.
// Wrapping the assign statement in a try block is often advisable.

   1  
   2  for(item in itemList){
   3  	assign(paste(item, "object", sep="."),  function(get(paste(item, "data", sep=".")), options)
   4  }

Position-based substitution in arrays

// Replace array values based on their row and column position
// This is an alternative to using for loops to test each position

   1  
   2  a <- array(0, dim=c(10, 10))
   3  a[row(a) <= col(a)*2] <- 1

Zero-inflated negative binomial

// Simulates a zero-inflated, negative binomial
// from Ben Bolker

   1  
   2  rzinbinom <- function(n, mu, size, zprob) {
   3    ifelse(runif(n) < zprob, 0, rnbinom(n, mu=mu, size=size))
   4  }

Spotlight queries of R documentation

// from http://comments.gmane.org/gmane.comp.lang.r.mac/642
// Use Spotlight to query R documentation

   1  
   2  spotlight<-function(word) {
   3  place<-paste(Sys.getenv("R_HOME"),"/library/",sep="")
   4  string<-paste("mdfind -onlyin",place,word,"|",getOption("pager"))
   5  system(string, intern=TRUE)
   6  }
« Newer Snippets
Older Snippets »
Showing 1-7 of 7 total  RSS