Skip to content

Instantly share code, notes, and snippets.

@whatalnk
Last active August 6, 2016 00:55
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 whatalnk/842c99dd14e0c7762a0d37efcadb1fdd to your computer and use it in GitHub Desktop.
Save whatalnk/842c99dd14e0c7762a0d37efcadb1fdd to your computer and use it in GitHub Desktop.
[R] sapply(x, simplyfy = FALSE)
a <- data.frame(x = c(1, 2, 3), y = c(4, 5, 6), z = c(7, 8, 9))
b <- data.frame(x = c(1, 2, 3) * 2, y = c(4, 5, 6) * 2, z = c(7, 8, 9) * 3)
c <- data.frame(x = c(1, 2, 3) * 3, y = c(4, 5, 6) * 3, z = c(7, 8, 9) * 3)
l <- list(a = a, b = b, c = c)
x <- c("a", "b", "c")
get_df <- function(x){
df <- l[[x]]
return(df)
}
list_of_df <- lapply(x, get_df)
names(list_of_df)
# NULL
list_of_df
[[1]]
# x y z
# 1 1 4 7
# 2 2 5 8
# 3 3 6 9
# [[2]]
# x y z
# 1 2 8 21
# 2 4 10 24
# 3 6 12 27
# [[3]]
# x y z
# 1 3 12 21
# 2 6 15 24
# 3 9 18 27
list_of_df_sapply <- sapply(x, get_df, simplify = FALSE, USE.NAMES = TRUE)
names(list_of_df_sapply)
# [1] "a" "b" "c"
list_of_df_sapply
$a
# x y z
# 1 1 4 7
# 2 2 5 8
# 3 3 6 9
# $b
# x y z
# 1 2 8 21
# 2 4 10 24
# 3 6 12 27
# $c
# x y z
# 1 3 12 21
# 2 6 15 24
# 3 9 18 27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment