library("network") set.seed(1702) data("flo") data("emon") net <- network.initialize(5) net nmat <- matrix(rbinom(25, 1, 0.5), nr = 5, nc = 5) net <- network(nmat, loops = TRUE) net summary(net) all(nmat == net[,]) net <- as.network(nmat, loops = TRUE) all(nmat == net[,]) nflo <- network(flo, directed = FALSE) nflo nflo[9,] nflo[9,1] nflo[9,4] is.adjacent(nflo, 9, 1) is.adjacent(nflo, 9, 4) network.size(nflo) network.edgecount(nflo) network.density(nflo) has.loops(nflo) is.bipartite(nflo) is.directed(nflo) is.hyper(nflo) is.multiplex(nflo) as.sociomatrix(nflo) all(nflo[,] == as.sociomatrix(nlflo)) all(as.matrix(nflo) == as.sociomatrix(nflo)) as.matrix(nflo,matrix.type = "edgelist") net <- network.initialize(5, loops = TRUE) net[nmat>0] <- 1 all(nmat == net[,]) net[,] <- 0 net[,] <- nmat all(nmat == net[,]) net[,] <- 0 for(i in 1:5) for(j in 1:5) if(nmat[i,j]) net[i,j] <- 1 all(nmat == net[,]) net[,] <- 0 add.edges(net, row(nmat)[nmat>0], col(nmat)[nmat>0]) all(nmat == net[,]) net[,] <- as.numeric(nmat[,]) all(nmat == net[,]) net <- network.initialize(5) add.edge(net, 2, 3) net[,] add.edges(net, c(3, 5), c(4, 4)) net[,] net[,2] <- 1 net[,] delete.vertices(net, 4) net[,] add.vertices(net, 2) net[,] get.edges(net, 1) get.edges(net, 2, neighborhood = "in") get.edges(net, 1, alter = 2) get.edgeIDs(net, 1) get.edgeIDs(net, 2, neighborhood = "in") get.edgeIDs(net, 1, alter = 2) get.neighborhood(net, 1) get.neighborhood(net, 2, type = "in") net[2,3] <- 0 net[2,3] == 0 delete.edges(net, get.edgeIDs(net, 2, neighborhood = "in")) net[,] net <- network.initialize(5) set.network.attribute(net, "boo", 1:10) net %n% "hoo" <- letters[1:7] list.network.attributes(net) get.network.attribute(net, "boo") net %n% "hoo" delete.network.attribute(net, "boo") list.network.attributes(net) set.vertex.attribute(net, "boo", 1:5) net %v% "hoo" <- letters[1:5] list.vertex.attributes(net) get.vertex.attribute(net, "boo") net %v% "hoo" delete.vertex.attribute(net, "boo") list.vertex.attributes(net) net <- network(nmat) set.edge.attribute(net, "boo", sum(nmat):1) set.edge.value(net, "hoo", matrix(1:25, 5, 5)) net %e% "woo" <- matrix(rnorm(25), 5, 5) net[,, names.eval = "zoo"] <- nmat * 6 list.edge.attributes(net) get.edge.attribute(get.edges(net, 1), "boo") get.edge.value(net, "hoo") net %e% "woo" as.sociomatrix(net, "zoo") delete.edge.attribute(net, "boo") list.edge.attributes(net) MtSHloc <- emon$MtStHelens %v% "Location" MtSHimat <- cbind(MtSHloc %in% c("L", "B"), MtSHloc %in% c("NL", "B")) MtSHbyloc <- network(MtSHimat, matrix = "incidence", hyper = TRUE, directed = FALSE, loops = TRUE) MtSHbyloc %v% "vertex.names" <- emon$MtStHelens %v% "vertex.names" MtSHbyloc plot(nflo, displaylabels = TRUE, boxed.labels = FALSE) plot(nflo, displaylabels = TRUE, mode = "circle") plot(emon$MtSi) library("sna") network.layout.degree <- function(d, layout.par){ id <- degree(d, cmode = "indegree") od <- degree(d, cmode = "outdegree") cbind(id, od) } plot(emon$MtStHelens, mode = "degree", displaylabels = TRUE, boxed.labels = FALSE, suppress.axes = FALSE, label.cex = 0.5, xlab = "Indegree", ylab = "Outdegree", label.col = 3) plot(MtSHbyloc, displaylabels = TRUE, label = c(network.vertex.names(MtSHbyloc), "Local", "Non-Local"), boxed.labels = FALSE, label.cex = rep(c(0.5, 1), each = c(27, 2)), label.col = rep(c(3, 4), each = c(27, 2)), vertex.col = rep(c(2, 5), each = c(27, 2))) rnbernexp <- function(n, nv, p = 0.5, onset.hazard = 1, termination.hazard = 1){ nets <- list() for(i in 1:n) nets[[i]] <- .Call("rnbernexp_R", network.initialize(nv, directed = FALSE), p, onset.hazard, termination.hazard, PACKAGE = "networkapi.example") if(i > 1) nets else nets[[1]] }