简体   繁体   中英

How to convert large list object (class of igraph.vs) to a dataframe in R

g is an igraph object. I wish to find the cliques ( mylist ), and then convert this large list object to a dataframe object. ie one column with the clique number, another column with the members of this clique.

mylist = maximal.cliques(g)

# error here when converting to dataframe 
cliques_df = mylist %>% 
   map_df(as_tibble)

However, the code produces the error: Error in as.data.frame.default(value, stringsAsFactors = FALSE): cannot coerce class '"igraph.vs"' to a data.frame

EDIT:

Running vertex_attr_names(g) produces "NodeID" (so NodeID is the node attribute).

However, my g (igraph object) does not seem to have the NodeID displayed as attribute. Is this normal?

G

Link to data file: https://drive.google.com/drive/folders/14eiJhW499lMM5BKaU4Qau-B7ieZCrSKx?usp=sharing

UPDATE: In the attached example you have the clique numbers and clique members in a dataframe. When you are using maximal.cliques(g) the attribute name is kept, but attribute PaperID seems to be dropped. You have to do the following assignment for the attribute name : V(g)$name <- NodeIds and use attributes(x)$name in the second sapply . Take a closer look to the working example attached. I've stated the problem.

library(igraph)
#> 
#> Attache Paket: 'igraph'
#> The following objects are masked from 'package:stats':
#> 
#>     decompose, spectrum
#> The following object is masked from 'package:base':
#> 
#>     union


g <- sample_gnp(100, 0.3)
NodeIds <- paste("A", 1:100, sep =":")
V(g)$name <- NodeIds
V(g)$PaperID <- NodeIds
mylist <- maximal.cliques(g)


clique_number <- sapply(mylist, length)
clique_members <- sapply(mylist, function(x) paste("'", attributes(x)$name, "'", collapse = ",", sep = ""))
str(clique_members)
#>  chr [1:2035] "'A:87','A:81','A:57'" "'A:87','A:81','A:77','A:69'" ...


# empty character vector!!! 
cliques_members2 <- sapply(mylist, function(x) paste("'", attributes(x)$PaperID, "'", collapse = ",", sep = ""))
str(cliques_members2)
#>  chr [1:2035] "''" "''" "''" "''" "''" "''" "''" "''" "''" "''" "''" "''" ...

cliques_df <- data.frame(cliqueNums = clique_number, cliqueMembs = clique_members)
head(cliques_df, n = 10)
#>    cliqueNums                 cliqueMembs
#> 1           3        'A:87','A:81','A:57'
#> 2           4 'A:87','A:81','A:77','A:69'
#> 3           3        'A:87','A:79','A:69'
#> 4           4 'A:87','A:79','A:75','A:51'
#> 5           4 'A:87','A:79','A:75','A:65'
#> 6           3        'A:87','A:69','A:91'
#> 7           3        'A:87','A:65','A:28'
#> 8           3        'A:87','A:65','A:17'
#> 9           3        'A:87','A:57','A:28'
#> 10          3        'A:87','A:57','A:46'

# checks:
mylist[1:10]
#> [[1]]
#> + 3/100 vertices, named, from c3ed415:
#> [1] A:87 A:81 A:57
#> 
#> [[2]]
#> + 4/100 vertices, named, from c3ed415:
#> [1] A:87 A:81 A:77 A:69
#> 
#> [[3]]
#> + 3/100 vertices, named, from c3ed415:
#> [1] A:87 A:79 A:69
#> 
#> [[4]]
#> + 4/100 vertices, named, from c3ed415:
#> [1] A:87 A:79 A:75 A:51
#> 
#> [[5]]
#> + 4/100 vertices, named, from c3ed415:
#> [1] A:87 A:79 A:75 A:65
#> 
#> [[6]]
#> + 3/100 vertices, named, from c3ed415:
#> [1] A:87 A:69 A:91
#> 
#> [[7]]
#> + 3/100 vertices, named, from c3ed415:
#> [1] A:87 A:65 A:28
#> 
#> [[8]]
#> + 3/100 vertices, named, from c3ed415:
#> [1] A:87 A:65 A:17
#> 
#> [[9]]
#> + 3/100 vertices, named, from c3ed415:
#> [1] A:87 A:57 A:28
#> 
#> [[10]]
#> + 3/100 vertices, named, from c3ed415:
#> [1] A:87 A:57 A:46

mylist[[22]]
#> + 5/100 vertices, named, from c3ed415:
#> [1] A:21 A:67 A:62 A:27 A:22
cliques_df[22, ]
#>    cliqueNums                        cliqueMembs
#> 22          5 'A:21','A:67','A:62','A:27','A:22'

Created on 2020-07-07 by the reprex package (v0.3.0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM