knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "README-" )
R package: Utility functions and addins for RStudio.
Helper tools I use in many projects.
By Ludvig R. Olsen,
Cognitive Science, Aarhus University.
Started in Feb. 2017
Contact at:
r-pkgs@ludvigolsen.dk
Main functions:
Development version:
install.packages("devtools")
devtools::install_github("LudvigOlsen/LRO.utilities")
# Attach packages library(LRO.utilities) library(dplyr) # %>% library(knitr) # kable() library(ggplot2)
Exponentiate vectors to make polynomials of degree 2 to degree.
E.g. create columns v^2, v^3, v^4...
# On vector vect <- c(1,3,5,7,8) polynomializer(vect, degree = 3) %>% kable() # On vectors in dataframe data <- data.frame(vect = vect, bect = vect*3, dect = vect*5) polynomializer(data, cols = c('bect','dect'), degree = 3) %>% kable()
"Not in"
c(1,3,5) %ni% c(2,3,6)
Center and/or scale multiple columns of a dataframe.
scaler is designed to work with %>% pipelines.
scaler_fit returns fit_object with information used to transform data.
scaler_transform scales data based on the information in the fit_object.
scaler_invert inverts scaling based on the information in the fit_object.
scaler_ and scaler_fit_ are standard evalution versions.
# Scale and center 'vect' and 'bect' # in dataframe from previous example scaler(data, vect, bect) %>% kable() # Only scaling 'vect' - working in pipeline data %>% scaler(vect, center = F) %>% kable() # Only center 'bect' and 'dect' # selecting with column index range data %>% scaler(2:3, scale = F) %>% kable()
# Fit scaler fitted_scaler <- data %>% scaler_fit() fitted_scaler %>% kable() # Transform data scaled_df <- data %>% scaler_transform(fit_object = fitted_scaler) scaled_df %>% kable() # Invert scaling scaled_df %>% scaler_invert(fit_object = fitted_scaler) %>% kable()
Binarize multiple columns of a dataframe based on a given threshold.
binarizer is designed to work with %>% pipelines.
binarizer_ is a standard evalution version.
scaled_df %>% binarizer(thresh = 0)
Rename single column in dataframe. This is a bit like plyr::rename just only for 1 column at a time.
rename_col(data, old_name = 'bect', new_name = 'sect') %>% kable()
Calculate Bayes factor from 2 distributions and plot the the two distributions.
Returns list with ggplot2 object, BF10, and BF01.
prior <- rnorm(1000, mean=0, sd=1) posterior <- rnorm(1000, mean=2, sd=2) s_d <- savage_dickey(posterior, prior, Q = 0, plot = TRUE) s_d$BF10 s_d$BF01 s_d$post_prior_plot + theme_bw()
Wrapper for zoo::rollapply for applying a function to rolling windows and getting the result of the previous window. Appends NAs at start only.
# Create dataframe df <- data.frame('round' = c(1,2,3,4,5,6,7,8,9,10), 'score' = c(5,3,4,7,6,5,2,7,8,6)) # For each row we find the mean score of the previous 2 rounds df$mean_prev_score = roll_previous(df$score, width = 2, FUN = mean) df %>% kable()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.