使用 purrr::map 重命名更大列表中列表中元素的行名称?

using purrr::map to rename row names in elements within lists in a bigger list?

{library(Seurat)
library(tidyverse)
library(purrr)}



dirNames <- unique(dirname(list.files("data/scRNA_CITE",
                          full.names = T,
                          recursive = T)))


######


#reading multiple directories and a resultant list
dat <- purrr::map(dirNames, Read10X)
names(dat) <- dirNames

#reading just one file
#M06 <- Read10X(data.dir = "data/m06/filtered_feature_bc_matrix/")

#renaming the rows of a list element (here named antibody capture) within one list works! 
rownames(x = M06[["Antibody Capture"]]) <- gsub(pattern = "*_TotalSeqC", replacement = "", 
                                                x = rownames(x = M06[["Antibody Capture"]]))

#creating function for purrr
change_rname <- function(x){
  rownames(x[["Antibody Capture"]]) <- sub(pattern = "*_TotalSeqC", replacement = "", x[["Antibody Capture"]])
}

  
 # using the same function to rename multiple elements within multiple lists of a bigger list works temporarily BUT DOES not get saved within the bigger list
purrr::map(dat,
           ~change_rname(.x))

我不确定如何为此制作一个 reprex,以允许加载文件。如何使该函数与 purrr 一起工作并允许在列表元素内重命名?

reprex package (v0.3.0)

于 2020-12-01 创建

Return 从函数更改的数据帧。

change_rname <- function(x){
  rownames(x[["Antibody Capture"]]) <- sub(pattern = "*_TotalSeqC",
                                       replacement = "", x[["Antibody Capture"]])
  return(x)
}
#apply function with `purrr:map`
result <- purrr::map(dat,change_rname)