Skip to content

Instantly share code, notes, and snippets.

View vikjam's full-sized avatar
💭
👨🏾‍💻

vikjam

💭
👨🏾‍💻
View GitHub Profile
@vikjam
vikjam / prop_table.r
Last active February 25, 2022 18:34
Proportions table in dplyr
#'@title Pipe-friendly (simplified) prop.table
#'@description \code{prop_table} Computes proportions similiar to prop.table using \code{dplyr} functions.
#'@param dat The data used for the computation.
#'@param vars The columns to create aggregate counts of.
#'@param na.rm Whether to remove missing values or not. Default is \code{FALSE}.
#'@return A \code{data.frame} with the counts and proportions by \code{vars}.
#'
#'@author vikjam
#'
#'@examples
@vikjam
vikjam / csv_to_dict.py
Created July 21, 2021 19:00
CSV to dict via pandas
import pandas as pd
from io import StringIO
params_df = pd.read_csv(
StringIO(
"parameter,value\ntheta,0.1\nbeta,0.5"
)
)
params_df
# parameter value
@vikjam
vikjam / join.r
Last active July 12, 2021 05:54
Joining datasets
library(dplyr) # Helps manipulate data
# Let's create example datasets
records <- data.frame(
user_id = c(1, 2, 3, 4, 5),
location = c("A", "B", "C", "C", "D")
)
# user_id location
# 1 1 A
# 2 2 B
@vikjam
vikjam / suppress_stdout_stderr.py
Created February 18, 2021 04:34
Suppress output from function
# Via Stack Overflow
# https://stackoverflow.com/questions/11130156/suppress-stdout-stderr-print-from-python-functions
from contextlib import contextmanager,redirect_stderr,redirect_stdout
from os import devnull
@contextmanager
def suppress_stdout_stderr():
"""A context manager that redirects stdout and stderr to devnull"""
with open(devnull, 'w') as fnull:
with redirect_stderr(fnull) as err, redirect_stdout(fnull) as out:
@vikjam
vikjam / display_more.py
Created February 13, 2021 06:54
Display many rows of a pandas DataFrame
# Adapted from https://www.kaggle.com/jamesleslie/titanic-neural-network-for-beginners
def display_more(df, nrows=1_000, ncols=1_000):
with pd.option_context("display.max_rows", nrows, "display.max_columns", ncols):
display(df)
@vikjam
vikjam / example.py
Created January 30, 2021 01:05
Python mutable default arguments
l = ["m"]
def return_list(lst=l):
return(f"lst={lst} and l={l}")
l.append("n")
print(return_list()) # lst=['m', 'n'] and l=['m', 'n']
print(return_list(lst=l)) # lst=['m', 'n'] and l=['m', 'n']
l = ["n"]
print(return_list()) # lst=['m', 'n'] and l=['n']
@vikjam
vikjam / g_see.R
Last active October 22, 2021 17:16
Data browser in the browser with googleVis (gvisTable)
g_see <- function(dat) {
plot(
googleVis::gvisTable(dat,
options = list(page = "enable")
)
)
}
@vikjam
vikjam / make_gifs.sh
Last active April 30, 2020 23:24
Making GIFs, but convert them to .gif beforehand
# https://github.com/kohler/gifsicle/issues/12
# brew install imagemagick gifsicle
convert *.jpeg GIF:- | gifsicle --delay=50 --loop --optimize=2 --colors=256 --multifile - > out.gif
@vikjam
vikjam / .gitignore
Last active November 6, 2020 18:18
Ignore .env and specific folders in Git
# Ignore everything in this directory (place in the directory you want to ignore)
*
# Except this file
!.gitignore
@vikjam
vikjam / README.md
Last active April 5, 2020 16:54
COVID-19 Cases (San Diego County, by ZIP Code) - Updated April 4, 2020

COVID-19 Cases (San Diego County, by ZIP Code) - Updated April 4, 2020

Sources:

County officials stressed the presence of cases, or lack thereof, should not affect how people operate during the coronavirus pandemic.

Staying at home, practicing social distancing and washing your hands are just some practices to maintain, regardless of the number of cases reported in your community.