R/mobnation.R

#' National movements network
#'
#' Function to analyze the dataset generated by the impcheckdata function and
#' produces a national mobility network with respect to a given country.
#' This network contains movements inside a given country.
#'
#' Cette fonction analyse le jeu de données généré par la fonction impcheckdata
#' et produit un réseau de mobilité nationale par rapport à un pays donné.
#' Ce réseau contient les mouvements ayant lieu à l'intérieur du pays de référence.
#'
#' @import igraph
#' @importFrom graphics plot
#' @export mobnation
#' @param x the data.frame containing the rawdata and generated by the impcheckdata function. \cr
#' Required columns: "ORIGINE_NOM", "ORIGINE_PAYS", "ORIGINE_LONGITUDE_X", "ORIGINE_LATITUDE_Y",
#' "DESTINATION_NOM", "DESTINATION_PAYS", "DESTINATION_LONGITUDE_X", "DESTINATION_LATITUDE_Y" and "EFFFECTIF"
#'
#' @param mon_pays the reference country between quotation marks, with the same writting than in x.
#' @keywords national movements network
#'
#' @examples
#' mobnation(rawdata, "TUNISIE")
#'
mobnation <- function(x, mon_pays){
  Origines <- x[,c("ORIGINE_NOM", "ORIGINE_PAYS", "ORIGINE_LONGITUDE_X", "ORIGINE_LATITUDE_Y")]
  Destinations <- x[,c("DESTINATION_NOM", "DESTINATION_PAYS", "DESTINATION_LONGITUDE_X", "DESTINATION_LATITUDE_Y")]
  names(Origines) <- names(Destinations) <- c("NOM","PAYS","LONGITUDE_X", "LATITUDE_Y")
  Origines$NOM <- as.character(Origines$NOM)
  Origines$PAYS <- as.character(Origines$PAYS)
  Destinations$NOM <- as.character(Destinations$NOM)
  Destinations$PAYS <- as.character(Destinations$PAYS)
  Localites <- unique(rbind(Origines, Destinations))
  Nom_Localites <- unique(Localites$NOM)
  x_tmp <- x[x$DESTINATION_PAYS == mon_pays & x$ORIGINE_PAYS == mon_pays,]
  Reseau_nation <- graph_from_data_frame(x_tmp[,c("ORIGINE_NOM","DESTINATION_NOM")],
                                         vertices=Nom_Localites, directed=TRUE)
  E(Reseau_nation)$weight <- x_tmp[,c("EFFFECTIF")]
  Reseau_nation
}
Pachka/AQCR_SNA_Package documentation built on May 11, 2019, 7:26 p.m.