1
library(tidyverse)

ridiculous_function <- function(a, b){
  moo <- a
  baz <- b

list(moo, baz)
}

test <- ridiculous_function("apple", "A")

> test
[[1]]
[1] "apple"

[[2]]
[1] "A"

This code produces a list of elements of a and b, however what I would like is to run the function over two vectors in parallel, and then put all of the results in the same list.

For example, with these two vectors:

fruits10 <- fruit[1:10]
letters10 <- LETTERS[1:10]

I would want to create a list which produces elements of character vectors for "apple", "A", "apricot", "B", "avocado", "C".. and so on. My real scenario is a lot more complex so I need a solution which works with the confines of my function.

Expected output:

> test
[[1]]
[1] "apple"

[[2]]
[1] "A"

[[3]]
[1] "apricot"

[[4]] 
[1] "B"

[[5]]
[1] "avocado"

[[6]]
[1] "C"

....

[[19]]
[1] "blueberry"

[[20]]
[1] "T"
Nautica
  • 2,004
  • 1
  • 12
  • 35
  • Do you mean that you want a list with a structure like `list(fruit[1:10], LETTERS[1:10])`, or a list with a structure like `lapply(1:10, function(i) c(fruit[i], LETTERS[i]))`, or perhaps something else altogether? I think your question is a little unclear on exactly what you want your expected output to look like. – duckmayr Nov 17 '18 at 15:11
  • @duckmayr included expected output – Nautica Nov 17 '18 at 15:16
  • This seems to be a variant on the question here: https://stackoverflow.com/questions/25961897/how-to-merge-2-vectors-alternating-indexes . For example, if you can ensure the vectors are of the same length, then `as.list(rbind(fruit[1:10], LETTERS[1:10]))` should give you what you're looking for. I'm adding this as a comment rather than an answer as if I'm right, this should probably be marked as a duplicate. – duckmayr Nov 17 '18 at 15:25
  • My real function is a lot more complex (working with scraping data, wrangling, and transformation to produce three data frames) so I need a solution which works with the confines of my example function. I just chose the letters and fruit example to use as placeholder vectors instead of my own vectors. – Nautica Nov 17 '18 at 15:29
  • Then I don't understand what it is you really need? A primer on parallelization and how `%dopar%` + `foreach` works? If so, maybe start [here](https://cran.r-project.org/web/packages/doParallel/vignettes/gettingstartedParallel.pdf). Otherwise I think we need more information on your "real function". – duckmayr Nov 17 '18 at 15:38
  • 3
    *"Two lists in parallel"* to me sounds like `mapply(list, fruit10, letters10, SIMPLIFY=FALSE)` (replacing `list` with your function, eventually), but that provides a list of "tuples", not the flattened list you have there. The format you have here seems a little non-standard in that you are losing some of the "pairing" association, only retained by inference due to index within the `list`. – r2evans Nov 17 '18 at 15:48
  • Or `Map(ridiculous_function, fruits10, letters10)`. – Rui Barradas Nov 17 '18 at 16:15

2 Answers2

1

How about:

fruits10 <- fruit[1:10]
letters10 <- LETTERS[1:10]

ridiculous_function <- function(a, b){
  moo <- a
  baz <- b

  list(moo, baz)
}

library(tidyverse)

flatten(map2(fruits10, letters10, ridiculous_function))

which gives you

[1]]
[1] "apple"

[[2]]
[1] "A"

[[3]]
[1] "apricot"

[[4]]
[1] "B"

[[5]]
[1] "avocado"

[[6]]
[1] "C"

[[7]]
[1] "banana"

[[8]]
[1] "D"

etc...

davsjob
  • 1,882
  • 15
  • 10
0

Here are a few different ways of doing this:

library(tidyverse)

fruits10 <- fruit[1:10]
letters10 <- LETTERS[1:10]

ridiculous_function <- function(a, b){
  moo <- a
  baz <- b

  list(moo, baz)
}

# using mapply, base R for writing packages
mapply(ridiculous_function, fruits10, letters10) %>% 
  split(rep(1:ncol(.), each = nrow(.)))

# using map2, takes two args
map2(fruits10, letters10, ridiculous_function)

# using pmap, can take as many args as you want
list(a = fruits10,
     b = letters10) %>% 
  pmap(ridiculous_function)

You ask for results in a flat list format, so you can pop a flatten at the end of each of these, but usually you would want to retain the list structure.

Zafar
  • 1,897
  • 15
  • 33