Skip to content

Instantly share code, notes, and snippets.

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

Anthony Yoskovich ayoskovich

🏠
Working from home
View GitHub Profile
@ayoskovich
ayoskovich / legend_styling.R
Created December 14, 2021 16:04
Style the legend in ggplot
df %>%
ggplot(aes(x, y, color=foo))+
geom_count()+
# Remove legend for only the size aesthetic
scale_size(guide = 'none')+
# Increase legend point size
guides(colour = guide_legend(override.aes = list(size=5)))
@ayoskovich
ayoskovich / facet_style.R
Created November 9, 2021 16:21
Style facets in ggplot
theme_bw()+
theme(
axis.ticks = element_blank(),
axis.text = element_text(color='black'),
legend.position = 'none',
strip.text.y.left = element_text(angle = 0, face = 'bold'),
strip.text.x = element_text(face='bold'),
strip.placement = 'outside',
strip.background = element_rect(fill='white', linetype = 'blank')
)
@ayoskovich
ayoskovich / snippets.R
Created November 6, 2021 00:37
R snippets
snippet qq
dbGetQuery(con, read_file(here('${0}')))
snippet gl
${0} %>% glimpse()
snippet vv
${0} %>% View()
snippet pw
@ayoskovich
ayoskovich / countem.R
Created November 2, 2021 17:01
Create dataframe of value counts of all variables in select statement.
df %>%
select(
colA,
colB,
colC
) %>%
mutate_all(~as.character(.x)) %>%
map_df(
~count(data.frame(x=.x), x),
.id="var"
@ayoskovich
ayoskovich / snippets.R
Created July 27, 2021 15:18
My snippets for R
snippet qq
dbGetQuery(con, read_file(here('${0}')))
snippet vv
${0} %>% View()
@ayoskovich
ayoskovich / dplyr_rowwise_isna.R
Created July 26, 2021 14:41
Filter based on data nullness (?) across many columns
howMany <- function(x){ length(unique(x))}
allSame <- function(x){ length(unique(x)) == 1}
df %>%
# Keep rows that have values for all columns that contain '-'
filter(if_all(matches('-'), ~ !is.na(.))) %>%
rowwise() %>%
# Use c_across to work with rowwise
@ayoskovich
ayoskovich / refresh_doc.R
Created July 19, 2021 20:29
Close and re-open a word / powerpoint file from within R
ref_doc <- function(fname, prog){
system(paste0("TASKKILL /F /IM ", prog))
browseURL(fname)
}
ref_doc('foo.docx',
'winword.exe')
@ayoskovich
ayoskovich / read_sql.py
Created March 19, 2021 14:54
Read an external sql file into a pandas dataframe.
import pyodbc
import pandas as pd
def rs(fname, conn):
""" Returns a pandas dataframe from external file. """
with open(fname, 'r') as f:
return pd.read_sql_query(f.read(), conn)
conn = pyodbc.connect('Driver={SQL Server};'
'Server=servername;'
@ayoskovich
ayoskovich / within.md
Created February 21, 2021 15:08
Google sheets within cell chart

Use the value in D2 to create a red bar chart relative to the max value in column d

=SPARKLINE(D2,{"charttype","bar"; "color1", "red"; "max",max($D$2:D)})
@ayoskovich
ayoskovich / whoa.py
Created February 16, 2021 23:02
Filter pandas on mult criteria
df = pd.DataFrame({"AAA": [4, 5, 6, 7], "BBB": [10, 20, 30, 40], "CCC": [100, 50, -30, -50]})
Crit1 = df.AAA <= 5.5
Crit2 = df.BBB == 10.0
Crit3 = df.CCC > -40.0
# Could do this
# AllCrit = Crit1 & Crit2 & Crit3
# But this is better