Last active
August 22, 2018 03:33
-
-
Save jduckles/94833ff736f6642ff82e075d2b3447f5 to your computer and use it in GitHub Desktop.
A simple example of lapply
This file contains 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
mydata1 <- data.frame(letter = c("a","b","c","a","b","c")) | |
mydata2 <- data.frame(letter = c("a","b","c","a","b","c","a","b","a")) | |
stuff <- list(mydata1,mydata2) | |
myawesome <- function(df) { | |
levels <- c("a","b","c") | |
as.factor(df$letter) | |
} | |
lapply(stuff, myawesome) |
This file contains 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
mylist <- list(a=data.frame(vals=rnorm(100)),b=data.frame(vals=rnorm(100)) ) | |
my_summary <- function(df) { | |
stdev <- sd(df$vals) | |
mean_val <- mean(df$vals) | |
list(stdev=stdev, mean=mean_val) | |
} | |
lapply(mylist, my_summary) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment