title: How to rename a column of a data frame with part of the data frame identifier in R?
body: <p>I've got a number of files that contain gene expression data. In each file, the gene name is kept in a column "Gene_symbol" and the expression measure (a real number) is kept in a column "RPKM". The file name consists of an identifier followed by _ and the rest of the name (ends with "expression.txt"). I would like to load all of these files into R as data frames, for each data frame rename the column "RPKM" with the identifier of the original file and then join the data frames by "Gene_symbol" into one large data frame with one column "Gene_symbol" followed by all the columns with the expression measures from the individual files, each labeled with the original identifier.</p>
  
  <p>I've managed to transfer the identifier of the original files to the names of the individual data frames as follows.</p>
  
  <pre><code>files &lt;- list.files(pattern = "expression.txt$")
  for (i in files) {var_name = paste("Data", strsplit(i, "_")[[1]][1], sep = "_"); assign(var_name, read.table(i, header=TRUE)[,c("Gene_symbol", "RPKM")])}
  </code></pre>
  
  <p>So now I'm at a stage where I have dataframes as follows:</p>
  
  <pre><code>Data_id0001 &lt;- data.frame(Gene_symbol=c("geneA","geneB","geneC"),RPKM=c(2.43,5.24,6.53))
  Data_id0002 &lt;- data.frame(Gene_symbol=c("geneA","geneB","geneC"),RPKM=c(4.53,1.07,2.44))
  </code></pre>
  
  <p>But then I don't seem to be able to rename the RPKM column with the id000x bit. (That is in a fully automated way of course, looping through all the data frames I will generate in the real scenario.)</p>
  
  <p>I've tried to store the identifier bit as a comment with the data frames but seem to be unable to assign the comment from within a loop. </p>
  
  <p>Any help would be appreciated,</p>
  
  <p>mce</p>