我有一个来自于walks对象的列表igraph:
> walks
[[1]]
+ 3/10 vertices, named, from d1edbf2:
[1] C O D
[[2]]
+ 3/10 vertices, named, from d1edbf2:
[1] C O J
[[3]]
+ 3/10 vertices, named, from d1edbf2:
[1] C O N
[[4]]
+ 3/10 vertices, named, from d1edbf2:
[1] C O N我需要返回列表中唯一的子元素。预期结果是:
[[1]]
[1] C O D
[[2]]
[1] C O J
[[3]]
[1] C O N我尝试在玩具示例列表上使用unique()函数
list1 = list(c("C", "O", "D"), c("C", "O", "J"), c("C", "O", "N"), c("C", "O", "N"))
unique(list1)我已经得到了预期的结果。
问题。如何获得来自igraph对象的列表的结果?
编辑
> dput(walks)
list(structure(c(1L, 7L, 2L), .Names = c("C", "O", "D"), env = <weak reference>, graph = "d1edbf24-043a-11ea-aa53-d7a9b9f5ae37", class = "igraph.vs"),
structure(c(1L, 7L, 4L), .Names = c("C", "O", "J"), class = "igraph.vs", env = <weak reference>, graph = "d1edbf24-043a-11ea-aa53-d7a9b9f5ae37"),
structure(c(1L, 7L, 6L), .Names = c("C", "O", "N"), env = <weak reference>, graph = "d1edbf24-043a-11ea-aa53-d7a9b9f5ae37", class = "igraph.vs"),
structure(c(1L, 7L, 6L), .Names = c("C", "O", "N"), env = <weak reference>, graph = "d1edbf24-043a-11ea-aa53-d7a9b9f5ae37", class = "igraph.vs"))在简短的形式中,我看到:
> dput(walks, control = NULL)
list(c(1, 7, 2), c(1, 7, 4), c(1, 7, 6), c(1, 7, 6))发布于 2019-11-11 11:40:16
唯一的函数是正确的选择,在这种情况下,它不能工作,因为列表中的元素有一个类"igraph“。您需要先提取出名称,然后应用唯一的名称。
您的dput中有一些奇怪的"env“,因此我模拟了下面的一些数据来说明这一点:
library(igraph)
set.seed(111)
g <- make_ring(9, directed = TRUE) %u%
make_star(10, center = 10) + edge(10, 1)
g <- set.vertex.attribute(g, "name", value=letters[1:10])
result = lapply(1:5,function(i)random_walk(g, start = 1, steps = 3))您可以得到与您的示例类似的内容:
> result
[[1]]
+ 3/10 vertices, named, from 0105e1a:
[1] a b c
[[2]]
+ 3/10 vertices, named, from 0105e1a:
[1] a b c
[[3]]
+ 3/10 vertices, named, from 0105e1a:
[1] a j a
[[4]]
+ 3/10 vertices, named, from 0105e1a:
[1] a j a
[[5]]
+ 3/10 vertices, named, from 0105e1a:
[1] a j a
> class(result[[1]])
[1] "igraph.vs"您可以检查这两个不同的输出:
# does not work for you
unique(walks)
# this works
unique(lapply(walks,names))
> unique(lapply(walks,names))
[[1]]
[1] "a" "j" "a"
[[2]]
[1] "a" "b" "c"
[[3]]
[1] "a" "b" "j"https://stackoverflow.com/questions/58796061
复制相似问题