[R] Error in if (class(networks) == "matrix") from a function

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Wed Sep 21 21:35:39 CEST 2022


Hello,

Check what class(datmat) returns and use ?inherits instead.


class(datmat)
#[1] "matrix" "array"
inherits(datmat, "matrix")
#[1] TRUE


Also, the error the posted code gives is

centrality(datmat,type="flow",center=TRUE)
Error in checkDataTypes(y = NULL, networks = networks, lag = lag) :
   could not find function "checkDataTypes"


but I assume you have function checkDataTypes somewhere in your session, 
the problem should be solved with inherits().

Hope this helps,

Rui Barradas

Às 20:20 de 21/09/2022, Chao Liu escreveu:
> Dear R-Help community,
> 
> This is a crosspost on SO but I've had no luck so far. So I have a function
> which computes a centrality index for the nodes in a network or matrix.
> Here is the function:
> 
> library(igraph) #load package igraph
> centrality <- function (networks, type = c("indegree", "outdegree", "freeman",
>      "betweenness", "flow", "closeness", "eigenvector", "information",
>      "load", "bonpow"), directed = TRUE, lag = 0, rescale = FALSE,
>      center = FALSE, coefname = NULL, ...) {
>      if (is.null(directed) || !is.logical(directed)) {
>          stop("'directed' must be TRUE or FALSE.")
>      }
>      else if (length(directed) != 1) {
>          stop("The 'directed' argument must contain a single logical
> value only.")
>      }
>      else if (directed == FALSE) {
>          gmode <- "graph"
>      }
>      else {
>          gmode <- "digraph"
>      }
>      objects <- checkDataTypes(y = NULL, networks = networks,
>          lag = lag)
>      centlist <- list()
>      for (i in 1:objects$time.steps) {
>          if (type[1] == "indegree") {
>              cent <- degree(objects$networks[[i]], gmode = gmode,
>                  cmode = "indegree", rescale = rescale, ...)
>          }
>          else if (type[1] == "outdegree") {
>              cent <- degree(objects$networks[[i]], gmode = gmode,
>                  cmode = "outdegree", rescale = rescale, ...)
>          }
>          else if (type[1] == "freeman") {
>              cent <- degree(objects$networks[[i]], gmode = gmode,
>                  cmode = "freeman", rescale = rescale, ...)
>          }
>          else if (type[1] == "betweenness") {
>              cent <- betweenness(objects$networks[[i]], gmode = gmode,
>                  rescale = rescale, ...)
>          }
>          else if (type[1] == "flow") {
>              cent <- flowbet(objects$networks[[i]], gmode = gmode,
>                  rescale = rescale, ...)
>          }
>          else if (type[1] == "closeness") {
>              cent <- closeness(objects$networks[[i]], gmode = gmode,
>                  rescale = rescale, ...)
>          }
>          else if (type[1] == "eigenvector") {
>              cent <- evcent(objects$networks[[i]], gmode = gmode,
>                  rescale = rescale, ...)
>          }
>          else if (type[1] == "information") {
>              cent <- infocent(objects$networks[[i]], gmode = gmode,
>                  rescale = rescale, ...)
>          }
>          else if (type[1] == "load") {
>              cent <- loadcent(objects$networks[[i]], gmode = gmode,
>                  rescale = rescale, ...)
>          }
>          else if (type[1] == "bonpow") {
>              cent <- bonpow(objects$networks[[i]], gmode = gmode,
>                  rescale = rescale, tol = 1e-20, ...)
>          }
>          else {
>              stop("'type' argument was not recognized.")
>          }
>          centlist[[i]] <- cent
>      }
>      time <- numeric()
>      y <- numeric()
>      for (i in 1:objects$time.steps) {
>          time <- c(time, rep(i, objects$n[[i]]))
>          if (is.null(centlist[[i]])) {
>              y <- c(y, rep(NA, objects$n[[i]]))
>          }
>          else {
>              if (center == TRUE) {
>                  centlist[[i]] <- centlist[[i]] - mean(centlist[[i]],
>                    na.rm = TRUE)
>              }
>              y <- c(y, centlist[[i]])
>          }
>      }
>      if (is.null(coefname) || !is.character(coefname) || length(coefname) >
>          1) {
>          coeflabel <- ""
>      }
>      else {
>          coeflabel <- paste0(".", coefname)
>      }
>      if (lag == 0) {
>          laglabel <- ""
>      }
>      else {
>          laglabel <- paste0(".lag", paste(lag, collapse = "."))
>      }
>      label <- paste0(type[1], coeflabel, laglabel)
>      dat <- data.frame(y, time = time, node = objects$nodelabels)
>      dat$node <- as.character(dat$node)
>      colnames(dat)[1] <- label
>      attributes(dat)$lag <- lag
>      return(dat)}
> 
> Here is the matrix:
> 
> dat <- read.table(text="A B #this is edgelist
> 1 2
> 1 3
> 1 2
> 2 1
> 2 3
> 3 1
> 3 2
> 3 1", header=TRUE)
> datmat <- as.matrix(get.adjacency(graph.edgelist(as.matrix(dat),
> directed=TRUE))) #this is the matrix
> colnames(datmat) <- c("1", "2", "3") #rename the columns
> 
> When I applied the function to a matrix
> centrality(datmat,type="flow",center=TRUE), it returns the error:
> 
> Error in if (class(networks) == "matrix") { :
>    the condition has length > 1
> 
> What is the cause of this error? How to fix it? Any help will be greatly
> appreciated!
> 
> 
> Best,
> 
> Chao
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list