【发布时间】:2015-12-14 08:13:26
【问题描述】:
我想在注释data.frame 的多个列下收集术语。
下面是 annot 的玩具数据集的第一行信息。
colnames(annot)
# [1] "HUGO.Name" "Common.Name" "Gene.Class" "Cell.Type" "Annotation"
annot[1,]
# HUGO.Name Common.Name Gene.Class Cell.Type
# 1 CCL1 CCL1 Immune Response - Cell Type specific aDC
# Annotation
# 1 Cell Type specific, Chemokines and receptors, Inflammatory response
到目前为止,我一直在迭代地编写 colnames,但我想学习如何编写一个函数来循环遍历 annot 的所有列(以及更普遍的其他 data.frames)。
这是我的手动方法:
yA <- unique(str_trim(unlist(strsplit(annot[, "Annotation"], ","))))
yC <- unique(str_trim(unlist(strsplit(annot[, "Cell.Type"], ","))))
yA
# [1] "Cell Type specific" "Chemokines and receptors"
# [3] "Inflammatory response" "Cytokines and receptors"
# [5] "Chronic inflammatory response" "Th2 orientation"
# [7] "T-cell proliferation" "Defense response to virus"
# [9] "B-cell receptor signaling pathway" "CD molecules"
# [11] "Regulation of immune response" "Adaptive immune response"
# [13] "Antigen processing and presentation"
如何构造函数“y”来简化此过程?我尝试了以下方法:
y <- function (i,n) {unique(str_trim(unlist(strsplit(i[, as.name(n)], ","))))}
但是,当我尝试使用此功能时出现错误。
yA <- y(annot, Annotation)
# Error in .subset(x, j) : invalid subscript type 'symbol'
# Called from: `[.data.frame`(i, , as.name(n))
我打算使用 yA 和 yC 的输出来制作如下列表:
# look up associated HUGO.Name per each term of yA
for (i in yA) {
eval(call("<-", as.name(i),
annot[grepl(i, annot[,"Annotation"], fixed =T), "HUGO.Name"]))
}
# make lists
nSannot_list<- mget(yA)
【问题讨论】:
-
我不确定我是否理解您在更新中的意图。你能以我的简化样本数据为例吗?
-
谢谢!它完全实现了我的想法