R: How to ignore certain value on pmin
<p>I have this two <code>data.table</code>s</p>

<pre><code>DT &lt;- data.table(name = c("a","b","c","d"),
                 "850Hz" = c(0,1,1,0),
                 "1800Hz" = c(2,0,2,0),
                 "2100Hz" = c(0,3,0,3),
                 "2600Hz" = c(4,0,0,4))
Info &lt;- data.table(Freq = c("850Hz", "1800Hz", "2100Hz", "2600Hz"), Rng = c(3000, 2000, 1800, 1000))
</code></pre>

<p>I want to create a new column called 'Range' in DT, proceeding for each row like follows:
If the row contains a number different than 0 in the column "850Hz", then the value 3000 should be considered (according to the association stablished between "850Hz" and 3000 in the <code>data.table</code> Info). Then, if there's a non-zero number in the column "1800Hz" it should consider the value 2000 and so on for the other 2 columns. At the end, it should calculate the maximum of all the considered values for that row and write that in the new column 'Range'.
And so on for the rest of the rows.</p>

<p>Someone suggested me to use this piece of code:</p>

<pre><code>Info[, {
  DT[, Range := pmax(Range, (get(Freq) != 0) * Rng, na.rm = TRUE)]
  NULL
}, by = Freq]
</code></pre>

<p>I don't understand how it works but it does the job. My question is, why if I wanted to have the MINIMUM of the considered values for each row instead of the maximum? If I use <code>pmin</code>, for this specific example all the values in the column 'Range' will be 0 because it <em>does</em> consider the cases where the value is 0 in the columns ("850Hz", etc), and I suppose it multiplies that zero by the respective value. I need to tell it to ignore the zeros in those 4 columns. How to?</p>

<hr>

<p>A possible answer would be to convert all the zeros in those columns into <code>N/A</code>'s and then using na.rm = TRUE. But I'm having trouble figuring out how to do that selective substitution. The amount of columns to be considered (850Hz, 1800Hz, 2100Hz, 2600Hz in this example) <em>could be smaller than 4</em>. That's because depending of the data, one or more of those 4 columns could be missing. What I would need is to consider all the columns in DT whose name exists in the vector Info[,Freq]. Then, change all the 0's in those columns only, for <code>N/A</code>'s. I've been trying and failing every time.</p>