Skip to content

Instantly share code, notes, and snippets.

@gshotwell
Last active August 29, 2015 14:23
Show Gist options
  • Save gshotwell/4d9d1e3d939230532f55 to your computer and use it in GitHub Desktop.
Save gshotwell/4d9d1e3d939230532f55 to your computer and use it in GitHub Desktop.
library(dplyr)
library(ggplot2)
library(microbenchmark)
x1 <- bind_rows(lapply(1:10, function(x)iris))
x2 <-data.frame();for(i in 1:10){x2<-rbind(x2,iris)}
all.equal(x1, x2)
f1 <- function(vector){
bind_rows(lapply(vector, function(x)iris))
}
f2 <- function(vector){
x2 <-data.frame();
for(i in vector){
x2<-rbind(x2,iris)
}
}
f3 <- function(n){
iris[rep(1:nrow(iris), n),]
}
microbenchmark(f1(1:20), f2(1:20), f3(20))
#It looks like copying a full dataframe is substantially faster than a complete subset of the dataframe
g1 <- function(vector){
lapply(vector, function(x)iris)
}
g2 <- function(vector){
lapply(vector, function(x)iris[1:150, ])
}
microbenchmark(g1(1:20), g2(1:20))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment