Skip to content

Instantly share code, notes, and snippets.

View tgh0831's full-sized avatar

Thomas Higginbotham tgh0831

  • San Antonio, TX
View GitHub Profile
@tgh0831
tgh0831 / read_opentsdb.r
Created February 22, 2017 21:02 — forked from danslimmon/read_opentsdb.r
Reads data from OpenTSDB into an R data frame.
# Loads data from OpenTSDB.
#
# `server.url`: The URL of the OpenTSDB server (e.g. 'http://tsdb.example.com')
# `metrics`: List of strings specifying the metrics to pull (e.g.
# c("avg:web.cpu.user{host=*}", "sum:web.requests{type=login}")
# )
# `start.dt`: How far to go back in the time series, in any format OpenTSDB
# (e.g. "4h-ago")
# `end.dt`: Where to end the results (defaults to now)
# `datify`: Whether to convert timestamps to POSIXlt objects (defaults to TRUE)
@tgh0831
tgh0831 / deleterecords.sql
Last active November 18, 2016 13:37
T-SQL script to delete a whole bunch of records from a table without growing the log. In this example my table has a field called "recordinsertdt" and I'm keeping the most recent 215 days and deleting the rest. Records are deleted 50000 rows at a time.
doOver:
DELETE TOP (50000)
FROM mytable
WHERE recordinsertdt <=
(
SELECT DATEADD(day, -215, CAST(GETDATE() AS DATE))
);
IF @@rowcount > 0
GOTO doOver;
@tgh0831
tgh0831 / GetWeatherForDate
Created November 8, 2016 20:01
Uses Ram Narasimhan's weatherData package from GitHub (Ram-N/weatherData) to pull several weather observations from a personal weather station on Weather Underground over several days. The version of the package from CRAN currently does not support personal weather stations.
#need to install devtools to download and install package from GitHub
library(devtools)
install_github("weatherData", "Ram-N")
library(weatherData)
data1 <- getWeatherForDate("KTXSHINE2", station_type = "id", start_date="2016-11-01",
end_date = "2016-11-08",
opt_detailed = TRUE,
opt_all_columns = TRUE)
@tgh0831
tgh0831 / GettingStartedWithR.md
Last active November 2, 2016 16:23
Getting started with R
@tgh0831
tgh0831 / foverlaps example.r
Created November 1, 2016 20:58
Fast Overlaps example using the data.table package's foverlaps() function. Uses lubridate to convert text values for date and time to POSIXct
require(data.table)
require(lubridate)
#create the data frames
#x is a data table with a list of events to match against the different dates in x to see if there is overlap
x = data.table(
eventid=c(1,2,3),
start =mdy_hms(c('10/1/2016 04:30:00','10/1/2016 18:02:00','10/2/2016 14:21:00')),
end =mdy_hms(c('10/1/2016 05:43:00','10/2/2016 01:23:00','10/4/2016 08:54:00'))
@tgh0831
tgh0831 / dcast (pivot) Example.r
Created October 27, 2016 20:16
This is an example of pivoting (casting) a data frame.
require(data.table) #data.table is only required to create the sample data frame
require(reshape2) #dcast is a function in the reshape2 package
#generate a data table with some records
df <- data.table(ServerName = c('SERVER1','SERVER2','SERVER3','SERVER4'),RecordDate = seq(as.Date("2011-12-30"), as.Date("2012-01-11"), by="days"),Value = seq(0, 30, by = 2.35))
#Pivot the result, force the value to be the "Value" field
df <- dcast(df, RecordDate ~ ServerName, value.var = 'Value')
#Get rid of NA values in the data frame
@tgh0831
tgh0831 / Padded zero text numbers.sql
Created October 26, 2016 16:39
Short T-SQL example of formatting a number with leading zeroes.
--When you have a number that needs to be formatted with leading zeroes
DECLARE @idnumber INT= 80253;
SELECT @idnumber AS idnumberint,
RIGHT('000000'+CAST(@idnumber AS VARCHAR), 6) AS idnumbervarchar;
@tgh0831
tgh0831 / dixondates.R
Created October 26, 2016 14:38 — forked from noamross/dixondates.R
Using times and dates in R
#'% Using Dates and Times in R
#'% Bonnie Dixon
#'% 14-02-10 15:09:57
#'
#' *Today at the [Davis R Users'
#' Group](http://www.noamross.net/davis-r-users-group.html), [Bonnie
#' Dixon](http://ffhi.ucdavis.edu/people/directory/bmdixon) gave a tutorial on the
#' various ways to handle dates and times in R. Bonnie provided this great script
#' which walks through essential classes, functions, and packages. Here it is piped through
#' `knitr::spin`. The original R script can be found as a gist
@tgh0831
tgh0831 / Simple RODBC Example.r
Last active September 2, 2020 19:58
This is a simple example using the RODBC package to return a query from a Microsoft SQL server to a data frame.
##### This is a simple RODBC example
##### The ODBCDriverName will be the driver name in ODBC Administrator
require(RODBC)
#open the ODBC connection
ch <- odbcConnect("ODBCDriverName")
##### Alternative ODBC connection for Microsoft SQL Server
ch <- odbcDriverConnect(
@tgh0831
tgh0831 / install ROracle on Windows.md
Created October 24, 2016 21:06 — forked from jgilfillan/install ROracle on Windows.md
Instructions on how to install ROracle for R on Windows.