【发布时间】:2019-06-27 19:17:58
【问题描述】:
我想提取 seq.df(单列 df)中与匹配 map.list(列表列表)中的索引匹配的蛋白质。
示例数据:
seq.df<- rbind.data.frame("MTHISPAVYGLWAIMSVLLAAFCAY",
"MERSSAIVFPNVGTSVLSATIHLVGVTVLAHLISRRTALRGTST",
"MLFEPFWCLLDLLRWSLDTHYIPAKRPLNGGGRSSNFD")
map.list<- list(a<- list(2,3,4,5,6,7),
b<- list(13,14,30,31,32),
c<- list(5,6,10,11))
期望的输出:
THISPA
GTAHL
PFLD
如果我只对 map.list 的第一个子列表运行嵌套应用,我会得到我想要的第一个蛋白质:
prot.list<- apply(seq.df, 1, function (x) lapply(map.list[[1]], function (y) substring(x, y, y)))
返回第一个序列的预期结果 (THISPA,)
但我不确定如何让这个函数遍历 map.list 中的所有子列表。我试图将它包装到一个 for 循环中,但它没有给我预期的结果:
for (i in seq_along(map.list)){
each.map.list<- map.list[[i]]
prot.list<- apply(seq.df, 1, function (x) lapply(each.map.list, function (y) substring(x, y, y)))
}
输出:
SPGL
SAPN
PFLD
我宁愿添加另一个 lapply 语句,但我不确定如何在 map.list 中指定每个列表
#this does not work, but something like:
prot.list<- apply(seq.df, 1, function (x) lapply(map.list, function (y) lapply([[y]], function (z) substring(x, z, z)))
【问题讨论】:
标签: r nested-loops lapply