A set of convenience functions for everyday programming and for visualization. Work in progress. Currently just a bunch of functions that I use very often.
The qbarplot()
function allows for quickly generating ggplot2 barplots
from raw dataframes:
qbarplot(diamonds, color, cut)
The qbeeswarm()
function allows for quickly generating ggplot2
beeswarm plots with overlaid boxplots. Depends on the ggbeeswarm
package.
set.seed(200)
df <- data.frame(A = c(rep("high", 50), rep("lo", 50)),
B = round(runif(n = 100)*100))
qbeeswarm(df, x = A, y = B)
The pretty_df()
function formats the output of dataframes in a way
that there are two digits after the decimal separator. Note that the
“prettified” version will often not be printed in the R output but it
will appear e.g. if you export the data to a CSV file.
df <- data.frame(a = c("a", "b", "c"),
b = c(0.89469435394539469569, 123684.2683523825, 0.00000005))
p_df <- pretty_df(df)
write.csv(p_df, "prettydataframe.csv")
This might be helpful for researchers in the field of language
acquisition, where we often work with age values formatted like
“years;months.days”, e.g. “2;3.4”. R will usually parse these as
character values, and if there are no trailing zeros (as in “02;03.04”),
they will be sorted incorrectly (so that a child at 02;10.17 is suddenly
younger than a child at 2;1.11). This function circumvents this problem
by adding trailing zeros. If you prefer the formatting without trailing
zeros, use factors = TRUE
and the factor levels of the original age
column will be ordered correctly.
# for vectors
x <- c("1;3.4", "1;2.5", "1;4.3")
format_age(x)
# for dataframes:
df <- c(age = c("1;3.4", "1;2.5", "1;4.3"),
utterance = "bla", "bla", "bla")
format_age(df,
col = "age", # name of the age column
month_separator = ";", # sign separating month from year
day_separator = ".", # sign separating day from month
factors = T # reorder factor levels of original column (default is FALSE)
)
# if the column name of the dataframe is "age" or "Age"
# (or is the only column containing the string "age" or "Age"),
# you don't have to specify the column name:
df <- c(age = c("1;3.4", "1;2.5", "1;4.3"),
utterance = "bla", "bla", "bla")
format_age(df)
The load_packages()
function checks whether the packages specified as
its arguments are installed and installs them if they are not. If
require = TRUE
(the default), they are also loaded in the process.
More functions following soon!
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.