1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| function (x, matrix.type = NULL, directed = TRUE, hyper = FALSE,
loops = FALSE, multiple = FALSE, bipartite = FALSE, ignore.eval = TRUE,
names.eval = NULL, na.rm = FALSE, edge.check = FALSE, ...)
{
nattr <- attr(x, "n")
battr <- attr(x, "bipartite")
vattr <- attr(x, "vnames")
if (is.logical(x)) {
x <- 1 * x
}
if (is.null(matrix.type))
matrix.type <- which.matrix.type(x)
else matrix.type <- match.arg(matrix.type, c("adjacency",
"incidence", "edgelist", "bipartite"))
if (is.logical(bipartite) && bipartite)
matrix.type <- "bipartite"
if ((bipartite > 0) && (matrix.type == "adjacency") && (NROW(x) ==
bipartite))
matrix.type <- "bipartite"
unames <- NULL
if (matrix.type == "edgelist") {
if (dim(x)[2] > 2)
vals <- x[, -(1:2)]
else vals <- NULL
if (is.character(x <- as.matrix(x[, 1:2, drop = FALSE]))) {
unames <- sort(unique(as.vector(x)))
x <- cbind(match(x[, 1], unames), match(x[, 2], unames))
}
if (!is.null(vals))
x <- cbind(x, vals)
}
if (matrix.type == "adjacency" && !is.null(colnames(x))) {
unames <- colnames(x)
}
if (matrix.type == "bipartite") {
directed <- FALSE
bipartite <- dim(x)[1]
unames <- 1:sum(dim(x))
if (!is.null(rownames(x))) {
unames[1:(dim(x)[1])] <- rownames(x)
}
if (!is.null(colnames(x))) {
unames[(dim(x)[1]) + (1:(dim(x)[2]))] <- colnames(x)
}
}
if (!is.null(vattr))
unames <- vattr
n <- switch(matrix.type, adjacency = dim(x)[1], incidence = dim(x)[1],
bipartite = sum(dim(x)), edgelist = max(x[, 1:2]), )
if (is.numeric(nattr))
n <- nattr
if (is.numeric(battr))
bipartite <- battr
g <- network.initialize(n, directed = directed, hyper = hyper,
loops = loops, multiple = multiple, bipartite = bipartite)
g <- switch(matrix.type, adjacency = network.adjacency(x,
g, ignore.eval, names.eval, na.rm, edge.check), incidence = network.incidence(x,
g, ignore.eval, names.eval, na.rm, edge.check), bipartite = network.bipartite(x,
g, ignore.eval, names.eval, na.rm, edge.check), edgelist = network.edgelist(x,
g, ignore.eval, names.eval, na.rm, edge.check))
if (!is.null(unames)) {
g <- set.vertex.attribute(g, "vertex.names", unames)
}
g
}
<environment: namespace:network> |
Partager