This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Load packages | |
library(dplyr) | |
library(sparklyr) | |
# Set up connect | |
sc <- spark_connect(master = "local") | |
# Create a Spark DataFrame of mtcars | |
mtcars_sdf <- copy_to(sc, mtcars) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sim_binom <- function(n_samples = 1000, n_features = 2, | |
true_target_prob = 0.5, beta = NULL, seed = NULL) { | |
if(!is.null(seed)) { | |
set.seed(seed) | |
} | |
x = matrix(rnorm(n_samples * n_features), | |
nrow = n_samples, ncol = n_features) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
# Nest iris by Species | |
iris_nest <- iris %>% | |
group_by(Species) %>% | |
nest() | |
# Get the data list and set the names of the list to Species | |
# write_csv for each df in the data list with its name as the filename | |
iris_nest %>% |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# create some data | |
(df <- data_frame(x = 1:2, | |
y = c(NA, NA), | |
z = c(NA, 3))) | |
# remove rows where either col y or z contain NA | |
# i.e. keep rows where all variables are not NA | |
df %>% | |
filter_at(vars(y:z), all_vars(!is.na(.))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(scales) | |
library(viridis) | |
show_col(viridis(12)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# create a tibble---------------------- | |
tbl <- tibble::tibble(x = letters[1:5], | |
y = letters[5:1]) | |
# returns a tibble -------------------- | |
dplyr::select(tbl, x) | |
tbl[1] | |
tbl[, 1] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# a function to format strings | |
# to be in Proper case | |
str_proper <- function(string) { | |
# get the first letter | |
first_letter = substring(string, first = 1, last = 1) | |
# get the other letters | |
other_letters = substring(string, first = 2) | |
# combine the first letter (upper case) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
group_prop <- function(df, ...) { | |
# enquo the dots | |
vars <- enquos(...) | |
# count then calculate | |
# proportions | |
df_count <- df %>% | |
count(!!!vars) | |
if (length(vars) > 1) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# function to round a value to the nearest digit | |
# e.g. if nearest = 5 then 42 would round to 40 | |
# and 47 would be rounded to 45 | |
# source: http://r.789695.n4.nabble.com/Rounding-to-the-nearest-5-td863189.html | |
round_nearest <- function(x, nearest) { | |
nearest * round(x / nearest) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#========================================================# | |
# Setup | |
#========================================================# | |
library(dplyr) | |
library(ggplot2) | |
library(here) | |
library(pwr) | |
library(scales) | |
library(stringr) |
OlderNewer