This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(sqldf) | |
# there is a default dataset called iris that is always loaded into the R scope | |
# let's say I want to break it into two randomly using sqldf | |
# assign id's to the rows | |
my.iris <- iris | |
my.iris$id <- 1:nrow(my.iris) | |
ran.split <- data.frame(rs = sample(1:max(my.iris$id), floor(max(my.iris$id) / 2), replace=F)) | |
sqldf("select * from `my.iris` as x inner join `ran.split` as y on x.id = y.rs") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
twoModeProject <- function(w, m=1, ...) { | |
if(!require(parallel)) { | |
stop("Requires the parallel package.") | |
} | |
if(!require(data.table)) { | |
stop("Requires the data.table package.") | |
} | |
if(!("data.table" %in% class(w))) { | |
w <- data.table(w) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' ## Storing R Objects in a SQLite Database | |
#' Two packages we are using. The first is the ```RSQLite``` which will be used to create and manage an in-memory SQLite database. The second is ```igraph``` which I will use to create and visualize a random network. Some of the work I do is on network simulation. I often don't know the metrics I need from a simulated network when it's created, so I want to be able to store the networks that are created so that I can go back later and analyze them. | |
library(RSQLite) | |
library(igraph) | |
#' Create a database in memory. | |
con <- dbConnect(SQLite(), ":memory:") | |
#' The table has two columns, an *id* column and a column called *graph* which is a **blob** type. This type just stores binary data. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(data.table) | |
library(igraph) | |
x <- fread("all_networks_140923.csv") | |
igraph.options(vertex.label = NA, edge.arrow.size = 0, vertex.size = 5) | |
g <- graph.data.frame(x[,list(source, target, weight, type, survey)]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(XML) | |
library(data.table) | |
library(ggplot2) | |
library(ggmap) | |
args <- commandArgs(trailingOnly = T) | |
cat(args) | |
x <- readLines('http://realtime.lextran.com/InfoPoint/map/GetVehicleXml.ashx?RouteId=3') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### Functions for connecting and running uclassify text | |
#' Create an output dataset | |
#' | |
#' @param text_vec The vector of text that will be used | |
#' @return A data.table | |
uclassify_output_dt <- function(text_vec) { | |
N <- length(text_vec) | |
data.table(txt = text_vec, text_coverage = numeric(N)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(dplyr) | |
library(igraph) | |
clean_graph <- function(teamID, el) { | |
e <- el %>% filter(id == teamID) %>% | |
select(source, target) | |
g <- graph_from_data_frame(e) %>% | |
as.undirected(mode = 'collapse', edge.attr.comb = 'max') %>% | |
simplify | |
return(g) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
reduce.tdm <- function(m, rs = 1, cs = 100) { | |
repeat { | |
i <- sum(dim(m)) | |
cat("Min row sum\t", min(rowSums(m)),"\tMin col sum\t",min(colSums(m)), "\n") | |
m <- m[rowSums(m) >= rs, colSums(m) >= cs] | |
if(sum(dim(m)) == i) break | |
} | |
return(m) | |
} | |
m <- reduce.tdm(m) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Animate optim ordinary least squares | |
rm(list=ls()) | |
gc() | |
library(ggplot2) | |
library(tidyverse) | |
library(foreign) | |
library(gganimate) | |
library(transformr) | |
library(data.table) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Animate fitting ordinary least squares | |
# Author: Jesse Michael Fagan, PhD | |
# Date: 2020-02-10T19:29 | |
## Packages ######################################## | |
rm(list=ls()) | |
gc() | |
library(magick) | |
library(gganimate) | |
library(transformr) |