Skip to content

Instantly share code, notes, and snippets.

@jduckles
Last active August 22, 2018 03:33
Show Gist options
  • Save jduckles/94833ff736f6642ff82e075d2b3447f5 to your computer and use it in GitHub Desktop.
Save jduckles/94833ff736f6642ff82e075d2b3447f5 to your computer and use it in GitHub Desktop.
A simple example of lapply
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)
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