This file contains 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
#' Set Working Directory | |
#' | |
#' \code{setwd(dir)} is used to set the working directory to \code{dir}. | |
#' | |
#' This version of \code{setwd} is different from the one in \code{base} in that it allows for paths containing Unix-like | |
#' environment variables. For example, if HOME is set (globally) to your home directory, then \code{base::setwd($HOME)} | |
#' will not work. However, the version below will work. | |
#' | |
#' @param dir A character string: tilde expansion will be done. |
This file contains 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(clusterProfiler) | |
library(org.Hs.eg.bd) | |
library(KEGGREST) | |
library(dplyr) | |
library(magrittr) | |
# List of genes of interest | |
x <- c("GPX3", "GLRX", "LBP", "CRYAB", "DEFB1", "HCLS1", "SOD2", "HSPA2", | |
"ORM1", "IGFBP1", "PTHLH", "GPC3", "IGFBP3","TOB1", "MITF", "NDRG1", | |
"NR1H4", "FGFR3", "PVR", "IL6", "PTPRM", "ERBB2", "NID2", "LAMB1", |
This file contains 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(tidyverse) | |
# 1. Theoretical answer | |
# It's the probability that both teams score no goal, | |
# plus the probability they both score one goal, | |
# plus ... plus the probability they both score five goals | |
# | |
# If we assume the scores are binomial random variables | |
# and that the scores of each team are independent, | |
# this is simply a sum of products of binomial probabilities. |
This file contains 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 is a short script that shows how to compute the KL divergence using the kernel density estimator | |
# The key point is to use linear interpolation to evaluate the density at the same points | |
# 1. Generate a sample of standard normal variates | |
x <- rnorm(100) | |
# 2. Compute kernel density estimator | |
dens_obs <- density(x) | |
# 3. Use linear interpolation to evaluate over a grid |
This file contains 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(tidyverse) | |
# Create dataset with fantasy scores | |
data_ottawa <- tibble::tribble( | |
~harris, ~Sinopoli, ~Spencer, ~Ellingson, | |
19.8, 13.4, 20.2, 18.4, | |
3.4, 8, 4.1, 21.7, | |
25.7, 31.8, 5.9, 9.9, | |
-0.3, 7.7, 6.6, 5.6, | |
18.5, 34.1, 7.8, 7.7, |
This file contains 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(tidyverse) | |
library(lubridate) | |
library(weathercan) | |
# Lethbridge and Saskatoon | |
data <- weather_dl(station_ids = c(2265, 50091), | |
start = "2012-01-01", end = "2018-08-15") | |
# Plot full data | |
data %>% |
This file contains 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
# Loop through list of departments | |
list_depts <- c("Adult Medicine", | |
"Heart Health", | |
"Maternal Services") | |
for (dept in list_depts) { | |
# Add underscores | |
dept_f <- stringr::str_replace_all(dept, " ", "_") | |
# Change the name of output | |
output <- "report_template.Rmd" %>% |
This file contains 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(rvest) | |
library(tidyverse) | |
library(lubridate) | |
url <- "https://www.hockey-reference.com/teams/TBL/2019_games.html" | |
webpage <- read_html(url) | |
results <- webpage %>% | |
html_table(fill = TRUE) %>% |
This file contains 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(tidyverse) | |
# What I want | |
target <- mtcars %>% | |
filter(cyl %in% c(4, 8), | |
gear %in% c(3, 4)) | |
# Solution | |
dynamic_filter <- function(df, variables, conditions){ | |
filter_conditions <- purrr::map2(variables, conditions, |
This file contains 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
set.seed(12345) | |
mu <- 0.5 | |
sigma <- 1 | |
n <- 25 | |
B <- 1000 | |
results <- replicate(B, { | |
data <- rnorm(n, mu, sigma) | |
OlderNewer