This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
matrix_to_df_firstline_header <- function(mat){ | |
requireNamespace("purrr") | |
mat %>% | |
## cut columns into lists | |
apply(2, function(s) list(s)) %>% | |
flatten %>% | |
map(flatten_chr) %>% | |
## set names to the first element of the list | |
{set_names(x = map(., ~ .x[-1]), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## this function will print "TRUE" if the categories defined by the vector are | |
## continuous, e.g. c("1500_20000","20000_NA","0_1500" ) ` | |
is_continuous_categories <- function(cat_vector){ | |
cat_range <- cat_vector %>% | |
str_split("_") %>% | |
transpose %>% | |
map(unlist) %>% | |
{c(invoke(setdiff, .), invoke(setdiff, rev(.)))} | |
identical(cat_range, c("0", "NA")) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(dplyr) | |
library(tidyr) | |
words_some_missing <- data_frame(one_word = c(NA, "M", NA), | |
second_word = c("F", NA, NA)) | |
words_some_missing %>% | |
unite(sex, one_word, second_word) | |
## that sticks the NAs together, which I think is something you dont want? In |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(map) | |
library(rucrdtw) | |
map(list(first = 33, second = 19, third = 7), ~ ucred_mv(synthetic_control[-.x,], synthetic_control[.x,], byrow= TRUE)) %>% map_dbl("distance") %>% tibble::enframe(.) | |
# consider also adding this to that shiny app |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
microbenchmark::microbenchmark({lm(ys~xs, data = testdf)$residuals}, | |
{fastLm(ys~xs, data = testdf)$residuals}, | |
{homemade_residuals(indeps, deps)}, times = 500) | |
identical(homemade_residuals(indeps, deps), homemade_residuals2(indeps, deps)) | |
# all.equal() | |
testdf <- data_frame(xs = runif(200, 0, 15), | |
ys = xs * 5 + 15 + rnorm(200, sd = 3)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(dplyr) | |
library(igraph) | |
library(ggplot2) | |
library(ggraph) | |
ed <- frame_data( | |
~from, ~to, | |
"a", "b", | |
"a", "c", | |
"c", "e" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(dplyr) | |
library(ggplot2) | |
data_frame(x = seq(2001, 2016, by = 2), | |
y = exp(rnorm(length(x), mean = 0.3, sd = 0.03)*(x - 2000))) %>% | |
ggplot(aes(x = x, y = y)) + geom_point() + geom_line() + | |
labs(x = "Year", | |
y = "Cumulative number of papers", | |
title = "Number of papers with a 'Number of papers' figure") + | |
theme_bw() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(dplyr) | |
library(tidyr) | |
library(stringr) | |
too_wide <- frame_data( | |
~common_name, ~plant_genera, | |
"Abagrotis apposita", "Amelanchier Arbutus Cenanothus", | |
"Abagrotis brunipennis", "Prunus Vaccinium" | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
BEGIN { | |
fam = 0 | |
parasite_genus = 0 | |
host_genus = 0 | |
parasite_species = 0 | |
print "Host_fam\tHost_genus\tHost_sp\tParasite_genus\tParasite_sp\tk_i\tLocation\tParasite_fam" | |
} | |
/^[A-Z]/ {fam = $1} | |
/^\t[A-Z]/ { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rmax <- function(rm, baserate){ | |
force(rm) | |
force(baserate) | |
function(x) { | |
rm * x / ( | |
(rm / baserate) + x | |
) | |
} | |
} |