Created
June 4, 2015 14:16
-
-
Save StevenMMortimer/44df7b34c8dde8f6ef03 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(plyr) | |
#testing gist change | |
#LLPLY | |
# Create a list with 2 elements | |
l <- list(a = 1:10, b = 11:20, c=21:30, d=31:40) | |
# Apply a function to each element of the list | |
system.time(lapply(l, mean)) | |
system.time( | |
lapply(l, FUN=function(x){ | |
# THIS FUNCTION TAKES A LONG TIME! | |
Sys.sleep(2) | |
mean(x) | |
} | |
) | |
) | |
# register parallel backend | |
# MAC VERSION | |
#library(doMC) | |
#registerDoMC(2) | |
#unregister <- function() { | |
# env <- foreach:::.foreachGlobals | |
# rm(list=ls(name=env), pos=env) | |
#} | |
# WINDOWS VERSION | |
library(doSNOW) | |
cl <- makeCluster(4, type = "SOCK") | |
registerDoSNOW(cl) | |
system.time( | |
suppressWarnings( | |
llply(.data=l, .fun=function(x){ | |
Sys.sleep(2) | |
mean(x) | |
}, | |
.parallel=TRUE) | |
) | |
) | |
# WINDOWS VERSION | |
stopCluster(cl) | |
# MAC VERSION | |
#unregister() | |
#LDPLY | |
iris_split_datasets <- list('part1'=iris[1:50,], | |
'part2'=iris[51:100,], | |
'part3'=iris[101:150,]) | |
combined_data <- ldply(.data=iris_split_datasets, .fun=function(x){x}) | |
nrow(combined_data) | |
names(combined_data) | |
tail(combined_data) | |
#DLPLY | |
models <- dlply(.data=iris, .variables=.(Species), .fun=function(x){ | |
linear_model <- lm(Sepal.Length~Sepal.Width, data=x) | |
log_linear_model <- lm(log(Sepal.Length)~Sepal.Width, data=x) | |
list('linear_model'=linear_model, 'log_linear_model'=log_linear_model) | |
}) | |
names(models) | |
#NESTED LLPLY | |
llply(names(models), .fun=function(x){ | |
species <- models[[x]] | |
l <- llply(names(species), .fun=function(x){ | |
summary(species[[x]])$r.squared | |
}) | |
names(l) <- names(species) | |
l | |
}) | |
#DDPLY | |
datas <- ddply(.data=iris, .variables=.(Species), .fun=function(x){ | |
x$new_var <- x$Sepal.Length + x$Petal.Width | |
x | |
}) | |
head(datas) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment