Search code examples
rlistdata-wranglingr-colnames

How do I make the name of a list the name of its column?


I have a set of 270 RNA-seq samples, and I have already subsetted out their expected counts using the following code:

for (i in 1:length(sample_ID_list)) {
  assign(sample_ID_list[i], subset(get(sample_file_list[i]), select = expected_count)) 
  }

Where sample_ID_list is a character list of each sample ID (e.g., 313312664) and sample_file_list is a character list of the file names for each sample already in my environment to be subsetted (e.g., s313312664).

Now, the head of one of those subsetted samples looks like this:

> head(`308087571`)
# A tibble: 6 x 1
  expected_count
           <dbl>
1           129 
2             8 
3           137 
4          6230.
5          1165.
6             0 

The problem is I want to paste all of these lists together to make a counts dataframe, but I will not be able to differentiate between columns without their sample ID as the column name instead of expected_count.

Does anyone know of a good way to go about this? Please let me know if you need any more details!


Solution

  • Update: Thank you all so much for your help. I had to update my for loop as follows:

    for (i in 1:length(sample_ID_list)) {
      assign(sample_ID_list[i], subset(get(sample_file_list[i]), select = expected_count)) 
      data<- get(sample_ID_list[i])
      colnames(data)<- sample_ID_list[i]
      assign(sample_ID_list[i],data)
      }
    

    and was able to successfully reassign the names!