R - Name concatenation separator in unlist() (flattening list of lists)
<p>It is possible to flatten lists of lists using <code>unlist(list, recursive = FALSE)</code>, as was shown in <a href="https://stackoverflow.com/questions/16300344/how-to-flatten-a-list-of-lists-in-r">this</a> question. This action concatenates list names using the default dot (<code>.</code>) separator, which is standard for variable naming in R. A simple example illustrates this:</p>

<pre><code># Create example list, l
&gt; l &lt;- list("a" = list("x" = 1, "y" = 2), "b" = list("x" = 3, "y" = 4))

&gt; l
$a
$a$x
[1] 1

$a$y
[1] 2


$b
$b$x
[1] 3

$b$y
[1] 4

# Unlist lists in l
&gt; l.unlisted &lt;- unlist(l, recursive = FALSE)

&gt; l.unlisted
$a.x
[1] 1

$a.y
[1] 2

$b.x
[1] 3

$b.y
[1] 4
</code></pre>

<p>In spite of the standard naming convention, I want the names to have a different separator (<code>_</code>). It is possible to do this through string manipulation by using <code>sub</code> to find and replace the default <code>.</code> separator in each name after concatenation has already taken place once in <code>unlist()</code>, as follows:</p>

<pre><code>&gt; names(l.unlisted) &lt;- sub('.', '_', names(l.unlisted), fixed=TRUE)

&gt; l.unlisted
$a_x
[1] 1

$a_y
[1] 2

$b_x
[1] 3

$b_y
[1] 4
</code></pre>

<p>While this would be sufficient in most situations, I think that the extra concatenation step can be eliminated by altering the default separator used by <code>unlist()</code>. I hypothesize that this can be done by altering the source code of the function using <code>fix()</code> by adding a <code>sep</code> argument similar to the the one used in <code>paste()</code>. However, I do not know how to do so, as <code>unlist()</code> is an internal function.</p>

<p>Is there a way to alter the default name concatenation separator in <code>unlist()</code>, and how can this be done?</p>