The initial lapply call on adjList in adj2mat creates separate columns for each element in the matrix, which crashes the function. We could just wrap the inner adjList reference in an as.data.frame or, even better, just use the matrix directly after detecting the typeof.