Reading constraints in R

read_Pajek_clu <- function(f,skip=1){
# reads a partition from Pajek's clu file; skip *vertices and comments lines
  read.table(f,skip=skip,colClasses=c("integer"),header=FALSE)$V1
}

read_Pajek_net <- function(f,skip=0){
# reads a network from Pajek's net file; skip comments lines
   L <- readLines(f)
   st <- grep("\\*",L)
   S <- unlist(strsplit(L[st[1]]," "))
   n <- as.integer(S[2]); n1 <- st[1]+1; n2 <- st[2]-1
   m1 <- st[2]+1; m2 <- length(L); m <- m2-m1+1
   Names <- unlist(strsplit(L[n1:n2],'"'))[3*(1:n)-1]
   R <- vector(mode="list",length=n)
   S <- unlist(strsplit(L[m1:m2],'[[:space:]]+'))
   b <- as.integer(S[4*(1:m)-2]); e <- as.integer(S[4*(1:m)-1])
   for(k in 1:m){u <- b[k]; v <- e[k]; R[[u]] <- c(R[[u]],v)}
   names(R) <- Names
   return(R)
}

Pajek file should be produced using Pajek export in *arcs or *edges format. Each line contains initial space + 3 numbers.

source("C:\\Users\\batagelj\\Documents\\papers\\2018\\CRoNoS\\shape\\pajek.R")

netRel <- "C:/Users/batagelj/Documents/papers/2018/CRoNoS/shape/europe/Europe.net"
R <- read_Pajek_net(netRel,3)

> netRel <- "C:/Users/batagelj/Documents/papers/2018/CRoNoS/shape/europe/EuropeA3.net"
> R <- read_Pajek_net(netRel,3)
> listRel(R)
SJM 1 : 31 53 48 
ALB 2 : 15 26 29 56 20 
AND 3 : 12 38 
AUT 4 : 8 13 17 20 23 36 37 40 
BEL 5 : 12 13 25 30 41 
BIH 6 : 7 29 35 
HRV 7 : 17 29 35 37 20 
CZE 8 : 13 32 36 
DNK 9 : 13 39 46 
EST 10 : 22 53 11 39 
FIN 11 : 31 39 53 
FRA 12 : 13 20 25 28 38 40 16 21 41 
DEU 13 : 25 30 32 40 39 
GIB 14 : 38 
GRC 15 : 26 45 51 20 54 
GGY 16 : 41 21 
HUN 17 : 35 36 37 50 52 
IRL 18 : 41 48 19 
IMN 19 : 41 
ITA 20 : 34 37 40 55 27 38 29 
JEY 21 : 41 
LVA 22 : 24 44 53 39 
LIE 23 : 40 
LTU 24 : 32 44 53 39 
LUX 25 : 
MKD 26 : 35 45 56 
MLT 27 : 
MCO 28 : 
MNE 29 : 35 56 
NLD 30 : 
NOR 31 : 39 53 48 46 
POL 32 : 36 44 52 53 39 
PRT 33 : 38 
SMR 34 : 
SRB 35 : 45 50 56 
SVK 36 : 52 
SVN 37 : 
ESP 38 : 
SWE 39 : 53 
CHE 40 : 
GBR 41 : 48 46 
ARM 42 : 43 47 51 
AZE 43 : 47 51 53 
BLR 44 : 52 53 
BGR 45 : 50 51 
FRO 46 : 48 
GEO 47 : 51 53 
ISL 48 : 
MDA 49 : 50 52 
ROU 50 : 52 
TUR 51 : 54 
UKR 52 : 53 
RUS 53 : 
CYP 54 : 
VAT 55 : 
XKX 56 : 
> Ro <- R
> Ri <- vector("list",length(R))
> names(Ri)<-names(R)
> for(i in 1:length(R)) for(j in R[[i]]) Ri[[j]] <- union(Ri[[j]],i)
> sRi <- Ri; sRo <- Ro


notes/da/eurdc.txt · Last modified: 2018/04/11 12:19 by vlado
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki