Skip to content

Commit

Permalink
addstates
Browse files Browse the repository at this point in the history
  • Loading branch information
James Curley committed May 13, 2016
1 parent dacb3bf commit e2ac847
Show file tree
Hide file tree
Showing 16 changed files with 253 additions and 4 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ Imports:
igraph
License: GPL-3
LazyData: TRUE
RoxygenNote: 5.0.0
RoxygenNote: 5.0.1
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Generated by roxygen2: do not edit by hand

export(addstates)
export(delete_isolates)
export(get_statespace)
export(lastints)
export(lastnet)
export(rowmatch)
export(stateclass)
export(stateid)
18 changes: 18 additions & 0 deletions R/addstates.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#' Add State ids and classes
#'
#' @param df a dataframe with two variables, winners and losers
#' @return the df with two extra rows containing state id and class
#' @examples
#' df<-data.frame(winner=c(1,2,3,1,2,3,2,1,2,3,3,1,2,3,4,3,1,3,2,1,1,1,1,2,2),
#' loser=c(2,4,4,4,3,2,1,3,3,4,4,2,3,2,3,4,2,4,3,3,3,2,2,4,3)
#' )
#' addstates(df)
#' @export

addstates <- function(df){
v<-lastnet(df)
v1<-lapply(v, get_statespace)
df$id<-unlist(lapply(v1, stateid))
df$class<-unlist(lapply(v1, stateclass))
return(df)
}
13 changes: 13 additions & 0 deletions R/delete_isolates.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#' Delete isolated nodes from network
#'
#' @param q an igraph object
#' @return an igraph object containing only connected nodes
#' @examples
#' g=igraph::graph.edgelist(cbind(a=c(1,2,3,4), b=c(3,1,1,4)),directed=TRUE)
#' delete_isolates(g)
#' @export

delete_isolates<-function(q){
igraph::delete.vertices(q,which(igraph::degree(q)==0))
}

15 changes: 12 additions & 3 deletions R/get_statespace.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@


get_statespace <- function(g){

g<-delete_isolates(g)

dd<-cbind(
do.call('rbind',list(igraph::triad.census(g))),
maxdom = max(table(igraph::head_of(g,es=1:length(igraph::E(g))))),
Expand All @@ -18,9 +21,15 @@ get_statespace <- function(g){
degreediff = max(igraph::degree(g,mode="out")-igraph::degree(g,mode="in"))
)

colnames(dd)[1:16]<-c("X003","X012","X102","X021D","X021U","X021C","X111D","X111U",
"X030T","X030C","X201","X120D","X120U","X120C","X210","X300")

dd=as.data.frame(dd)

dd=dd[c(1,2,4:6,9:10,17:21)]
return(dd)

colnames(dd)<-c("X003","X012","X021D","X021U","X021C",
"X030T","X030C","maxdom", "noedges",
"nonodes","distance","degreediff"
)

return(dd)
}
23 changes: 23 additions & 0 deletions R/rowmatch.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#' Match rows of dataframes
#'
#' @param v1 a dataframe or matrix
#' @param v2 a row of a dataframe or matrix or a vector of length equal to ncol(v1)
#' @param nomatch how to represent non matching indices of rows
#' @return a vector with a 1 in the index of matched rows
#' @examples
#' v1 <- data.frame(A=c(1,2,3),B=c(2,3,3),C=c(3,3,2),D=c(1,1,1))
#' v2 <- data.frame(A=3,B=3,C=2,D=1)
#' rowmatch(v1,v2,nomatch=NA)
#' @export


rowmatch<-function (v1, v2, nomatch = NA)
{
if (class(v2) == "matrix")
v2 <- as.data.frame(v2)
if (is.null(dim(v1)))
v1 <- as.data.frame(matrix(v1, nrow = 1))
cv1 <- do.call("paste", c(v1[, , drop = FALSE], sep = "\r"))
cv2 <- do.call("paste", c(v2[, , drop = FALSE], sep = "\r"))
match(cv1, cv2, nomatch = nomatch)
}
22 changes: 22 additions & 0 deletions R/stateclass.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#' Find State Class
#'
#' @param x a vector of network characteristics generated by get_statespace function
#' @return the state class corresponding to the network characteristics
#' @examples
#' g=igraph::graph.edgelist(cbind(a=c(1,2,3,3), b=c(4,1,1,2)),directed=TRUE)
#' gs=get_statespace(g)
#' stateclass(gs)
#' @export

stateclass <- function(x){

data("states", envir = environment())
xx <- get("states", envir = environment())

data("stateclasses", envir = environment())
xxx <- get("stateclasses", envir = environment())

q<-rowmatch(xx[1:12], x, nomatch = NA)
q1<-xxx[which(q==1)]
return(q1)
}
6 changes: 6 additions & 0 deletions R/stateclasses.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#' State classes of 41 network states
#'
#' Vector defining the classes of 41 network states
#'
#' @format A vector of length 41:
"stateclasses"
19 changes: 19 additions & 0 deletions R/stateid.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#' Find State ID
#'
#' @param x a vector of network characteristics generated by get_statespace function
#' @return the state id corresponding to the network characteristics
#' @examples
#' g=igraph::graph.edgelist(cbind(a=c(1,2,3,3), b=c(4,1,1,2)),directed=TRUE)
#' gs=get_statespace(g)
#' stateid(gs)
#' @export

stateid <- function(x){

data("states", envir = environment())
xx <- get("states", envir = environment())

q<-rowmatch(xx[1:12], x, nomatch = NA)
q1<-which(q==1)
return(q1)
}
Binary file added data/stateclasses.rda
Binary file not shown.
24 changes: 24 additions & 0 deletions man/addstates.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions man/delete_isolates.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions man/rowmatch.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions man/stateclass.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions man/stateclasses.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions man/stateid.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e2ac847

Please sign in to comment.