Skip to content

Instantly share code, notes, and snippets.

View avallecam's full-sized avatar
🎯
Focusing

Andree Valle Campos avallecam

🎯
Focusing
View GitHub Profile
@avallecam
avallecam / delays_cfr.R
Last active April 16, 2024 17:56
reporting delays bias CFR
``` r
# packages ----------------------------------------------------------------
library(tidyverse)
library(incidence2)
# input -------------------------------------------------------------------
onset <- tibble(
date = seq(ymd(20200101),ymd(20200104),1),
@avallecam
avallecam / python.Rmd
Last active February 9, 2024 20:10
quick start to use python within R
If I need to install python packages
```{python}
# install packages
pip install pandas
pip install matplotlib
```
I can install python (!!) and the packages in R using `{reticulate}`
@avallecam
avallecam / sampling-distribution-rowwise.R
Last active November 13, 2023 18:32
calculate the sampling distribution as demo for central limit theorem
``` r
# from py to R ------------------------------------------------------------
# to convert the a python notebook to Rmd file
# with https://pkgs.rstudio.com/rmarkdown/reference/convert_ipynb.html
# for one sample ----------------------------------------------------------
set.seed(123)
@avallecam
avallecam / rowwise-examples.R
Last active October 4, 2023 19:33
collect some examples of rowwise relevant (?) data
``` r
# from dplyr --------------------------------------------------------------
# https://dplyr.tidyverse.org/articles/rowwise.html#creating
library(tidyverse)
df <- tibble(
x = 1:2,
y = 3:4,
@avallecam
avallecam / complete.r
Created July 28, 2023 17:24 — forked from deanmarchiori/complete.r
Turning implicit missing values into explicit missing values.
# tidyr::complete() & tidyr::full_seq() -----------------------------------
# Turning implicit missing values into explicit missing values.
# Bonus: Filling in gaps in a date range
library(tidyr)
library(tibble)
library(dplyr)
# Making up some observations from two weather stations.
@avallecam
avallecam / confidence_intervals.r
Created July 3, 2023 09:38
get confidence intervals from numerator and denominator
``` r
library(tidyverse)
#> Warning: package 'tidyverse' was built under R version 4.2.3
#> Warning: package 'ggplot2' was built under R version 4.2.3
#> Warning: package 'tibble' was built under R version 4.2.3
#> Warning: package 'forcats' was built under R version 4.2.3
# library(datapasta)
# tribble_paste()
@avallecam
avallecam / missing_with_factors.R
Created January 24, 2023 13:52
handle missing observations for factor variables
if (!require("pacman")) install.packages("pacman")
#> Loading required package: pacman
pacman::p_load(tidyverse)
# con missing como NA -----------------------------------------------------
f1 <- tibble(variable = c("a", "a", NA, NA, "a", "b",
NA, "c", "a", "c", "b"))
f1
#> # A tibble: 11 × 1
@avallecam
avallecam / how_to_across_dplyr.R
Last active April 15, 2023 19:19
Use across() and c_across() {dplyr} functions!
# https://dplyr.tidyverse.org/articles/rowwise.html
``` r
library(tidyverse)
# how to use across() -------------------------------------------------------
# _ with summary functions ------------------------------------------------
# from cheetsheet
# summarise(mtcars, across(everything(), mean))
@avallecam
avallecam / combo-case_when-stringer.R
Last active March 9, 2022 11:11
cleaning solutions: combine case_when and stringer functions in different messy data scenarios
``` r
if(!require("tidyverse")) install.packages("tidyverse")
if(!require("snakecase")) install.packages("snakecase")
if(!require("janitor")) install.packages("janitor")
library(tidyverse)
# read raw data -----------------------------------------------------------
# scenario:
@avallecam
avallecam / write_multiple_purrr.R
Last active February 18, 2023 17:49
write (or export to different files) multiple data frames inside a tibble using purrr
# https://stackoverflow.com/a/70956247/6702544
``` r
# use functions from the
# tidyr, stringr and purrr packages
library(tidyverse)
# create fake dataset
expand_grid(cat = c("a","b"),
lev = "c",
num = 1:2) %>%