如何设置两个条件的for循环

问题描述 投票:0回答:1

我编写了这个 for 循环来计算 R 代码中 seurat 中的标记。

seurat.integrated$res.01
"C0"
"C5"
。我需要计算每个簇的标记。 重点是,对于每个周期,我需要更改
list[[]]$gene
的 nr,因为列表中的每个元素对应于一个簇。因此,对于 C0,
list[[]]
的 nr 应为 1,对于 C1 应为 2,依此类推。我怎样才能把它写在循环中?谢谢!

for (i in seurat.integrated$res.01) {
  results <- FindMarkers(seurat.integrated,
                         ident.1 = paste(i, "day4", sep = "_"),
                         ident.2 = paste(i, "day6", sep = "_"),
                         features = list[[]]$gene,
                         logfc.threshold = -Inf,
                         test.use = "wilcox",
                         min.pct = 0.2,
                         min.cells.group = 0,
                         )

  }
r for-loop
1个回答
0
投票

没有数据:似乎可以通过使用

which()
获取向量的数字索引来解决问题。

注意:只有当 C0、C1、C2 ... 等每个仅出现一次时,这才按预期工作。

clusters <- c("C0", "C1", "C2", "C3", "C4", "C5")
li <- list(a = "a", b = "b", c = "c", d = "d", e = "e", f = "f")

for (i in clusters) {
  index <- which(clusters == i)
  li[[index]] |> print()
}
#> [1] "a"
#> [1] "b"
#> [1] "c"
#> [1] "d"
#> [1] "e"
#> [1] "f"
© www.soinside.com 2019 - 2024. All rights reserved.