Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I suggest to recode myfun to take a matrix and use pasteCols from plotrix package.</p> <pre><code>library(plotrix) myfun = function(x){ out = pasteCols(t(x), sep = ":") # some code return(out) } </code></pre> <p>then, its very easy: for each group, compute the index of the first and of the last column you want to use when you call myfun, using modulus and integer division:</p> <pre><code>rubiques_solution = function(group, myd, num_to_group){ # loop over groups for(g in unique(group)){ var_index = which(group == g) num_var = length(var_index) # test to make sure num_to_group is smaller than the number of variable if(num_var &lt; num_to_group){ stop("num_to_group &gt; number of variable in at least one group") } # number of calls to myfun num_calls = num_var %/% num_to_group # the idea here is that we create the first and last column # in which we are interested for each call first = seq(from = var_index[1], by = num_to_group, length = num_calls) last = first + num_to_group -1 # the last call will contain possibly more varialbe, we adjust here: last[length(last)] = last[length(last)] + (num_var %% num_to_group) for(i in num_calls){ # maybe do something with the return value of myfun ? myfun(myd[,first[i]:last[i]]) } } } group = rep(1:10, each = 10) # same than yours myd = data.frame (matrix (sample (c("AB", "BB", "AA"), 100*100, replace = T), ncol = 100)) # same than yours num_to_group = 2 # this is your first example rubiques_solution(group, myd, num_to_group) </code></pre> <p>hope i understood the problem right.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload