Skip to content

Instantly share code, notes, and snippets.

View privefl's full-sized avatar

Florian Privé privefl

View GitHub Profile
@privefl
privefl / multMatVec.cpp
Created December 30, 2016 17:01
Matrix-vector multiplication in Rcpp and RcppArmadillo
// [[Rcpp::depends(RcppArmadillo, bigmemory, BH)]]
#include <RcppArmadillo.h>
#include <bigmemory/MatrixAccessor.hpp>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector prod1(XPtr<BigMatrix> bMPtr, const NumericVector& x) {
MatrixAccessor<char> macc(*bMPtr);
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
bool checkDuplicate(const IntegerMatrix& pop, int ind) {
if (ind == 1) {
return(true);
} else {
int i, j, k = ind - 1;
int n = pop.nrow();
N <- 23e4
n <- 2e4
prb <- ifelse(sample(0:1, size = N, replace = TRUE) == 1, 1.0, 0.05)
smpl <- sampling::UPrandomsystematic(n / sum(prb) * prb)
table(prb[as.logical(smpl)])
table(prb[sample.int(N, size = n, replace = FALSE, prob = prb)])
@privefl
privefl / Unicode To Hexa HTML
Created December 2, 2017 09:27
Convert unicode in HTML to hexa.
unicode2hex <- function(html_file) {
new_text <- gsub(pattern = "&lt;U\\+([0-9]{4})&gt;", replacement = "&#x\\1",
x = readLines(html_file, encoding = "UTF-8"))
writeLines(new_text, con = html_file, useBytes = TRUE)
}
#### IN ORDER ####
# In first session, run:
write(1, "test.txt")
obj.lock <- flock::lock("test.txt")
# In second session, run this (this will wait the unlock in the first session)
obj.lock <- flock::lock("test.txt")
write(2, "test.txt", append = TRUE)
flock::unlock(obj.lock)
library(dplyr)
# dplyr programming
my_summarise <- function(df, group) {
group <- enquo(group)
df %>%
group_by(!!group) %>%
summarise_all(mean)
}
library(dplyr)
# dplyr programming
my_summarise <- function(df, group) {
group <- enquo(group)
df %>%
group_by(!!group) %>%
summarise_all(mean)
}
library(dplyr)
# dplyr programming
my_summarise <- function(df, group) {
group <- enquo(group)
df %>%
group_by(!!group) %>%
summarise_all(mean)
}
library(dplyr)
# dplyr programming
my_summarise <- function(df, group) {
group <- enquo(group)
df %>%
group_by(!!group) %>%
summarise_all(mean)
}
MY_THEME <- function(p, coeff = 1) {
p + theme_bw() +
theme(plot.title = element_text(size = rel(2.0 * coeff), hjust = 0.5),
plot.subtitle = element_text(size = rel(1.5 * coeff), hjust = 0.5),
legend.title = element_text(size = rel(1.8 * coeff)),
legend.text = element_text(size = rel(1.3 * coeff)),
axis.title = element_text(size = rel(1.5 * coeff)),
axis.text = element_text(size = rel(1.2 * coeff)),
legend.key.height = unit(1.3 * coeff, "line"),
legend.key.width = unit(1.3 * coeff, "line"))