Skip to content

Instantly share code, notes, and snippets.

Avatar
👷‍♂️
back at it

Clayton Yochum ClaytonJY

👷‍♂️
back at it
View GitHub Profile
@ClaytonJY
ClaytonJY / origin.md
Last active Nov 12, 2019
'origin' must be supplied
View origin.md
# built-in function for "today"
Sys.Date()
#> [1] "2019-11-11"

# Dates are actually just integers underneath
# we can coerce to integer to see that version of it
today_days <- as.integer(Sys.Date())
today_days
#> [1] 18211
@ClaytonJY
ClaytonJY / group-and-summarize.md
Last active Oct 31, 2019
Adding impact factor
View group-and-summarize.md
library(tidyverse)

# fake some data
input_tbl <- lst(
  MarketDay = as.Date(c("2016-01-01", "2016-01-02", "2016-01-03")),
  HourEnding = 1:3,
  FuelType = c("Coal", "Gas", "Hydro"),
  Month = 1,  # because only Jan dates
  Region = "North"
@ClaytonJY
ClaytonJY / impute_and_sum_demo.R
Last active Mar 10, 2019
Add column in base R, imputing with zeros
View impute_and_sum_demo.R
df <- data.frame(
id = 1:5,
label = LETTERS[1:5], # A, B, C, D, E
gen_1 = c(1.0, 1.1, NA, 1.3, NA),
gen_2 = c(2.0, NA, 2.2, 2.3, NA),
gen_3 = c(3.1, NA, 3.2, NA, NA)
)
# take a look
df
@ClaytonJY
ClaytonJY / quo-missing-null.R
Created Nov 9, 2018
default args for bare col names: missing or NULL?
View quo-missing-null.R
library(ggplot2)
library(rlang)
f <- function(df, x, y, facet) {
x <- enquo(x)
y <- enquo(y)
facet = enquo(facet)
g <- ggplot(df, aes(x = !!x, y = !!y)) +
@ClaytonJY
ClaytonJY / where-is-imap_at.R
Last active Jul 3, 2018
Replacing different values with missing in a subset of columns
View where-is-imap_at.R
library(tidyverse)
mt_tbl <- as_tibble(mtcars)
# types need to match those in mtcars (all doubles)
# careful; `na_if` doesn't work with multiple values
missing_codes <- list(
cyl = 6.0,
disp = 160.0,
carb = 1.0
@ClaytonJY
ClaytonJY / tidy-parallel-timing.R
Last active Mar 24, 2018
Different ways to parallelize a grouped dplyr operation
View tidy-parallel-timing.R
library(dplyr)
library(purrr)
# helper to make examples
new_tbl <- function(n_groups, row_range = 5:10) {
1:n_groups %>%
map_df(~tibble(
group_id = .x,
value = rnorm(sample.int(row_range, 1L))
@ClaytonJY
ClaytonJY / interval-checking.R
Created Feb 5, 2018
Checking interval membership in a tidy-ish way
View interval-checking.R
library(dplyr)
library(purrr)
#### setup ####
tbl <- tibble(
id = 1:25
)
@ClaytonJY
ClaytonJY / weird-diag-example.md
Last active Jan 3, 2018
R's diag function does weird things
View weird-diag-example.md
diag(1.1)          
#>      [,1]
#> [1,]    1
diag(0.9)          
#> <0 x 0 matrix>
diag(0.9, nrow = 1)
#>      [,1]
#> [1,]  0.9
diag(-1)           
@ClaytonJY
ClaytonJY / group-level-vfolding.R
Last active Oct 5, 2017
"grouped" vfolding in rsample
View group-level-vfolding.R
library(dplyr)
library(purrr)
library(rsample)
# suppose we want to keep cylinder-groups together
# we'll vfold those instead of the whole thing
initial_fold <- mtcars %>%
distinct(cyl) %>%
vfold_cv(v = 3)
View tryCatch-example.R
# test function
f <- function(x) {
if (missing(x)) {
stop("error, Will Robinson")
} else {
x
}
}
f(1) # 1
You can’t perform that action at this time.