在R中的igraph对象中存储函数返回值

时间:2018-05-25 06:21:26

标签: r igraph

我有一个返回名为readFn()的igraph对象的函数。我想使用for循环和存储结果

来调用此函数
files <- c("345","456")

for(i in files){
  g <- readFn(i) 
}

#Error: object 'g' not found
#In addition: There were 50 or more warnings (use warnings() to see the first #50)


readFn <- function(i) {
#some lines
graph #graph -> is an igraph object to be returned to for loop
}

我想在名为g_345和g_456

的igraph对象中获得2个图形

1 个答案:

答案 0 :(得分:1)

使用 lapply

library(igraph)

# example function that returns igraph object
readFn <- function(i) {
  graph.empty(n = 2)
  }

files <- c("345","456")

g <- lapply(files , readFn)

# assign names
names(g) <- paste0("g_", files)

然后我们可以按名称访问每个

g$g_345

# IGRAPH 598e70c D--- 2 0 -- 
# + edges from 598e70c: