Skip to content

Instantly share code, notes, and snippets.

View DavisVaughan's full-sized avatar

Davis Vaughan DavisVaughan

View GitHub Profile
@DavisVaughan
DavisVaughan / all-except.R
Last active February 24, 2021 13:48
Subset for all except certain words / patterns using stringr?
library(stringr)
# I had something like this
bobs <- c("bob.txt", "bob.scn", "bob")
# I want to remove just the strings with "scn"
# Using: https://stackoverflow.com/questions/5556213/regex-to-match-everything-except-the-given-words-which-may-include-hyphens-dash
str_subset(bobs, "^(?:(?!scn).)*$")
#> [1] "bob.txt" "bob"
@DavisVaughan
DavisVaughan / odbc-access-testing.r
Last active July 7, 2017 14:21
odbc-access-testing
library(DBI)
library(dbplyr)
suppressPackageStartupMessages(library(dplyr))
#> Warning: package 'dplyr' was built under R version 3.4.1
options(tibble.width = Inf)
cn <- dbConnect(odbc::odbc(), dsn = "access-odbc")
#dbListTables(cn)
library(DBI)
library(dbplyr)
library(dplyr, warn.conflicts = FALSE)
# This is an Access connection
cn <- dbConnect(odbc::odbc(), dsn = "dbplyr-testing")
# Connect to the test table
test_tbl <- tbl(cn, "test-1")
db_write_table.ACCESS <- function(con, table, types, values,
temporary = FALSE, ...) {
db_create_table(con, table, types, temporary = temporary)
# Convert factors to strings
is_factor <- vapply(values, is.factor, logical(1))
values[is_factor] <- lapply(values[is_factor], as.character)
# Encode special characters in strings
library(rlang)
library(tibble, warn.conflicts = FALSE)
# A formula for the cars dataset
f <- as.formula("speed ~ dist")
f
#> speed ~ dist
#> <environment: 0x7f8399800d80>
# X
# Just an example "vector of means"
vec_of_means <- c(6.4, 2, 3.3, 4.6, 5, 8)
n <- length(vec_of_means)
# Loops!
for(i in 1:(n-1)) {
print(paste0("Hello i: ", vec_of_means[i], " ==="))
for(j in (i+1):n) {
# devtools::install_github("business-science/tibbletime")
library(tibbletime)
#>
#> Attaching package: 'tibbletime'
#> The following object is masked from 'package:stats':
#>
#> filter
library(tidyverse)
#> Loading tidyverse: ggplot2
#> Loading tidyverse: tibble
// [[Rcpp::depends(xtensor)]]
#include <numeric>
#include "xtensor/xmath.hpp"
#include "xtensor-r/rarray.hpp"
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::plugins(cpp14)]]
suppressPackageStartupMessages(library(tibbletime))
suppressPackageStartupMessages(library(tidyverse))
data(FB)
# Create the column names
col_names <- map_chr(2:10, ~paste0("adjusted_", .x))
# Creating the rolling functions and assign them names
rollers <- map(2:10, ~rollify(mean, window = .x)) %>%
suppressPackageStartupMessages(library(tibbletime))
suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(purrr))
# Create a tibble of event dates, ~100k
# 2013-01-01 to the end of the year by 5 minutes
event_dates <- create_series(~2013, 5~M, force_class = "POSIXct")
event_dates
#> # A time tibble: 105,120 x 1