我的目标是在每种样品类型内生成一个重复的命名列表,这样我就可以用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"
答案 0 :(得分:3)
您可以使用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"
答案 1 :(得分:2)
必须在数据集中输入和输出数据,这似乎过于复杂。您可以使用Map
对其进行循环,它会根据第一个参数自动命名:
Map(paste, samples_base, list(replicates), sep=".")
#List of 4
# $ cell.wt.control : chr [1:3] "cell.wt.control.A" "cell.wt.control.B" ...
# $ cell.wt.protein_ko : chr [1:3] "cell.wt.protein_ko.A" "cell.wt.protein_ko.B" ...
# $ cell.mutant.control : chr [1:3] "cell.mutant.control.A" "cell.mutant.control.B" ...
# $ cell.mutant.protein_ko: chr [1:3] "cell.mutant.protein_ko.A" "cell.mutant.protein_ko.B" ...