Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Create a function that will split your data up into appropriate lists, and apply whatever functions you want to your list.</p> <p>This function will create your <em>second</em> grouping variable. (The first grouping variable (<code>group</code>) is provided in your question; if you change that value, you should also change <code>DIM</code> in the function below.)</p> <pre><code>myfun = function(LENGTH, DIM = 10) { PATTERN = rep(1:(DIM %/% LENGTH), each=LENGTH) c(PATTERN, rep(max(PATTERN), DIM %% LENGTH)) } </code></pre> <p>Here are the groups on which we will split <code>myd</code>. In this example, we are splitting <code>myd</code> first into 10-column groups, and each group into 3-column groups, except for the last group, which will have 4 columns (3+3+4 = 10).</p> <blockquote> <blockquote> <p>NOTE: <em>To change the number of columns you're grouping by, for example, grouping by two variables at a time, change</em> <strong><code>group2 = rep(myfun(3), length.out=100)</code></strong> <em>to</em> <strong><code>group2 = rep(myfun(2), length.out=100)</code></strong>.</p> </blockquote> </blockquote> <pre><code>group &lt;- rep(1:10, each = 10) # CHANGE THE FOLLOWING LINE ACCORDING # TO THE NUMBER OF GROUPS THAT YOU WANT group2 = rep(myfun(3), length.out=100) </code></pre> <p>This is the splitting process. We first split up just by names, and match those names with <code>myd</code> to create a list of <code>data.frames</code>.</p> <pre><code># Extract group names for matching purposes temp = split(names(myd), list(group, group2)) # Match the names to myd temp = lapply(1:length(temp), function(x) myd[, which(names(myd) %in% temp[[x]])]) # Extract the names from the list for future reference NAMES = lapply(temp, function(x) paste(names(x), collapse="_")) </code></pre> <p>Now that we have a list, we can do lots of fun things. You wanted to paste your columns together separated by a colon. Here's how you'd do that.</p> <pre><code># Do what you want with the list # For example, to paste the columns together: FINAL = lapply(temp, function(x) apply(x, 1, paste, collapse=":")) names(FINAL) = NAMES </code></pre> <p>Here's a sample of the output:</p> <pre><code>lapply(FINAL, function(x) head(x, 5)) # $MR.1.1_MR.1.2_MR.1.3 # [1] "AA:AB:AB" "AB:BB:AA" "BB:AB:AA" "BB:AA:AB" "AA:AA:AA" # # $MR.2.11_MR.2.12_MR.2.13 # [1] "BB:AA:AB" "BB:AB:BB" "BB:AA:AA" "AB:BB:AA" "BB:BB:AA" # # $MR.3.21_MR.3.22_MR.3.23 # [1] "AA:AB:BB" "BB:AA:AA" "AA:AB:BB" "AB:AA:AA" "AB:BB:BB" # # &lt;&lt;&lt;&lt;&lt;&lt;&lt;------SNIP------&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; # # $MR.1.4_MR.1.5_MR.1.6 # [1] "AB:BB:AA" "BB:BB:BB" "AA:AA:AA" "BB:BB:AB" "AB:AA:AA" # # $MR.2.14_MR.2.15_MR.2.16 # [1] "AA:BB:AB" "BB:BB:BB" "BB:BB:AB" "AA:BB:AB" "BB:BB:BB" # # $MR.3.24_MR.3.25_MR.3.26 # [1] "AA:AB:BB" "BB:AA:BB" "BB:AB:BB" "AA:AB:AA" "AB:AA:AA" # # &lt;&lt;&lt;&lt;&lt;&lt;&lt;------SNIP------&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; # # $MR.1.7_MR.1.8_MR.1.9_MR.1.10 # [1] "AB:AB:AA:AB" "AB:AA:BB:AA" "BB:BB:AA:AA" "AB:BB:AB:AA" "AB:BB:AB:BB" # # $MR.2.17_MR.2.18_MR.2.19_MR.2.20 # [1] "AB:AB:BB:BB" "AB:AB:BB:BB" "AB:AA:BB:BB" "AA:AA:AB:AA" "AB:AB:AB:AB" # # $MR.3.27_MR.3.28_MR.3.29_MR.3.30 # [1] "BB:BB:AB:BB" "BB:BB:AA:AA" "AA:BB:AB:AA" "AA:BB:AB:AA" "AA:AB:AA:BB" # # $MR.4.37_MR.4.38_MR.4.39_MR.4.40 # [1] "BB:BB:AB:AA" "AA:BB:AA:BB" "AA:AA:AA:AB" "AB:AA:BB:AB" "BB:BB:BB:BB" # # $MR.5.47_MR.5.48_MR.5.49_MR.5.50 # [1] "AB:AA:AA:AB" "AB:AA:BB:AA" "AB:BB:AA:AA" "AB:BB:BB:BB" "BB:AA:AB:AA" # # $MR.6.57_MR.6.58_MR.6.59_MR.6.60 # [1] "BB:BB:AB:AA" "BB:AB:BB:AA" "AA:AB:AB:BB" "BB:AB:AA:AB" "AB:AA:AB:BB" # # $MR.7.67_MR.7.68_MR.7.69_MR.7.70 # [1] "BB:AB:BB:AA" "BB:AB:BB:AA" "BB:AB:BB:AB" "AB:AA:AA:AA" "AA:AA:AA:AB" # # $MR.8.77_MR.8.78_MR.8.79_MR.8.80 # [1] "AA:AB:AA:AB" "AB:AA:AB:BB" "BB:BB:AA:AB" "AB:BB:BB:BB" "AB:AA:BB:AB" # # $MR.9.87_MR.9.88_MR.9.89_MR.9.90 # [1] "AA:BB:AB:AA" "AA:AB:BB:BB" "AA:BB:AA:BB" "AB:AB:AA:BB" "AB:AA:AB:BB" # # $MR.10.97_MR.10.98_MR.10.99_MR.10.100 # [1] "AB:AA:BB:AB" "AB:AA:AB:BB" "BB:AB:AA:AA" "BB:BB:AA:AA" "AB:AB:BB:AB" </code></pre>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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