Reputation: 2637
I have these three data frames and this function
set.seed(1)
df1 <-
data.frame(
x = c("cat", "dog"),
y = sample(1:100, 20),
z = seq(ymd("2020-01-01"), ymd("2020-01-20"), by = "1 day")
)
df2 <-
data.frame(
x = c("red", "green"),
y = sample(1:100, 20),
z = seq(ymd("2020-01-01"), ymd("2020-01-20"), by = "1 day")
)
df3 <-
data.frame(
x = c("up", "down"),
y = sample(1:100, 20),
z = seq(ymd("2020-01-01"), ymd("2020-01-20"), by = "1 day")
)
dataframes <-
c("df1", "df2", "df3")
exploreDataFrames <- function(dataframes, sequence = seq_along(dataframes)){
for (i in seq_along(sequence)){
print(get(dataframes[i]))
gg <-
get(dataframes[i]) %>%
ggplot(aes(z, y, group = 1)) +
geom_line() +
facet_wrap(~get(names(get(dataframes[i]))[1]))
return(gg)
}
}
exploreDataFrames(dataframes[3])
I'd like for my function to return a list with three entries:
list[1][1] <- name_of_the_dataframe
list[1][2] <- copy_of_the_complete_dataframe
list[1][3] <- ggplot_object
My goal is to be able to have a single list and iterate through the ggplot charts, quickly be able to slice it by the name of a particular data frame and pull up the complete dataframe.
What do I need to change about my function to make this possible?
Upvotes: 3
Views: 264
Reputation: 76683
To return the list the question asks for, create a list to hold the objects before the for
loop, with the length set to length(sequence)
and assign the values in the loop.
exploreDataFrames <- function(dataframes, sequence = seq_along(dataframes)){
out_list <- vector("list", length = length(sequence))
for (i in seq_along(sequence)){
dftmp <- get(dataframes[i])
print(dftmp)
gg <-
dftmp %>%
ggplot(aes(z, y, group = 1)) +
geom_line() +
facet_wrap(~get(names(dftmp)[1]))
out_list[[i]]$data.name <- dataframes[i]
out_list[[i]]$data <- dftmp
out_list[[i]]$gg.plot <- gg
}
out_list
}
exploreDataFrames(dataframes[3])
This will plot the out_list[[1]]$gg.plot
object, since the function's return value is not assigned to anything, it's returned to the .GlobalEnv
.
Upvotes: 3
Reputation: 151
You need to initiate the whole list before starting to populate it. Since you have a nested level in the list, you need to initiate that "inner" list too:
set.seed(1)
df1 <-
data.frame(
x = c("cat", "dog"),
y = sample(1:100, 20),
z = seq(ymd("2020-01-01"), ymd("2020-01-20"), by = "1 day")
)
df2 <-
data.frame(
x = c("red", "green"),
y = sample(1:100, 20),
z = seq(ymd("2020-01-01"), ymd("2020-01-20"), by = "1 day")
)
df3 <-
data.frame(
x = c("up", "down"),
y = sample(1:100, 20),
z = seq(ymd("2020-01-01"), ymd("2020-01-20"), by = "1 day")
)
dataframes <-
c("df1", "df2", "df3")
exploreDataFrames <- function(dataframes, sequence = seq_along(dataframes)){
ll <- list()
for (i in seq_along(sequence)){
ll[[i]] <- list()
ll[[i]][[1]] <- dataframes[i]
# print(get(dataframes[i]))
ll[[i]][[2]] <- get(dataframes[i])
ll[[i]][[3]] <-
get(dataframes[i]) %>%
ggplot(aes(z, y, group = 1)) +
geom_line() +
facet_wrap(~get(names(get(dataframes[i]))[1]))
}
return(ll)
}
out <- exploreDataFrames(dataframes)
Upvotes: 3
Reputation: 31454
You can use
l = sapply(dataframes, function (dfname) {
ggplot(get(dfname) , aes(z, y, group = 1)) +
geom_line() +
facet_wrap(~get(names(get(dfname))[1]))
}, simplify = FALSE, USE.NAMES = TRUE)
the data.frame names are then in names(l)
names(l[1])
# [1] "df1"
and the data are in
l[[1]]$data
# x y z
#1 cat 68 2020-01-01
#2 dog 39 2020-01-02
#3 cat 1 2020-01-03
#4 dog 34 2020-01-04
#5 cat 87 2020-01-05
#6 dog 43 2020-01-06
#7 cat 14 2020-01-07
#8 dog 82 2020-01-08
#9 cat 59 2020-01-09
#10 dog 51 2020-01-10
#11 cat 85 2020-01-11
#12 dog 21 2020-01-12
#13 cat 54 2020-01-13
#14 dog 74 2020-01-14
#15 cat 7 2020-01-15
#16 dog 73 2020-01-16
#17 cat 79 2020-01-17
#18 dog 37 2020-01-18
#19 cat 83 2020-01-19
#20 dog 97 2020-01-20
Upvotes: 6