Skip to content

Instantly share code, notes, and snippets.

@stephlocke
Created September 10, 2014 09:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephlocke/67781f2263f7fa2639a6 to your computer and use it in GitHub Desktop.
Save stephlocke/67781f2263f7fa2639a6 to your computer and use it in GitHub Desktop.
Provides indicative view of what I'm trying to achieve by changing functions and testing the before and after results
# Example functions that would be in seperate files in the R directory of the package
simpleFn1 <- function(a){
myConst <- 10
a*myConst
}
simpleFn2 <- function(a){
myConst <- 5
a/myConst
}
myComplexFn <- function(a=iris){
library("data.table")
myDT<-as.data.table(a)
myDT[,lapply(.SD,simpleFn1),by=Species]
}
# Current version of function results
State1<-myComplexFn()[,lapply(.SD,mean),by=Species]
# Change to function
myComplexFn<-function(a=iris){
library("data.table")
myDT<-as.data.table(iris)
myDT[,lapply(.SD,simpleFn1),by=Species]
myDT[,lapply(.SD,simpleFn2),by=Species]
}
# Current version of function results
State2<-myComplexFn()[,lapply(.SD,mean),by=Species]
# Results
State1
State2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment