Home > Mobile >  Run function in R that requires two elements and want to compare two by two
Run function in R that requires two elements and want to compare two by two

Time:09-16

I know this might be a pretty simple automatic iterative question.

I am running a PLS regression by using geomorph.

This function requires two 3D arrays inside it (A1 and A2), as can be seen in the documentation in the previous link.

Basically the function would be:

two.b.pls(A1, A2, iter = 999)

The point is that I am having 8 different 3D matrix arrays and want to run the PLS analysis for any possible combination.

More explicitly, if my arrays are named Group_1, Group_2... Group_8, what I need is to iteratively analyse these combinations:

two.b.pls(Group_1, Group_2, iter = 999)
two.b.pls(Group 1, Group 3, iter = 999)
...
two.b.pls(Group_7, Group_8, iter = 999)

CodePudding user response:

If we have object names in a vector, use combn to return the pairwise combinations, get the values and pass them into two.b.pls function

nm1 <- c('Frontal', 'Face', 'Parietal_L', 'Parietal_R', 'Temporal_L', 'Temporal_R', 'Occipital', 'Sphenoid')
out <- combn(nm1, 2, FUN = function(x)
     two.b.pls(get(x[1]), get(x[2]), iter = 999), simplify = FALSE)

If we want to get the combination name, an option is to name the list elements with combn output of the vector

names(out) <- combn(nm1, 2, FUN = paste, collapse="_")
  • Related