lapply alternative返回长度与输入不同列表

yws3nbqq  于 2023-04-18  发布在  其他
关注(0)|答案(2)|浏览(97)

我一直在使用lapply来处理列表,但是我不知道如何确切地知道如何返回我想要的结果。
我有一小部分数据(我不得不从dput中删除''环境'〉',所以看起来有点奇怪)

example <- list(structure(c(E10 = 1L, E82 = 2L, D81 = 4L, E04 = 12L, E06 = 15L
), class = "igraph.vs", graph = "9eef1560-da23-11ed-9a93-8f4ed5a9da01"), 
structure(c(E10 = 1L, E82 = 2L, D81 = 4L, E04 = 12L, E09 = 16L
), class = "igraph.vs", graph = "9eef1560-da23-11ed-9a93-8f4ed5a9da01"), 
structure(c(E10 = 1L, E82 = 2L, D44 = 10L, E05 = 5L, A84 = 14L
), class = "igraph.vs", graph = "9eef1560-da23-11ed-9a93-8f4ed5a9da01"))
[[1]]
+ 5/? vertices, named, from 9eef156 (deleted):
[1] E10 E82 D81 E04 E06

[[2]]
+ 5/? vertices, named, from 9eef156 (deleted):
[1] E10 E82 D81 E04 E09

[[3]]
+ 5/? vertices, named, from 9eef156 (deleted):
[1] E10 E82 D44 E05 A84

我的目标是遍历列表中的每个元素,并返回3个列表。From,To和Group。Group是列表中的第一个标签,在最初的例子中是E10,然后from和to只是成对地移动标签,所以E10和E82,然后E82和D81等等。我已经用下面的代码近似地实现了我想要实现的目标。注意:traj_length在这种情况下是值为5的变量,将随使用而改变。

output <- lapply(example, function(x) list(from = attributes(x)$names[1:(traj_length-1)], to = attributes(x)$names[2:(traj_length)], group = rep(attributes(x)$names[1], traj_length-1)))
> output
[[1]]
[[1]]$from
[1] "E10" "E82" "D81" "E04"

[[1]]$to
[1] "E82" "D81" "E04" "E06"

[[1]]$group
[1] "E10" "E10" "E10" "E10"

[[2]]
[[2]]$from
[1] "E10" "E82" "D81" "E04"

[[2]]$to
[1] "E82" "D81" "E04" "E09"

[[2]]$group
[1] "E10" "E10" "E10" "E10"

[[3]]
[[3]]$from
[1] "E10" "E82" "D44" "E05"

[[3]]$to
[1] "E82" "D44" "E05" "A84"

[[3]]$group
[1] "E10" "E10" "E10" "E10"

这是接近的,但不是每个元素(1,2,3等)的列表,我想返回3个列表,每个值都在那里,并且值被分离出来,这样我就可以轻松地转换为dataframe。看起来像这样:

$from[[1]]
"E10"

$to[[1]]
"E82"

$group[[1]]
"E10"

$from[[2]]
"E82"

$to[[2]]
"D81"

$group[[2]]
"E10"

任何建议或意见将不胜感激!
编辑:
对这样的评论作出回应;我们有一个基于数据集中每对的计数选择的对列表。我们的目标是组装指定长度的所有简单路径我已经创建了一个包含数据中所有简单路径的列表,过滤长度为5的。现在只需重新组合它们,并在绘制或应用LCS之前为每个分配一个组(最长公共子序列)得分来过滤最频繁的路径。

z9gpfhce

z9gpfhce1#

在R中,你可以做:

fn2<- function(x){
    y <- names(x)
    z <- data.frame(from = head(y, -1), to = y[-1], group=y[1])
    lapply(asplit(z, 1), as.list)
  }
lapply(example, fn2)

您也可以从purrr使用transpose,如下所示:

fn <- function(x){
   y <- names(x)
   data.frame(from = head(y, -1), to = y[-1], group=y[1])
 }
library(purrr)
map(example, ~ transpose(fn(.x)))

[[1]]
[[1]][[1]]
[[1]][[1]]$from
[1] "E10"

[[1]][[1]]$to
[1] "E82"

[[1]][[1]]$group
[1] "E10"

[[1]][[2]]
[[1]][[2]]$from
[1] "E82"

[[1]][[2]]$to
[1] "D81"

[[1]][[2]]$group
[1] "E10"

[[1]][[3]]
[[1]][[3]]$from
[1] "D81"

[[1]][[3]]$to
[1] "E04"

[[1]][[3]]$group
[1] "E10"
t98cgbkg

t98cgbkg2#

这正是purrr::list_transpose()所做的。

library(purrr)

list_transpose(l)
#> $from
#> $from[[1]]
#> [1] "E10" "E82" "D81" "E04"
#> 
#> $from[[2]]
#> [1] "E10" "E82" "D81" "E04"
#> 
#> $from[[3]]
#> [1] "E10" "E82" "D44" "E05"
#> 
#> 
#> $to
#> $to[[1]]
#> [1] "E82" "D81" "E04" "E06"
#> 
#> $to[[2]]
#> [1] "E82" "D81" "E04" "E09"
#> 
#> $to[[3]]
#> [1] "E82" "D44" "E05" "A84"
#> 
#> 
#> $group
#> $group[[1]]
#> [1] "E10" "E10" "E10" "E10"
#> 
#> $group[[2]]
#> [1] "E10" "E10" "E10" "E10"
#> 
#> $group[[3]]
#> [1] "E10" "E10" "E10" "E10"

数据:

l <- list(
  list(
    from = c("E10", "E82", "D81", "E04"),
    to = c("E82", "D81", "E04", "E06"),
    group = c("E10", "E10", "E10", "E10")
  ),
  list(
    from = c("E10", "E82", "D81", "E04"),
    to = c("E82", "D81", "E04", "E09"),
    group = c("E10", "E10", "E10", "E10")
  ),
  list(
    from = c("E10", "E82", "D44", "E05"),
    to = c("E82", "D44", "E05", "A84"),
    group = c("E10", "E10", "E10", "E10")
  )
)

相关问题