例如:
library(purrr)
test_df %>%
split(.$sample) %>%
setNames(paste0(names(.), ".png")) %>%
map(~ array(c(.x$red, .x$green, .x$blue), c(2, 3, 3)) / 255) %>%
iwalk(png::writePNG)
或者以更“循序渐进”的方式:
test_df %>%
split(.$sample) %>%
setNames(paste0(names(.), ".png")) %>%
map(`[`, 3:5) %>%
map(as.matrix) %>%
map(`/`, 255) %>%
map(array, c(2, 3, 3)) %>%
iwalk(png::writePNG)
或者没有tidyverse:
z <- split(test_df, test_df$sample)
mapply(function(x, y) {
png::writePNG(array(as.matrix(x[3:5]), c(2, 3, 3)) / 255, paste0(y, ".png"))
}, z, names(z))