r - 从两个向量的所有组合生成命名列表
问题描述
我的目标是在每个样本类型中生成一个命名的重复列表,以便我可以使用samples$cell.wt.control
. 我有有效的代码,它只是荒谬的长,我觉得应该有一个更简单的方法。有关于如何从两个向量制作命名列表以及如何制作两个向量的所有组合的问题,但我找不到任何从两个向量的所有组合中制作命名列表,使用一个作为结果的名称。
library(tidyverse)
samples_base = c("cell.wt.control",
"cell.wt.protein_ko",
"cell.mutant.control",
"cell.mutant.protein_ko")
replicates = LETTERS[1:3]
samples =
outer(samples_base, replicates, paste, sep = ".") %>% t %>% as.data.frame %>%
set_names(samples_base) %>%
mutate_if(is.factor, as.character) %>%
pivot_longer(starts_with("cell")) %>%
group_by(name) %>% summarize(sample = list(value)) %>% deframe
#$cell.mutant.control
#[1] "cell.mutant.control.A" "cell.mutant.control.B" "cell.mutant.control.C"
#$cell.mutant.protein_ko
#[1] "cell.mutant.protein_ko.A" "cell.mutant.protein_ko.B" "cell.mutant.protein_ko.C"
#$cell.wt.control
#[1] "cell.wt.control.A" "cell.wt.control.B" "cell.wt.control.C"
#$cell.wt.protein_ko
#[1] "cell.wt.protein_ko.A" "cell.wt.protein_ko.B" "cell.wt.protein_ko.C"
解决方案
你可以这样做purrr
:
samples_base %>%
set_names() %>%
purrr::map(~ paste0(., ".", replicates))
输出:
$cell.wt.control
[1] "cell.wt.control.A" "cell.wt.control.B" "cell.wt.control.C"
$cell.wt.protein_ko
[1] "cell.wt.protein_ko.A" "cell.wt.protein_ko.B" "cell.wt.protein_ko.C"
$cell.mutant.control
[1] "cell.mutant.control.A" "cell.mutant.control.B" "cell.mutant.control.C"
$cell.mutant.protein_ko
[1] "cell.mutant.protein_ko.A" "cell.mutant.protein_ko.B" "cell.mutant.protein_ko.C"
推荐阅读
- css - 关闭按钮在引导反应弹出窗口中未垂直居中
- python - 尽管在 cmd 中成功安装,Python 仍显示“没有名为 SALib 的模块”
- bash - 使用 sed 查找和替换多行字符串
- python - struct.error: unpack 解压 c++ 结构时需要 288 字节的缓冲区
- linux - SSH 权限有什么区别?
- django - 自定义内置 django 模型表单
- node.js - NodeJS 无法连接到服务器 ECONNREFUSED 上的 MongoDB
- javascript - 即使以开发者工具为中心,文本也不以移动设备为中心
- ruby - Ruby 中的逐行分析器
- java - 使用 SpringBoot 1.2.2 版的 Swagger 无法正常工作