Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在R中返回igraph列表的唯一子元素_R_List_Unique_Igraph - Fatal编程技术网

如何在R中返回igraph列表的唯一子元素

如何在R中返回igraph列表的唯一子元素,r,list,unique,igraph,R,List,Unique,Igraph,我有一个列表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

我有一个列表
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()
函数:

并取得了预期的效果

问题。如何获取来自
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"))

unique函数是正确的选择,在这种情况下它不起作用,因为列表中的元素有一个类“igraph”。您需要首先提取名称,然后应用unique

在您的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))
您可以检查两种不同的输出:

# 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"

你能分享dput(walks)?@ronaksah,我添加了dput()函数的输出。谢谢,代码对我有用。我的dput中出现一些奇怪的“env”的原因是什么?你有什么想法吗?嗨,尼克,很高兴这对你有用。至于“env”,如果您使用我的示例并执行str(g),您可以看到attr(,“env”)=和attr(,“graph”)=chr“841251eb-4389-4933-b79a-8740794a0e3f”。这将是不同于你的,所以我想这是一些设置igraph使用。。。不太确定。
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"