Skip to content

Instantly share code, notes, and snippets.

View csrvermaak's full-sized avatar

Rademeyer Vermaak csrvermaak

  • STANLIB
  • Cape Town
View GitHub Profile
@csrvermaak
csrvermaak / dplyr_filter.R
Created October 10, 2017 08:44
All Filter Tricks Possible
https://sebastiansauer.github.io/dplyr_filter/
@csrvermaak
csrvermaak / GitHelperFunctions
Last active September 7, 2016 10:02
Git Delete Tools
To remove a local branch from your machine:
```
git branch -d {the_local_branch} (use -D instead to force deletion without checking merged status)
```
To remove a remote branch from the server:
```
git push origin --delete {the_remote_branch}
Using, e.g., summarise_each, we can plug in ANY function and apply it to all groups!!
E.g., getting a boxplot table, by grouping according to a factor, and calculating the moments:
Data <- tbl_df ( Factors (str) | FactorValues (dbl) )
BoxMoments <- Data %>%
group_by(Factors) %>%
summarise_each( funs( Min = min, Max = max, Mean = mean, Median = median, N = n(),
LowHinge = boxplot.stats(.)[[1]][2],
UpHinge = boxplot.stats(.)[[1]][4]
@csrvermaak
csrvermaak / dplyr_functions_programmatic_use.R
Created January 29, 2016 12:07 — forked from steadyfish/dplyr_functions_programmatic_use.R
using dplyr functions programmatically
# using dplyr finctions in non-interactive mode
# examples
library(plyr)
library(dplyr)
d1 = data_frame(x = seq(1,20),y = rep(1:10,2),z = rep(1:5,4))
head(d1)
#### single table verbs ####
@csrvermaak
csrvermaak / Transpose.R
Created November 26, 2015 09:07
Transpose a dataframe and keep first column as column names
tmydf = setNames(data.frame(t(mydf[,-1])), mydf[,1])
@csrvermaak
csrvermaak / ProgressBar.R
Last active April 12, 2017 11:05
R Windows ProgressBar
# ======= MAP pb ============
# Setup
N <- 5
pb <- vector(mode = "list",N); for (iP in 1:N) {pb[[iP]] <- progress_estimated(nrow(VCV))}
# In MAPPED function
add argument:
, .pb=NULL
@csrvermaak
csrvermaak / StandardEvaluation.R
Last active January 5, 2017 09:49
Standard Evaluation Example
# =============== Filter ===================
# You need to use interp (from the lazyeval package) to substitute which_colum by the symbol v1 (and not the string "v1"), as explained in this vignette.
filter_criteria <- interp(~ which_column == 1, which_column = as.name("v1"))
df %>% filter_(filter_criteria)
# OR
df %>% filter_(paste0("is.na(`","ABL SJ Equity","`)"))
# =============== Mutate ===================
mutate_call = lazyeval::interp(~ a + b, a = as.name(col1), b = as.name(col2))
@csrvermaak
csrvermaak / PaletteDemo
Created November 11, 2015 12:55
Colour Palettes in R
library(RColorBrewer)
# Function for plotting colors side-by-side
pal <- function(col, border = "light gray", ...){
n <- length(col)
plot(0, 0, type="n", xlim = c(0, 1), ylim = c(0, 1),
axes = FALSE, xlab = "", ylab = "", ...)
rect(0:(n-1)/n, 0, 1:n/n, 1, col = col, border = border)
}