在列表列表上嵌套应用语句
Nested apply statement over a list of lists
我想提取 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)))
returns 第一个序列的预期结果 (THISSPA,)
但我不确定如何让这个函数遍历 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)))
我们可以用Map
unlist(Map(function(x, y) paste(substring(x, unlist(y),
unlist(y)), collapse=""), seq.df[[1]], map.list))
#[1] "THISPA" "GTAHL" "PFLD"
此外,我们可以在开始时做一个 unlist
并使用扁平化的 list
作为输入
,而不是 unlist
ing 两次
l1 <- lapply(map.list, unlist)
sapply(Map(substring, seq.df[[1]], first = l1, last = l1), paste, collapse="")
#[1] "THISPA" "GTAHL" "PFLD"
或者 map2
来自 purrr
library(purrr)
map2_chr(seq.df[[1]], map.list, ~ str_c(substring(.x,
unlist(.y), unlist(.y)), collapse=""))
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))
lapply(1:nrow(seq.df),
function(x)paste(strsplit(as.character(seq.df[x,]), "")[[1]][unlist(map.list[[x]])], collapse=""))
[[1]]
[1] "THISPA"
[[2]]
[1] "GTAHL"
[[3]]
[1] "PFLD"
这是一个使用mapply()
的解决方案
它使用匿名函数,使用seq.df的character-split字符串作为x,位置列表作为y。
mapply( function(x,y) paste0( x[ unlist(y) ], collapse = "" ),
x = stringr::str_split( seq.df[,1], pattern = ""),
y = map.list )
[1] "THISPA" "GTAHL" "PFLD"
我想提取 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)))
returns 第一个序列的预期结果 (THISSPA,)
但我不确定如何让这个函数遍历 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)))
我们可以用Map
unlist(Map(function(x, y) paste(substring(x, unlist(y),
unlist(y)), collapse=""), seq.df[[1]], map.list))
#[1] "THISPA" "GTAHL" "PFLD"
此外,我们可以在开始时做一个 unlist
并使用扁平化的 list
作为输入
unlist
ing 两次
l1 <- lapply(map.list, unlist)
sapply(Map(substring, seq.df[[1]], first = l1, last = l1), paste, collapse="")
#[1] "THISPA" "GTAHL" "PFLD"
或者 map2
来自 purrr
library(purrr)
map2_chr(seq.df[[1]], map.list, ~ str_c(substring(.x,
unlist(.y), unlist(.y)), collapse=""))
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))
lapply(1:nrow(seq.df),
function(x)paste(strsplit(as.character(seq.df[x,]), "")[[1]][unlist(map.list[[x]])], collapse=""))
[[1]]
[1] "THISPA"
[[2]]
[1] "GTAHL"
[[3]]
[1] "PFLD"
这是一个使用mapply()
它使用匿名函数,使用seq.df的character-split字符串作为x,位置列表作为y。
mapply( function(x,y) paste0( x[ unlist(y) ], collapse = "" ),
x = stringr::str_split( seq.df[,1], pattern = ""),
y = map.list )
[1] "THISPA" "GTAHL" "PFLD"