Skip to content

Instantly share code, notes, and snippets.

View somerealnumbers's full-sized avatar

Andrew Worsley somerealnumbers

View GitHub Profile
@somerealnumbers
somerealnumbers / project_setup.sh
Created January 10, 2018 07:10
Setup R 'work area'
echo "Version: 1.0
RestoreWorkspace: No
SaveWorkspace: No
AlwaysSaveHistory: No
EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 4
Encoding: UTF-8
# required packages
require(dplyr)
require(dygraphs)
require(readxl)
require(stringr)
require(lubridate)
require(xts)
# reading in the data
#################################################
# un-parameterised
require(dplyr)
df = iris[,sapply(X = iris
, function(X) is.numeric(X))]
df.cluster = df %>% kmeans(x = .
# required package
library(VennDiagram)
# plot venn
draw.pairwise.venn(area1 = 6, area2 = 6, cross.area = 3,
category = c('x', 'y'),
fill = c('darkred', 'darkgreen'),
alpha = rep(0.3, 2),
scaled = FALSE)
# creating set x
x = set([1,2,3,4,5,6])
# creating set y
y = set([4,5,6,7,8,9])
# x UNION y
x.union(y)
{1, 2, 3, 4, 5, 6, 7, 8, 9}
sqlSave(channel,
dat,
tablename = NULL,
append = FALSE,
rownames = TRUE,
colnames = FALSE,
verbose = FALSE,
safer = TRUE,
addPK = FALSE,
fast = TRUE,
sqlSave(channel
, dat
, tablename = NULL
, append = FALSE
, rownames = TRUE
, colnames = FALSE
, verbose = FALSE
, safer = TRUE
, addPK = FALSE
, fast = TRUE
# required libraries
library(dplyr)
# loading the iris data set
df.iris = iris
# examining column classes
sapply(X = df.iris, function(X) class(X))
# the 'base' way
guess_weight = function(posts){
# reading in the guesses
guesses = readLines(posts)
# Match the guesses in kilograms and store result in vector
kgmatch = regmatches(guesses,regexpr('[1-9]+\\.?[0-9]* *(k|K) *(g|G)(s|S)?',guesses))
# Changing to numeric and storing in a vector
kgnumerics = as.numeric(regmatches(kgmatch,regexpr('[1-9]+\\.?[0-9]*',kgmatch)))
@somerealnumbers
somerealnumbers / data_frames.R
Created November 14, 2015 00:07
Basic work with an R Data Frame
# creating an R data frame
sim_data = data.frame(X = rnorm(n = 10),
Y = rnorm(n = 10),
Z = rnorm(n = 10))
# select column j
sim_data[,1]
# ...or by name
sim_data['X']