calculate median from data.table columns in R
<p>I am trying to calculate a median value across a number of columns, however my data is a bit funky. It looks like the following example.</p>

<pre><code>library(data.table)

dt &lt;- data.table("ID" = c(1,2,3,4),"none" = c(0,5,5,3), 
                 "ten" = c(3,2,5,4),"twenty" = c(0,2,3,1))


   ID none ten twenty
1:  1    0   3      0
2:  2    5   2      2
3:  3    5   5      3
4:  4    3   4      1
</code></pre>

<p>In the table to column represents the number of occurrences of that value. I am wanting to calculate the median occurrence.</p>

<p>For example for ID = 1</p>

<pre><code>median(c(10, 10, 10))
</code></pre>

<p>is the calculation I am wanting to create.</p>

<p>for ID = 2</p>

<pre><code>median(c(0, 0, 0, 0, 0, 10, 10, 20, 20))
</code></pre>

<p>I have tried using <code>rep()</code> and <code>lapply()</code> with very limited success and am after some clear guidance on how this might be achieved. I understand for the likes of <code>rep()</code> I would be having to hard code my value to be repeated (e.g. <code>rep(0,2)</code> or <code>rep(10,2)</code>) and this is what I expect. I am just struggling to create a list or vector with the repetitions from each column.</p>