列表中的子集矩阵

时间:2018-02-01 17:03:42

标签: r subset

我一直试图将rownames采用这种格式的矩阵进行子集化 - " chr8:56979853-56987069_RPS20_ENSG00000008988.5"

我尝试像这样的子集;

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

然而,它错误,因为模式只采用第一个元素。有没有办法按元素列表进行子集化。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分: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包,或者您可以使用base 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)