====== MATLAB data in R ====== March 13, 2018 ===== Reading MATLAB sparse matrix ===== FB100 networks: ZIP dowloaded from https://archive.org/details/oxford-2005-facebook-matrix > install.packages("R.matlab") > library(R.matlab) > help(readMat) > A <- readMat("Amherst41.mat") > str(A) List of 2 $ A :Formal class 'dgCMatrix' [package "Matrix"] with 6 slots .. ..@ i : int [1:181908] 253 357 384 759 778 841 1120 1184 1307 1310 ... .. ..@ p : int [1:2236] 0 25 104 297 396 610 631 644 749 783 ... .. ..@ Dim : int [1:2] 2235 2235 .. ..@ Dimnames:List of 2 .. .. ..$ : NULL .. .. ..$ : NULL .. ..@ x : num [1:181908] 1 1 1 1 1 1 1 1 1 1 ... .. ..@ factors : list() $ local.info: num [1:2235, 1:7] 1 1 1 1 1 1 1 1 1 1 ... - attr(*, "header")=List of 3 ..$ description: chr "MATLAB 5.0 MAT-file, Platform: MACI64, Created on: Fri Feb 11 20:09:04 2011" ..$ version : chr "5" ..$ endian : chr "little" > head(A$local.info) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [1,] 1 2 0 0 359 2008 50112 [2,] 1 1 106 103 340 2007 11279 [3,] 1 1 114 0 0 2007 16202 [4,] 1 1 99 0 0 2006 9076 [5,] 1 2 111 109 347 2007 17773 [6,] 1 1 0 0 0 2009 3576 ===== Converting to Pajek ===== ==== Toy example ==== > M0 <- matrix(0, nrow = 4, ncol = 5) > M0[1,1] <- 5 > M0[3,1] <- 7 > M0[4,2] <- 4 > M0[2,3] <- 1 > M0[3,5] <- 1 > M0[4,5] <- 1 > M0 [,1] [,2] [,3] [,4] [,5] [1,] 5 0 0 0 0 [2,] 0 0 1 0 0 [3,] 7 0 0 0 1 [4,] 0 4 0 0 1 > library(Matrix) > M <- as(M0, "sparseMatrix") > M 4 x 5 sparse Matrix of class "dgCMatrix" [1,] 5 . . . . [2,] . . 1 . . [3,] 7 . . . 1 [4,] . 4 . . 1 > M@x [1] 5 7 4 1 1 1 > M@i [1] 0 2 3 1 2 3 > M@p [1] 0 2 3 4 4 6 > M@Dim [1] 4 5 > diff(M@p) [1] 2 1 1 0 2 > T <- as(M,'dgTMatrix') > str(T) Formal class 'dgTMatrix' [package "Matrix"] with 6 slots ..@ i : int [1:6] 0 2 3 1 2 3 ..@ j : int [1:6] 0 0 1 2 4 4 ..@ Dim : int [1:2] 4 5 ..@ Dimnames:List of 2 .. ..$ : NULL .. ..$ : NULL ..@ x : num [1:6] 5 7 4 1 1 1 ..@ factors : list() > cat("*arcs\n "); cat(paste(T@i+1,T@j+1,T@x,"\n")) *arcs 1 1 5 3 1 7 4 2 4 2 3 1 3 5 1 4 5 1 ==== Conversion from "dgCMatrix" to Pajek ==== mat2Pajek <- function(A,file,twomode=FALSE){ library(Matrix) tit <- attr(A,"header")$description; net <- file(file,"w") cat("MATLAB to Pajek\n",tit,"\nTransformed: ",date(),"\n",sep="") cat("% MATLAB to Pajek\n% ",tit,"\n% Transformed: ",date(),"\n",sep="",file=net) M <- A$A; T <- as(M,'dgTMatrix'); D <- A$local.info nr <- T@Dim[1]; nc <- T@Dim[2] cat("*network fromMATLAB\n",file=net) if(twomode){ cat("*vertices",nr+nc,nr,"\n",file=net) if(!is.null(T@Dimnames[[1]])){N <- T@Dimnames[[1]] for(i in seq_along(N)) cat(i,' "',N[i],'"\n',sep="",file=net)} if(!is.null(T@Dimnames[[2]])){N <- T@Dimnames[[2]] for(i in seq_along(N)) cat(i+nr,' "',N[i],'"\n',sep="",file=net)} } else { cat("*vertices",nr,"\n",file=net) if(!is.null(T@Dimnames[[1]])){N <- T@Dimnames[[1]] for(i in seq_along(N)) cat(i,' "',N[i],'"\n',sep="",file=net)} } cat("*arcs\n ",file=net) if(twomode) cat(paste(T@i+1,nr+T@j+1,T@x,"\n"),file=net) else cat(paste(T@i+1,T@j+1,T@x,"\n"),file=net) if(!is.null(D)){ nr <- nrow(D); nc <- ncol(D) for(j in 1:nc){ cat("\n*vector V",j,"\n*vertices ",nr,"\n ",sep="",file=net) cat(paste(D[,j],"\n"),file=net) } } close(net) } === Converting the toy example === > M0 <- matrix(0, nrow = 4, ncol = 5) > M0[1,1] <- 5 > M0[3,1] <- 7 > M0[4,2] <- 4 > M0[2,3] <- 1 > M0[3,5] <- 1 > M0[4,5] <- 1 > library(Matrix) > rownames(M0) <- c("a","b","c","d") > colnames(M0) <- c("A","B","C","D","E") > M <- as(M0, "sparseMatrix") > M 4 x 5 sparse Matrix of class "dgCMatrix" A B C D E a 5 . . . . b . . 1 . . c 7 . . . 1 d . 4 . . 1 > B <- list(A=M,local.info=NULL) > attr(B,"header") <- list(description="Hand made example") > str(B) List of 2 $ A :Formal class 'dgCMatrix' [package "Matrix"] with 6 slots .. ..@ i : int [1:6] 0 2 3 1 2 3 .. ..@ p : int [1:6] 0 2 3 4 4 6 .. ..@ Dim : int [1:2] 4 5 .. ..@ Dimnames:List of 2 .. .. ..$ : chr [1:4] "a" "b" "c" "d" .. .. ..$ : chr [1:5] "A" "B" "C" "D" ... .. ..@ x : num [1:6] 5 7 4 1 1 1 .. ..@ factors : list() $ local.info: NULL - attr(*, "header")=List of 1 ..$ description: chr "Hand made example" > mat2Pajek(B,file="B.paj",twomode=TRUE) MATLAB to Pajek Hand made example Transformed: Wed Mar 14 00:03:49 2018 % MATLAB to Pajek % Hand made example % Transformed: Wed Mar 14 00:03:49 2018 *network fromMATLAB *vertices 9 4 1 "a" 2 "b" 3 "c" 4 "d" 5 "A" 6 "B" 7 "C" 8 "D" 9 "E" *arcs 1 5 5 3 5 7 4 6 4 2 7 1 3 9 1 4 9 1 === Converting a FB100 network === > mat2Pajek(A,file="A.paj") MATLAB to Pajek MATLAB 5.0 MAT-file, Platform: MACI64, Created on: Fri Feb 11 20:09:04 2011 Transformed: Wed Mar 14 00:04:07 2018 % MATLAB to Pajek % MATLAB 5.0 MAT-file, Platform: MACI64, Created on: Fri Feb 11 20:09:04 2011 % Transformed: Wed Mar 14 00:04:07 2018 *network fromMATLAB *vertices 2235 *arcs 254 1 1 358 1 1 385 1 1 ... 1963 2235 1 1999 2235 1 2041 2235 1 *vector V1 *vertices 2235 1 1 ... 2005 2004 2009 *vector V7 *vertices 2235 50112 11279 ... 18233 9492 60686