I'd like to collect terms under multiple columns of the annot data.frame
.
Below is the first row of information for a toy datset for 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
So far, I've been writing the colnames
iteratively, but I'd like to learn how to write a function to loop through all columns of annot (and more generally other data.frames
).
This is my manual approach:
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"
How can I construct a function "y" to simplify this process? I've tried the following:
y <- function (i,n) {unique(str_trim(unlist(strsplit(i[, as.name(n)], ","))))}
However, I get an error when I try to use this function.
yA <- y(annot, Annotation)
# Error in .subset(x, j) : invalid subscript type 'symbol'
# Called from: `[.data.frame`(i, , as.name(n))
What I intend is to use the output of yA and yC to make lists as follows:
# 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)