Skip to content

Instantly share code, notes, and snippets.

View grayskripko's full-sized avatar
🏠
Working from home

Gray grayskripko

🏠
Working from home
  • Buenos Aires, Argentina
View GitHub Profile
@grayskripko
grayskripko / dbplyr-slice-head.R
Created April 15, 2024 18:58
dbplyr slice_head
dbtbl |> filter(row_number() < .., .by=..)
@grayskripko
grayskripko / parametrized-func-calls.R
Last active June 28, 2024 20:15
Parametrized function calls
expand_grid(
pick_n = 1:3) |>
rowwise() |>
mutate(mtr = unlist(list(measure(
sam_vos, n_weeks=4, fn = \(x) sort(x)[pick_n])))) |>
# OR unnamed
mutate(mtr = unlist(list(apply_to_wur(lst(across(ev()))))) |>
ungroup() |>
pprint()
# more: https://dplyr.tidyverse.org/articles/rowwise.html#repeated-function-calls
@grayskripko
grayskripko / insight-finder.R
Last active September 28, 2023 17:27
Power BI key influencers finder
# you can use it in Power BI to build a table of insights that will help you not to miss important influencers
suppressWarnings(suppressPackageStartupMessages({
library(tidyverse)
library(forcats)
library(ggplot2)
library(Hmisc)}))
custom_lump <- function(fct, min_size=nrow(tbl) / n_bins) {
kept_levels <- fct %>%
table() %>%
@grayskripko
grayskripko / mutate_pmap_all
Created February 6, 2023 21:27
mutate_pmap_all
tbl %>% mutate(res=pmap(., ~with(list(...), {any_col is available here!
@grayskripko
grayskripko / tidy_hint.R
Last active November 14, 2022 22:18
tidyverse hints
?dplyr::context
# gives us
cur_column() - current column name!
cur_<press tab>
n()
disp <- 10
mtcars %>% mutate(disp = .data$disp * .env$disp)
mtcars %>% mutate(disp = disp * disp)
?.data
@grayskripko
grayskripko / grouper_func_to_list.R
Created May 28, 2022 11:52
quos and nse, hard case
group_in_func <- function(x, ...) {
x %>%
group_by(!!!(...)) %>%
summarise(sum(mpg))
}
list(quos(any_name=cyl == 6)) %>%
map(
group_in_func,
x=head(select(as_tibble(mtcars), 1:2), 5) %>% print())
@grayskripko
grayskripko / pivot_longer_wider.R
Last active October 20, 2022 21:00
R pivot longer-wider example
pivot_longer(
cols=-c(date, match_id),
names_to=c("side", ".value"),
names_sep='(?<=^[ha])_')
pivot_wider(
id_cols=c(date, h_team, a_team),
names_from=side,
names_glue = "{side}_{.value}",
values_from=-c(date, h_team, a_team, team, side))
@grayskripko
grayskripko / least_corr_interact.R
Last active February 17, 2022 11:21
least correlated interactions
# let's say we have a set of features and want to create a bunch of their interations
# Which should we use? I tried to answer this question thinking that
# correlation is a good measure of novelty and usefullness of the information
explore$interactions <- function(df) {
assert(ncol(df) == 2)
df %>%
# { print(explore$cor_tibble(.)); . } %>%
mutate(sum=.[[1]] + .[[2]]) %>%
mutate(dif=.[[1]] - .[[2]]) %>%
@grayskripko
grayskripko / tidy_rowwise.R
Created February 15, 2022 10:54
tidyverse rowwise cases
tbl %>%
rowwise() %>%
group_map(~{...})
tbl %>%
mutate(i=1:n()) %>%
nest(data=-i) %>%
pluck('data') %>%
map(~{...})
@grayskripko
grayskripko / pandas_pipe.txt
Created February 15, 2022 10:51
Pandas .pipe() .apply() .transform() .map()
def inve(x):
print(type(x), x)
s = pd.Series([1,2,3])
s.pipe(inve) # series
s.apply(inve) # int 1
s.transform(inve) # int 1
s.map(inve) # int 1