I would like to take list of objects and build a single object out of all of them. The actual use case is to combine multiple Seurat Objects into a single object. Currently I use a for loop, however, I was curious if I could use purrr::map. To make the problem simpler, lets just concatenate a part of a list. Try not to get too cute with the result because I the true problem is more difficult (a more complex function).
w1 = list(a="first",b="This")
w2 = list(a="second",b="is")
w3 = list(a="third",b="the")
w4 = list(a="fourth",b="desired results")
The desired results would be an "This is the desired results".
list(w1,w2,w3,w4) %>% map(paste,.$b," ")
gives
[[1]] [1] "This "
[[2]] [1] "is "
[[3]] [1] "the "
[[4]] [1] "desired result "
I would like to save the results of the previous iteration and add it as a parameter to the function.
essentially I would like to replace the following line with a functional.
y=NULL;for (x in list(w1,w2,w3,w4)){ y=ifelse(is.null(y),x$b,paste0(y," ",x$b))}
#y
#"This is the desired result"
library(purrr)
list(w1, w2, w3, w4) %>%
accumulate(~paste(.x, .y[2][[1]]), .init = '') %>%
tail(1) %>%
substr(2, nchar(.))
# [1] "This is the desired results"