====== Zoo ====== ===== Hypernets ===== > library(jsonlite) > wdir <- "C:/Users/vlado/DL/data/hyper/All/AllSet_all_raw_data/zoo" > setwd(wdir) > Z <- read.table("zoo.data",head=FALSE,sep=",") > head(Z) V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17 V18 1 aardvark 1 0 0 1 0 0 1 1 1 1 0 0 4 0 0 1 1 2 antelope 1 0 0 1 0 0 0 1 1 1 0 0 4 1 0 1 1 3 bass 0 0 1 0 0 1 1 1 1 0 0 1 0 1 0 0 4 4 bear 1 0 0 1 0 0 1 1 1 1 0 0 4 0 0 1 1 5 boar 1 0 0 1 0 0 1 1 1 1 0 0 4 1 0 1 1 6 buffalo 1 0 0 1 0 0 0 1 1 1 0 0 4 1 0 1 1 > lab <- c( "animal name", "hair", "feathers", "eggs", "milk", "airborne", + "aquatic", "predator", "toothed", "backbone", "breathes", "venomous", + "fins", "legs", "tail", "domestic", "catsize", "type") > I <- c(2:13,15:17); J <- 1:15 > K <- as.logical(unlist(Z[1,I])) > J[K] [1] 1 4 7 8 9 10 15 > Vid <- lab[I] > Z$V1[27] <- "Frog" # 2 "frog"s, 26 & 27 > E = vector(mode="list",m) > for(i in 1:m) E[[i]] <- J[as.logical(as.vector(unlist(Z[i,I])))] > H <- data.frame(ID=Z$V1,legs=Z$V14,type=Z$V18,E=rep(NA,m)) > H$E <- E > n <- length(Vid); V <- data.frame(ID=Vid) > Tid <- c("mammal", "bird", "reptile", "fish", "amphibian", "insect", "other") > data <- list(tID=Tid) > info <- list(network="Zoo", + title="Zoo database", + by="Richard Forsyth", + href="https://archive.ics.uci.edu/dataset/111/zoo", + note='there are 2 instances of "frog" and one of "girl"!', + creator="V. Batagelj", + date=date(), + nNodes=n, + nLinks=m, + simple=FALSE ) > HN <- list(format="hypernets",info=info,nodes=V,links=H,data=data) > str(HN) List of 5 $ format: chr "hypernets" $ info :List of 10 ..$ network: chr "Zoo" ..$ title : chr "Zoo database" ..$ by : chr "Richard Forsyth" ..$ href : chr "https://archive.ics.uci.edu/dataset/111/zoo" ..$ note : chr "there are 2 instances of \"frog\" and one of \"girl\"!" ..$ creator: chr "V. Batagelj" ..$ date : chr "Thu Oct 19 02:08:46 2023" ..$ nNodes : int 15 ..$ nLinks : int 101 ..$ simple : logi FALSE $ nodes :'data.frame': 15 obs. of 1 variable: ..$ ID: chr [1:15] "hair" "feathers" "eggs" "milk" ... $ links :'data.frame': 101 obs. of 4 variables: ..$ ID : chr [1:101] "aardvark" "antelope" "bass" "bear" ... ..$ legs: int [1:101] 4 4 0 4 4 4 4 0 0 4 ... ..$ type: int [1:101] 1 1 4 1 1 1 1 4 4 1 ... ..$ E :List of 101 .. ..$ : int [1:7] 1 4 7 8 9 10 15 .. ..$ : int [1:7] 1 4 8 9 10 13 15 .. ..$ : int [1:7] 3 6 7 8 9 12 13 .. .. [list output truncated] $ data :List of 1 ..$ tID: chr [1:7] "mammal" "bird" "reptile" "fish" ... > write(toJSON(HN),"Zoo.json") ===== SDAJSON ===== > library(jsonlite) > wdir <- "C:/Users/vlado/DL/data/hyper/All/AllSet_all_raw_data/zoo" > setwd(wdir) > > Z <- fromJSON("Zoo.json") > Zhead <- list(nUnits=101, nVars=3, + vars=list(V1=list(ID="legs",type="integer"), + V2=list(ID="type",type="nominal",cats=Z$data$tID), + V3=list(ID="attrs",type="set",cats=Z$nodes$ID), + V4=list(ID="battrs",type="members",cats=Z$nodes$ID))) > Zinfo <- list(dataset=Z$info$network, + title=Z$info$title, + by=Z$info$by, + href=Z$info$href, + note=Z$info$note, + creator="V. Batagelj", + date=date() + ) > DF <- Z$links; rownames(DF) <- DF$ID; ZDF <- DF[,2:4] > colnames(ZDF)[3] <- "attrs" > n <- nrow(ZDF); za <- ZDF$attrs; zb <- vector("list",n) > for(i in 1:n) {v <- rep(0,15); v[za[[i]]] <- 1; zb[[i]] <- v} > ZDF$battrs <- zb > ZOO <- list(format="SDAJSON",info=Zinfo,head=Zhead,SDF=ZDF) > write(toJSON(ZOO),"ZooSDA.json") > # str(ZOO)