Skip to content

Instantly share code, notes, and snippets.

@kevincdurand1
kevincdurand1 / install-proto.sh
Created May 24, 2016 19:00 — forked from samklr/install-proto.sh
Install Protobuf debian ...
#! /bin/bash
wget https://github.com/google/protobuf/releases/download/v2.6.1/protobuf-2.6.1.tar.gz
tar xzf protobuf-2.6.1.tar.gz
cd protobuf-2.6.1
sudo apt-get update
sudo apt-get install build-essential
sudo ./configure
sudo make
sudo make check
sudo make install
@kevincdurand1
kevincdurand1 / Confusion Matrix.R
Last active April 8, 2017 01:28
Confusion Matrix
install.packages("caret")
require(caret)
model.predictions = predict(model, testset)
confusionMatrix(model.predictions, testset$outcome)
@kevincdurand1
kevincdurand1 / Creating WEb API via Flask with JSON.py
Last active April 8, 2017 01:33
Creating WEb API via Flask with JSON
"""
JSON EXAMPLE
{
"url": "http://www.example.com/api/posts/12345",
"title": "Writing RESTful APIs in Python",
"author": "http://www.example.com/api/users/2",
"body": "... text of the article here ...",
"comments": "http://www.example.com/api/posts/12345/comments"
}
@kevincdurand1
kevincdurand1 / Percent of Missing Values of dataset.R
Last active April 8, 2017 01:32
Percent of Missing Values of dataset
sapply(database, function(df) {
sum(is.na(df)==TRUE)/ length(df);
})
@kevincdurand1
kevincdurand1 / Writing text and output from analyses to a file.R
Last active April 8, 2017 01:32
Writing text and output from analyses to a file
# Start writing to an output file
sink('analysis-output.txt')
set.seed(12345)
x <-rnorm(10,10,1)
y <-rnorm(10,11,1)
# Do some stuff here
cat(sprintf("x has %d elements:\n", length(x)))
print(x)
cat("y =", y, "\n")
@kevincdurand1
kevincdurand1 / Saving in R data format.R
Last active April 8, 2017 01:32
Saving in R data format
# Save in a text format that can be easily loaded in R
dump("data", "data.Rdmpd")
# Can save multiple objects:
dump(c("data", "data1"), "data.Rdmpd")
# To load the data again:
source("data.Rdmpd")
# When loaded, the original data names will automatically be used.
# Save a single object in binary RDS format
@kevincdurand1
kevincdurand1 / Writing data to a file.R
Last active April 8, 2017 01:32
Writing data to a file
# A sample data frame
data <- read.table(header=TRUE, text='
subject sex size
1 M 7
2 F NA
3 F 9
4 M 11
')
@kevincdurand1
kevincdurand1 / Running a script.R
Last active April 8, 2017 01:31
Running an external script from R
#Problem
#You want to run R code from a text file.
# First, go to the proper directory
setwd('/home/username/desktop/rcode')
source('analyze.r')
@kevincdurand1
kevincdurand1 / Creating a formula from a string.R
Last active April 8, 2017 01:30
Creating a formula from a string
# This returns a string:
"y ~ x1 + x2"
#> [1] "y ~ x1 + x2"
# This returns a formula:
as.formula("y ~ x1 + x2")
#> y ~ x1 + x2
#> <environment: 0x3361710>
# These are the variable names:
@kevincdurand1
kevincdurand1 / Creating strings from variables.R
Last active April 8, 2017 01:30
Creating strings from variables
a <- "apple"
b <- "banana"
# Put a and b together, with a space in between:
paste(a, b)
#> [1] "apple banana"
# With no space, use sep="", or use paste0():
paste(a, b, sep="")
#> [1] "applebanana"