r - 使用 purrr::map 重命名更大列表中列表中元素的行名?
问题描述
{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))
我不知道如何为此制作一个代表,以允许加载文件。如何使函数与 purrr 一起工作并允许在列表元素内重命名?
由reprex 包(v0.3.0)于 2020-12-01 创建
解决方案
从函数返回更改后的数据框。
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)
推荐阅读
- android - Unity 2019 - SDK 工具版本 0.0 < 26.1.1
- javascript - Nuxt.js中如何导入three.js OBJLoader?“不能在模块外使用 import 语句”
- python - 如何从弹性搜索中的特定索引中获取数据?
- ada - 以 V 形 (ADA) 绘制带有斜十字的旗帜
- eslint - 嵌套目录的 ESlint 覆盖规则
- android-source - 如何在 selinux 许可模式下运行 Cuttlefish 虚拟 Android 设备?
- java - 单元测试 Spring REST API 服务(更新(PUT 方法))
- mysql - 如何编写 MySQL 查询以在我的用户数据集中查找唯一的人?
- r - 根据条件对数据框进行重复观察
- javascript - 从另一个文件 javascript 中的函数导入 const