Skip to content

Instantly share code, notes, and snippets.

View romainfrancois's full-sized avatar
🎉
tada⬢science ⬡⬡ ex(Posit/RStudio, ThinkR, Mango Solutions)

Romain François romainfrancois

🎉
tada⬢science ⬡⬡ ex(Posit/RStudio, ThinkR, Mango Solutions)
View GitHub Profile
@romainfrancois
romainfrancois / distinct2.r
Last active December 19, 2018 11:43
distinct with incomparable
library(dplyr, warn.conflicts = FALSE)
distinct2 <- function(df, predicate_incomparable, ...) {
predicate_incomparable <- enquo(predicate_incomparable)
# rows for which the predicate os true for at least one variable
incomparables <- filter_all(df, any_vars(!!predicate_incomparable))
# rows for which the predicate is false for all variables
comparables <- filter_all(df, all_vars(!(!!predicate_incomparable)))
library(tidyverse)
library(rap)
library(glue)
#> 
#> Attaching package: 'glue'
#> The following object is masked from 'package:dplyr':
#> 
#>     collapse
library(zap)
trace(wap, exit = quote({
print(f)
fm <- formals(f)
print(fm)
sepal_length <<- fm[["Sepal.Length"]]
print(rlang::node_car(sepal_length))
# devtools::install_github("romainfrancois/zap")
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(gapminder)
library(tidyverse)
library(zap)
library(broom)
# gapminder data for Asia only
gap <- gapminder %>%
filter(continent == "Asia") %>%
mutate(yr1952 = year - 1952) %>%
mutate(country = fct_reorder2(country, .x = year, .y = lifeExp))
library(dplyr, warn.conflicts = FALSE)
library(purrr)
df <- tibble(
x1 = sample(c(-1, 0, 1), size = 1000, replace = TRUE),
x2 = sample(c(-1, 0, 1), size = 1000, replace = TRUE),
x3 = sample(c(-1, 0, 1), size = 1000, replace = TRUE),
x4 = sample(c(-1, 0, 1), size = 1000, replace = TRUE)
)
library(rvest)
#> Loading required package: xml2
library(glue)

synonyms <- function(word) {
  glue("https://www.thesaurus.com/browse/{word}") %>% 
    read_html() %>% 
    html_nodes(".synonyms-container span a[data-linkid]") %>% 
 html_text()
library(dplyr, warn.conflicts = FALSE)
iris %>%
mutate_at(vars(starts_with("S")), ~if(is.factor(.)) as.character(.) else .)
# https://git.io/fxnlF
library(dplyr, warn.conflicts = FALSE)
iris %>%
mutate_at(vars(starts_with("S")), ~if(is.factor(.)) as.character(.) else .) %>%
as_tibble()
# https://git.io/fxnl5
``` r
library(rlang)
grab <- function(x, i, env = baseenv(), inline = FALSE){
ex <- if (inline) {
expr((!!`[`)( !!x, !!i) )
} else {
expr((!!x)[!!i])
}
cat("-------")