Skip to content

Instantly share code, notes, and snippets.

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

vikjam

💭
👨🏾‍💻
View GitHub Profile
@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 / 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 / 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 / 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