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
# www.analytikdat.cz | |
# www.analytikdat.cz/index.php/blog/entry/r-and-postgresql-using-rpostgresql-and-sqldf | |
# Load required libraries | |
library("RPostgreSQL") | |
library("sqldf") | |
# Establish connection | |
drv <- dbDriver("PostgreSQL") | |
# Simple version (localhost as default) |
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
#' Create SQL statement from rpart rules | |
#' | |
#' Rpart rules are changed to sql CASE statement. | |
#' | |
#' @param df data frame used for rpart model | |
#' @param model rpart model | |
#' @export | |
#' @examples | |
#' parse_tree(df=kyphosis,model=rpart(data=kyphosis,formula=Kyphosis~.)) | |
#' parse_tree(df=mtcars,model=rpart(data=mtcars,formula=am~.)) |
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(shiny) | |
german_data <- read.table(file="http://archive.ics.uci.edu/ml/machine-learning-databases/statlog/german/german.data", | |
sep=" ", header=FALSE, stringsAsFactors=TRUE) | |
names(german_data) <- c('ca_status','duration','credit_history','purpose','credit_amount','savings', | |
'present_employment_since','installment_rate_income','status_sex','other_debtors', | |
'present_residence_since','property','age','other_installment','housing','existing_credits', | |
'job','liable_maintenance_people','telephone','foreign_worker','gb') | |
german_data$gb <- factor(german_data$gb, levels=c(2,1), labels=c("bad","good")) | |
german_data <- german_data[,c("duration","age","credit_amount","gb")] |
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
create or replace function gtd(sample text) | |
returns varchar as $$ | |
declare outsql varchar; | |
begin | |
select tbl_definition into outsql from ( | |
with | |
source as (select $1::varchar instr) |
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
#initialize | |
library(ggplot2) | |
TestData <- data.frame( a = rep(LETTERS[1:4],10), | |
b = rep(c('A','B'),20), | |
c = rep(LETTERS[1:5],each=8), | |
d = rep(c('A','B'),2,each=10), | |
m1 = rnorm(40), | |
m2 = rnorm(40), | |
m3 = rnorm(40), |
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
standardize_name <- function(string) { | |
capped <- grep("^[^A-Z]*$", string, perl = TRUE) | |
substr(string[capped], 1, 1) <- toupper(substr(string[capped], 1, 1)) | |
string <- gsub('_',' ',string, fixed=TRUE) | |
return(string) | |
} | |
GenerateCube <- function(TableName,TableSchema,PrimaryKey,TimeTableName,TimeTableSchema,Destination) { |
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
GenerateCubeSQLite <- function(DataFrame,PrimaryKey,CubeDestination,DataSourceDestination,NumericAggregators=NA,DateAggregators=NA,MaxTime='2015-01-01',MinTime='2001-01-01') { | |
# Function to clean names | |
standardize_name <- function(string) { | |
capped <- grep("^[^A-Z]*$", string, perl = TRUE) | |
substr(string[capped], 1, 1) <- toupper(substr(string[capped], 1, 1)) | |
string <- gsub('_',' ',string, fixed=TRUE) | |
return(string) | |
} | |