R- lapply preserve rownames
<p>I have a function that creates a melted (reshape2 package) table from another one and returns it with a transformation of the original name as its name. I need to apply it to a list of dataframes. Since the variables that are in "id.var" parameter of melt function are the rownames of the original table, I'm having issues preserving them.</p>

<p>My original function is:</p>

<pre><code>creaMelts&lt;-function(tbl){
    library(reshape2)
    texto&lt;-deparse(substitute(tbl))
    tbl2&lt;-melt(tbl, id.vars=rownames(tbl))
    texto2&lt;-substr(texto,4,nchar(texto))
    colnames(tbl2)&lt;-c('userId','movieId', texto2)
    tblName&lt;&lt;-paste0('df', texto2)
    assign(tblName, tbl2)
    return(assign(tblName, tbl2, envir = .GlobalEnv ) )
}
</code></pre>

<p>A couple of data frames:</p>

<pre><code>tbl1&lt;-data.frame(a=seq(1,2,1), 'X1'=seq(2,3,1), 'X2'=seq(3,4,1))
rownames(tbl1)&lt;-c('X1', 'X2')
tbl2&lt;-data.frame(a=seq(1,2,1), 'X1'=seq(3,4,1), 'X2'=seq(4,5,1))
rownames(tbl2)&lt;-c('X1', 'X2')
</code></pre>

<p>So if I run:</p>

<pre><code>creaMelts(tbl1)
</code></pre>

<p>And then I ask for df1, I get the desired result:</p>

<pre><code>  userId movieId 1 NA
      2       3 a  1
      3       4 a  2
</code></pre>

<p>Since I need to do this fo a list:</p>

<pre><code>lista=list(tbl1=tbl1,tbl2=tbl2)
</code></pre>

<p>I wrote:</p>

<pre><code>lapply(seq_along(lista),function(tbl,n,rn,i){
    library(reshape2)
    texto&lt;-n[[i]]
    print(texto)
    print(rn[[i]])
    tbl2&lt;-melt(tbl, id.vars=rn[[i]])
    texto2&lt;-substr(texto,4,nchar(texto))
    colnames(tbl2)&lt;-c('userId','movieId', texto2)
    tblName&lt;-paste0('df', texto2)
    assign(tblName, tbl2)
    return(assign(tblName, tbl2, envir = .GlobalEnv ) )
}, tbl=lista,n=names(lista), rn=rownames(lista))
</code></pre>

<p>Following the idea that adding the parameter to lapply will preserve the rownames like it does with the names, but it doesn't.
In this case it does print each data frame name, but for the rownames I get NULL.</p>

<p>If I ask now for df1, I get</p>

<pre><code>   userId movieId    1
       a       1 tbl1
       a       2 tbl1
      X1       2 tbl1
      X1       3 tbl1
      X2       3 tbl1
      X2       4 tbl1
       a       1 tbl2
       a       2 tbl2
      X1       3 tbl2
      X1       4 tbl2
      X2       4 tbl2
      X2       5 tbl2
</code></pre>

<p>Which is not the desired result.</p>

<p><strong>How can I also use the rownames of each element of the list?</strong>
<strong>Also I would like to avoid the printing of the table in the screen.</strong></p>