Skip to content

Instantly share code, notes, and snippets.

View mberneaud's full-sized avatar

Malte Berneaud-Kötz mberneaud

  • @ruhrdot
  • Berlin
View GitHub Profile
@mberneaud
mberneaud / unite_columns.R
Created June 29, 2016 09:41
code to unite columns which add up to be complete columns when weaved together
# combining the two columns containing states names and codes into one
# This replaces each NA in columns one and two with an empty string.
# This is necessary for me to be able to unite them with tidyr's unite() function
cn.inflow[, 1:2][is.na(cn.inflow[, 1:2])] <- ""
cn.inflow <- unite(cn.inflow, state_name2, 1:2, sep = "")
# This replaces each NA in the last two columns into empty strings
cn.inflow[, 14:15][is.na(cn.inflow[, 14:15])] <- ""
cn.inflow <- unite(cn.inflow, ccode2, 14:15, sep = "")
@mberneaud
mberneaud / chop_vectors.R
Created June 20, 2016 08:34
Code used to chop up large vectors into lists of vectors of length n
split(country.pairs$country,
ceiling(seq_along(country.pairs[, "country"])/n))
# Credit goes to some user on Stackoverflow, who's name I can't remember.
@mberneaud
mberneaud / source_https.R
Created May 27, 2016 13:36
function used to source R scripts from GitHub. Practical so source R files from within an AWS instance
# Credit for this function goes to Tony Breyal, who shared it on his blog
# https://tonybreyal.wordpress.com/2011/11/24/source_https-sourcing-an-r-script-from-github/
source_https <- function(url, ...) {
# load package
require(RCurl)
# parse and evaluate each .R script
sapply(c(url, ...), function(u) {
eval(parse(text = getURL(u, followlocation = TRUE, cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl"))), envir = .GlobalEnv)
library(plotly)
x <- list(title = "Vote share in previous election")
y <- list(tickmode = "auto", nticks = 2)
p <- plot_ly(MayorElection2, x = L.VoteShareWinner, y = Reelection,
text = paste("Name winner:", NameCandidate1),
mode = "markers", group = SparkassenMember, colors = "Set1",
opacity = 0.5) %>% layout(xaxis = x, yaxis = y)
p
@mberneaud
mberneaud / regex_snippets.R
Last active May 4, 2016 14:22
Practical regex snipped for cleaning of strings
# selects last character in string
".{1}$"
# selects all characters after the first expression (content of the first capture group),
# which is a comma in this case
", (.*)"
# selects newlines or carriage returns
"[\r\n]"
@mberneaud
mberneaud / removing_breaks.R
Created April 14, 2016 17:13
Deleting newlines and carriage returns
x <- "foo\nbar\rbaz\r\nquux"
gsub("[\r\n]", "", x)
## [1] "foobarbazquux"
#Or
library(stringr)
str_replace_all(x, "[\r\n]" , "")
@mberneaud
mberneaud / clean_chinese_and_punctuation.R
Last active April 14, 2016 17:12
Deleting non UTF-8 Characters and removing punctuation
# removing non-ASCII characters from strings
fin2011[, 1] <- iconv(fin2011[, 1], "utf-8", "ASCII", sub="")
# removing special characters from strings
fin2012[, 2] <- gsub("[[:punct:]]", "", fin2012[, 2])