Use name of a named list inside for loop as a column name

Hi all,

Yet again, stumped by something that seemed simple.

I have a list of deseqdataset objects (1 from each of 5 tissues). I am extracting some data from each tissue, and storing the data in 1 column (for each tissue) in a tibble. So the final tibble has 6 columns. 1 for each tissue, and the join_by key (baseMean). The problem is I can't figure out how to assign the tissue name to each column without hard coding it.

The column names I want:

# Initialize the tibble
> pvalues_distribution <- tibble(baseMean = c("<10", "<100", "<1K", "<10K", "<100K", "<1M", "<10M")) %>% 
  mutate(baseMean = factor(baseMean, baseMean)) # maintains row order

image

# Populate the tibble
> for (name in names(deseq2_results$T1)) {

    # extract required columns + filter rows
    res <- as_tibble(data.frame(deseq2_results$T1[[name]])) %>%
      filter(pvalue < 0.1) %>%
      filter(!between(log2FoldChange, -1.5, 1.5)) %>%
      select(baseMean, pvalue, log2FoldChange)
   
    # calculate % frequency distribution of pvalues
    dist <- tibble(
      x = c(10, 100, 1000, 10000, 100000, 1000000, 10000000),
      baseMean = c("<10", "<100", "<1K", "<10K", "<100K", "<1M", "<10M")) %>%
      rowwise() %>%
      mutate(name = round(100 * (nrow(filter(res, baseMean < x)) / nrow(res)))) %>% 
      select(-1)

    # join each tissue
    pvalues_distribution <- left_join(pvalues_distribution, dist, join_by(baseMean))  
} 

image

As you can see, the setting of the column name for each tissue when I call mutate() is interpreted as 'name' instead of the tissue it came from.

Thanks for the help

Kenneth

1 Like

What happens if you replace this (above) by this (below)?

pvalues_distribution <- 
  left_join(pvalues_distribution, dist, join_by(baseMean)) |> 
  rename_with(
    \(x) str_replace(x, 'name', name), 
    contains('name')
  )

(I'm sure there's a better way, but this may work for now.)

1 Like

That works. Thank you again.

image

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.