<pre><code>list.of.results &lt;- lapply(seq(INSERT_TOTAL_NUMER_OF_SAMPLES_HERE),function(sample.id){
whichSignatures(tumor.ref = sigs.input, 
                           signatures.ref = signatures.nature2013, 
                           sample.id = sample.id, 
                           contexts.needed = TRUE,
                           tri.counts.method  = 'genome')[[1]]
})

sig &lt;- do.call(rbind,list.of.results)
</code></pre>

CC BY-SA 4.0
2019-11-07T17:18:36.720Z
do you know how to do this in dplyr ?
StupidWolf
CC BY-SA 4.0
2019-11-07T15:38:58.203Z
2019-11-07T15:38:58.203Z
story
<p>I have 139 samples; In R for each sample I run below codes</p>

<p>for example for <strong><em>sample 1</em></strong></p>

<pre><code>sample_1 = whichSignatures(tumor.ref = sigs.input, 
                           signatures.ref = signatures.nature2013, 
                           sample.id = 1, 
                           contexts.needed = TRUE,
                           tri.counts.method  = 'genome')

a1=(sample_1[[1]])




&gt; class(sample_1)
[1] "list"

&gt; a1=(sample_1[[1]])

&gt; head(a1)
  Signature.1A Signature.1B Signature.2 Signature.3 Signature.4 Signature.5 Signature.6 Signature.7 Signature.8
1    0.5370653            0           0           0           0           0           0           0           0
  Signature.9 Signature.10 Signature.11 Signature.12 Signature.13 Signature.14 Signature.15 Signature.16 Signature.17
1           0            0            0            0            0            0            0            0    0.3383947
  Signature.18 Signature.19 Signature.20 Signature.21 Signature.R1 Signature.R2 Signature.R3 Signature.U1 Signature.U2
1    0.1123409            0            0            0            0            0            0            0            0
&gt; dim(a1)
[1]  1 27
</code></pre>

<p>and for <strong><em>sample 2</em></strong></p>

<pre><code>sample_2 = whichSignatures(tumor.ref = sigs.input, 
                           signatures.ref = signatures.nature2013, 
                           sample.id = 2, 
                           contexts.needed = TRUE,
                           tri.counts.method  = 'genome')

a2=(sample_2[[1]])
</code></pre>

<p>And finally binding <code>a1,a2,...a139</code> by</p>

<pre><code>sig=rbind(a1,a2)


&gt; print(sig)
  Signature.1A Signature.1B Signature.2 Signature.3 Signature.4 Signature.5 Signature.6 Signature.7 Signature.8
1    0.5370653            0           0           0           0           0           0           0           0
2    0.7162292            0           0           0           0           0           0           0           0
  Signature.9 Signature.10 Signature.11 Signature.12 Signature.13 Signature.14 Signature.15 Signature.16 Signature.17
1           0            0            0            0            0            0            0            0    0.3383947
2           0            0            0            0            0            0            0            0    0.2837708
  Signature.18 Signature.19 Signature.20 Signature.21 Signature.R1 Signature.R2 Signature.R3 Signature.U1 Signature.U2
1    0.1123409            0            0            0            0            0            0            0            0
2    0.0000000            0            0            0            0            0            0            0            0
&gt;
</code></pre>

<p><code>sigs.input</code> is a common R object here</p>

<p>Doing these manually is too painful; Can you please help me how I do that automatically in R? I am not sure but something like looping or whatever you guess possible </p>

<p>Thanks</p>

<r><loop>
How I loop over this code
# How I loop over this code

(Asked by: username_0 on 2019-11-07)

I have 139 samples; In R for each sample I run below codes
for example for sample 1
sample_1 = whichSignatures(tumor.ref = sigs.input, 
                           signatures.ref = signatures.nature2013, 
                           sample.id = 1, 
                           contexts.needed = TRUE,
                           tri.counts.method  = 'genome')

a1=(sample_1[[1]])




> class(sample_1)
[1] "list"

> a1=(sample_1[[1]])

> head(a1)
  Signature.1A Signature.1B Signature.2 Signature.3 Signature.4 Signature.5 Signature.6 Signature.7 Signature.8
1    0.5370653            0           0           0           0           0           0           0           0
  Signature.9 Signature.10 Signature.11 Signature.12 Signature.13 Signature.14 Signature.15 Signature.16 Signature.17
1           0            0            0            0            0            0            0            0    0.3383947
  Signature.18 Signature.19 Signature.20 Signature.21 Signature.R1 Signature.R2 Signature.R3 Signature.U1 Signature.U2
1    0.1123409            0            0            0            0            0            0            0            0
> dim(a1)
[1]  1 27

and for sample 2
sample_2 = whichSignatures(tumor.ref = sigs.input, 
                           signatures.ref = signatures.nature2013, 
                           sample.id = 2, 
                           contexts.needed = TRUE,
                           tri.counts.method  = 'genome')

a2=(sample_2[[1]])

And finally binding a1,a2,...a139 by
sig=rbind(a1,a2)


> print(sig)
  Signature.1A Signature.1B Signature.2 Signature.3 Signature.4 Signature.5 Signature.6 Signature.7 Signature.8
1    0.5370653            0           0           0           0           0           0           0           0
2    0.7162292            0           0           0           0           0           0           0           0
  Signature.9 Signature.10 Signature.11 Signature.12 Signature.13 Signature.14 Signature.15 Signature.16 Signature.17
1           0            0            0            0            0            0            0            0    0.3383947
2           0            0            0            0            0            0            0            0    0.2837708
  Signature.18 Signature.19 Signature.20 Signature.21 Signature.R1 Signature.R2 Signature.R3 Signature.U1 Signature.U2
1    0.1123409            0            0            0            0            0            0            0            0
2    0.0000000            0            0            0            0            0            0            0            0
>

sigs.input is a common R object here
Doing these manually is too painful; Can you please help me how I do that automatically in R? I am not sure but something like looping or whatever you guess possible 
Thanks

**Tags:** r, loop

---

## 1 answers

### Answer by username_1 on 2019-11-07. Score: 6 | Accepted answer

list.of.results <- lapply(seq(INSERT_TOTAL_NUMER_OF_SAMPLES_HERE),function(sample.id){
whichSignatures(tumor.ref = sigs.input, 
                           signatures.ref = signatures.nature2013, 
                           sample.id = sample.id, 
                           contexts.needed = TRUE,
                           tri.counts.method  = 'genome')[[1]]
})

sig <- do.call(rbind,list.of.results)

> **Comments:**
> **username_2**: do you know how to do this in dplyr ?  

---

