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
| twosum <- function(nums, target) { | |
| cur_i <- 1 | |
| nxt_i <- cur_i + 1 | |
| while(cur_i != length(nums)) { | |
| thesum <- nums[cur_i] + nums[cur_i + 1] | |
| if (thesum == target) { | |
| return(c(cur_i, nxt_i)) | |
| } | |
| if (nxt_i == length(nums)) { | |
| cur_i <- cur_i + 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
| library(tidyverse) | |
| set.seed(123) | |
| df <- crossing( | |
| subject = 1:10, | |
| prime = c("yes", "no") | |
| ) %>% | |
| mutate( | |
| intercept = 500, |
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
| y | x1 | x2 | g | |
|---|---|---|---|---|
| -1.84 | a1 | b2 | s1 | |
| -0.66 | a1 | b2 | s1 | |
| -0.83 | a1 | b2 | s1 | |
| 0.87 | a2 | b2 | s1 | |
| 0.12 | a1 | b2 | s1 | |
| -0.67 | a1 | b2 | s1 | |
| 0.4 | a2 | b2 | s1 | |
| -1.12 | a1 | b2 | s1 | |
| -0.05 | a1 | b2 | s1 |
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
| # Without dplyr loaded ---- | |
| isNamespaceLoaded("dplyr") | |
| #> [1] FALSE | |
| ## FUNCTION: Both work for getting `dplyr::mutate` | |
| get("mutate", envir = asNamespace("dplyr")) | |
| #> function (.data, ...) | |
| #> { | |
| #> UseMethod("mutate") | |
| #> } |
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
| # https://fosstodon.org/@josi/113391735804937128 | |
| cumsum_cut_rlang <- function(x, cuts) { | |
| x[cuts-1] <- 0 | |
| res <- lapply(split(x, cumsum(x == 0)), \(.x) { | |
| cumsum(.x) | |
| }) |> | |
| unlist() |> | |
| unname() | |
| n <- length(res) | |
| to_fill <- numeric(n) |
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(arrow) | |
| CYC_2022 <- read_feather("https://raw.githubusercontent.com/yjunechoe/Semantic-Persistence/refs/heads/master/CYC_2022.arrow") | |
| # Diagnosing warnings of type: "Model failed to converge with max|grad| ..." | |
| library(lme4) | |
| #> Loading required package: Matrix | |
| fm <- Accuracy ~ Condition * SemanticFit * Transitivity + | |
| (1 | Item) + | |
| (1 + Condition | Subject) |
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
| # Top-level | |
| thefuck <- function(e = rlang::last_error()) { | |
| stopifnot(rlang::is_call_simple(e$call)) | |
| if (grep("^unused argument", e$message)) { | |
| fix_unused_arg(e) | |
| } | |
| # ... more cases | |
| } | |
| # Internal methods |
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(rrapply) | |
| expr <- y ~ x + (1 || z) | |
| lobstr::ast(!!expr) | |
| #> █─`~` | |
| #> ├─y | |
| #> └─█─`+` | |
| #> ├─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
| # The reprex | |
| library(ggplot2) | |
| data <- tibble::tribble( | |
| ~x, ~group, ~y, ~measure, ~base, | |
| "FY18", "aaa", 0.5603448, 260, 464, | |
| "FY15", "aaa", 0.5081269, 1313, 2584, | |
| "FY19", "aaa", 0.5799373, 185, 319, | |
| "FY16", "aaa", 0.5225225, 580, 1110, | |
| "FY13", "aaa", 0.4779116, 595, 1245, | |
| "FY17", "aaa", 0.5502471, 334, 607, |
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
| lobstr::ast(T ? F : 1) | |
| `%?%` <- function(e1, e2) { | |
| e2_expr <- substitute(e2) | |
| if (e1) { | |
| return(eval.parent(e2_expr[[2]])) | |
| } else { | |
| return(eval.parent(e2_expr[[3]])) | |
| } | |
| } |
NewerOlder