5

I have written a function that takes three arguments:

create.template <- function(t.list, x, y){
    temp <- cbind(get(t.list[x]), get(t.list[y]), NA)
}

The output of this function is a data.frame with 11 columns and 17 rows.

Now I would like to create a loop over the function with two lists, one for x and one for y. Thereby

x.list <- list(1,2,3)
y.list <- list(4,5,6)

In the final step I would like to establish something like

for (x in x.list and y in y.list){
   create.template(t.list, x, y)
}

and possibly combine the resulting dataframes (3 dataframes with 11 columns each) rowwise in one final dataframe.

I know that you can do this in Python with the zip() function and then append the results easily by append() and concatenate(), but I have not found an equivalent in R so far. Any help is highly appreciated!

Carmen
  • 763
  • 2
  • 10
  • 22

1 Answers1

0

We can get the values of multiple objects with mget, use either Reduce or do.call to cbind the list of vectors

Reduce(cbind, c(mget(ls(pattern = "\\.list")), NA))

Or

do.call(cbind, c(mget(c("x.list", "y.list")), NA))
akrun
  • 874,273
  • 37
  • 540
  • 662