Skip to content

Instantly share code, notes, and snippets.

@tomasgreif
tomasgreif / RPostgreSQL_sqldf
Last active April 6, 2017 18:42
RPostgreSQL and sqldf basic usage
# 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)
@tomasgreif
tomasgreif / gist:6038822
Created July 19, 2013 12:36
Parse rpart model.
#' 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~.))
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")]
@tomasgreif
tomasgreif / gtd
Created October 9, 2013 17:17
Guess table definition. The function will create sql create table statement from sample data.
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)
@tomasgreif
tomasgreif / global.R
Created November 6, 2013 10:30
Illustration
#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),
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) {
@tomasgreif
tomasgreif / GenerateCubeSQLite
Last active December 30, 2015 06:19
Create OLAP cube for Saiku from any data frame
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)
}