Note that there are some explanatory texts on larger screens.

plurals
  1. POselecting n consequent grouped variables and apply the function in r
    primarykey
    data
    text
    <p>Here is example data:</p> <pre><code> myd &lt;- data.frame (matrix (sample (c("AB", "BB", "AA"), 100*100, replace = T), ncol = 100)) variablenames= paste (rep (paste ("MR.", 1:10,sep = ""), each = 10), 1:100, sep = ".") names(myd) &lt;- variablenames </code></pre> <p>Each variable has a group, here we have ten groups. Thus the group index for the each variable in this data frame is as follows: </p> <pre><code>group &lt;- rep(1:10, each = 10) </code></pre> <p>Thus Variable names and group </p> <pre><code> data.frame (group, variablenames) group variablenames 1 1 MR.1.1 2 1 MR.1.2 3 1 MR.1.3 4 1 MR.1.4 5 1 MR.1.5 6 1 MR.1.6 7 1 MR.1.7 8 1 MR.1.8 9 1 MR.1.9 10 1 MR.1.10 11 2 MR.2.11 &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; 100 10 MR.10.100 </code></pre> <p>Each groups means that the following steps whould be applied to group of variables seperately. </p> <p>I have longer function to work the following is short example:</p> <p>function considering two variables at time </p> <pre><code>myfun &lt;- function (x1, x2) { out &lt;- NULL out &lt;- paste(x1, x2, sep=":") # for other steps to be performed here return (out) } # group 1 myfun (myd[,1], myd[,2]); myfun (myd[,3], myd[,4]); myfun (myd[,5], myd[,6]); myfun (myd[,7], myd[,8]); myfun (myd[,9], myd[,10]); # group 2 myfun (myd[,11], myd[,12]); myfun (myd[,13], myd[,14]); .......so on to group 10 ; </code></pre> <p>In this way I need to walk for variables 1:10 (i.e. in first group to perform the above action), then 11:20 (the second group). The group doesnot matter in this case number of variables in each group are divisible with number of variables (10) taken (considered) at a time (2).</p> <p>However in the following example where 3 variables taken at a time - number of total variable in each group (3), 10/3, you have one variable left over at the end. </p> <p>function considering three variable at time. </p> <pre><code>myfun &lt;- function (x1, x2, x3) { out &lt;- NULL out &lt;- paste(x1, x2, x3, sep=":") # for other steps to be performed here return (out) } # for group 1 myfun (myd[,1], myd[,2], myd[,3]) myfun (myd[,4], myd[,5], myd[,6]) myfun (myd[,7], myd[,8], myd[,9]) # As there one variable left before proceedomg to second group, the final group will have 1 extra variable myfun (myd[,7], myd[,8], myd[,9],myd[,10] ) # for group 2 myfun (myd[,11], myd[,12], myd[,13]) # and to the end all groups and to end of the file. </code></pre> <p>I want to loop this process by user defined n number of variables consered at time, where n may be 1 to maximum number of variables in each group. </p> <p><strong>Edit: Just illustration to show the process (just group 1 and 2 demostrated for example):</strong></p> <p><img src="https://i.stack.imgur.com/kYUMf.jpg" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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