Instead of looping over the unique
'ID' and subset
ing, a faster option is split
which will split the data.frame
into list
of data.frame based on the unique
values of 'ID'
df_list <- split(df, df$ID)
From here, we can either use lapply
or a for
loop
pdf(paste0(out_dir, output_date,'.pdf'))
for(i in seq_along(df_list)) {
ggplot(data = df_list[[i]]) +
...
}
dev.off()
Or with lapply
pdf(paste0(out_dir, output_date,'.pdf'))
lapply(df_list, function(dat)
ggplot(data = dat) +
...
)
dev.off()
Regarding the creation of an object of unique
'ID', a better option is
for(un in unique(df$ID)) {
df_new <- df %>%
filter(ID == un)
ggplot(df_new) +
...
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…