【问题标题】:subset matrix from a list列表中的子集矩阵
【发布时间】:2018-02-01 17:03:42
【问题描述】:

我一直在尝试对行名采用这种格式的矩阵进行子集化 - “chr8:56979853-56987069_RPS20_ENSG00000008988.5”

我试着像这样子集化;

include_list <- c("RPS20", "VIL1", "KRT20", "CLDN7")
goi <- subset(mat2, rownames(mat2) %like% include_list)

但它会出错,因为该模式只采用第一个元素。有没有办法通过元素列表进行子集化。任何帮助表示赞赏。

【问题讨论】:

  • 查找有关“在字符串中搜索模式”的问题。你可以做类似subset(mat2, grepl(pattern = paste(include_list, collapse = "|"), x = rownames(mat2))的事情。
  • grep 是一种选择,通过基因名称注意“cdc2”和“cdc28”等情况。另一种选择是拆分行名提取基因名称,然后进行匹配。 subset(mat2, sapply(strsplit(rownames(mat2), "_"), "[[", 2) %in% include_list))
  • 这行得通 - goi

标签: r subset


【解决方案1】:
out <- c()
for (x in include_list) {
  check_each <- rownames(mat2)[rownames(mat2) %like% include_list]
  out <- c(out, check_each)
}
goi <- subset(mat2, rownames(mat2) %in% out)

请注意,%like% 来自 DescTools 包,或者您可以使用基本 R:

out <- c()
for (x in include_list) {
  check_each <- rownames(mat2)[grepl(x, rownames(mat2))]
  out <- c(out, check_each)
}
goi <- subset(mat2, rownames(mat2) %in% out)

【讨论】:

  • OP 需要在字符串中搜索,而不是找到完全匹配的。
  • 另外提到你使用的包%like%来自,它不在Base中。
猜你喜欢
  • 2018-01-03
  • 1970-01-01
  • 2022-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-19
  • 2016-06-25
  • 2017-05-10
相关资源
最近更新 更多