<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>functions | Darren Liang</title><link>https://dliang55.netlify.app/category/functions/</link><atom:link href="https://dliang55.netlify.app/category/functions/index.xml" rel="self" type="application/rss+xml"/><description>functions</description><generator>Wowchemy (https://wowchemy.com)</generator><language>en-us</language><copyright>© 2026 Darren Liang</copyright><lastBuildDate>Tue, 30 Nov 2021 00:00:00 +0000</lastBuildDate><image><url>https://dliang55.netlify.app/media/icon_hu0b7a4cb9992c9ac0e91bd28ffd38dd00_9727_512x512_fill_lanczos_center_3.png</url><title>functions</title><link>https://dliang55.netlify.app/category/functions/</link></image><item><title>df2ggplot: Quick and dirty ggplot</title><link>https://dliang55.netlify.app/post/df2ggplot/</link><pubDate>Tue, 30 Nov 2021 00:00:00 +0000</pubDate><guid>https://dliang55.netlify.app/post/df2ggplot/</guid><description>&lt;h1 id="purpose">Purpose&lt;/h1>
&lt;p>To quickly create a ggplot chart in R using any data object which has rows and columns. This function takes some typical defaults for creating a chart found in the ggplot cheatsheet (&lt;a href="https://raw.githubusercontent.com/rstudio/cheatsheets/main/data-visualization.pdf" target="_blank" rel="noopener">https://raw.githubusercontent.com/rstudio/cheatsheets/main/data-visualization.pdf&lt;/a>) to create a quickly stylized chart. The purpose is to just create a quick graph to visualize your data without thinking too much about the how and why that is to cut down the data management and time it takes to organize and create a graph.&lt;/p>
&lt;p>NOTE: This function doesn&amp;rsquo;t directly pipe into the ggplot package so there are limited settings which can be adjusted in this function however additional layers can be added as the output object is a ggplot compatible list object.&lt;/p>
&lt;h1 id="why-use-this-function">Why use this function?&lt;/h1>
&lt;p>This function allows you to quickly create a graph using any data object with rows and columns. Since most data is organized in a way that columns are variables and rows are participants, this function allows data to be inputted as is without conforming to the ggplot specific format. There are some idiosyncracies in R and ggplot such is not typical in any other programming language (e.g. pivotting tables) so this function will extract only the specified variables, clean, and pivot the table in addition to creating the specified chart, if avalilable, in addition to providing basic titles, labels, and scales. This should make ggplot more accessible with other types of objects (e.g. list, tibble, dataframe) as well as users coming from different programming languages as a function specification may be more intuitive.&lt;/p>
&lt;h1 id="function">Function&lt;/h1>
&lt;pre>&lt;code class="language-r">#install.packages(&amp;quot;tidyverse&amp;quot;)
library(tidyverse)
df2ggplot &amp;lt;- function(data, x, y = NULL, group = NULL, type = NULL) {
#' @author Darren Liang
#' @note Last updated: 01 DEC 2021
#' @description Quickly creates a type of chart using ggplot using the data
#' and variables from a typically organized data frame (columns are variables
#' and rows are participants). since it is not typical to pivot tables in
#' other programs, this function will extract and pivot only the required
#' variables for ease of use in addition to providing basic titles and labels.
#' @param data (list/ tibble/ data frame) A data object which contains columns
#' and rows. Will be coerced into a data frame anyways.
#' @param x (char) A character string which contains the name of a column
#' which can be found in the data object. Plotted on the x-axis.
#' @param y (char) OPTIONAL. A character string which contains the name of a
#' column which can be found in the data object. Plotted on the y-axis.
#' @param group (char) OPTIONAL. A character string which contains the name of
#' a column which can be found in the data object. Will be used to color
#' separate the group.
#' @param type (char) OPTIONAL. A character string which contains the name of
#' the type of chart you which to create. Refer to the ggplot2 cheat sheet for
#' the most popular usage types (only include the type past geom*). Not all
#' graph types are included in this function.
#' @usage df2ggplot(data = data, x = &amp;quot;var1&amp;quot;, y = &amp;quot;var2&amp;quot;, group = &amp;quot;var3&amp;quot;,
#' type = &amp;quot;chart&amp;quot;)
#' @return A list object which can be viewed as a plot.
#' @examples
#' df2ggplot(data = mtcars, x = &amp;quot;mpg&amp;quot;, y = &amp;quot;hp&amp;quot;, group = &amp;quot;gear&amp;quot;,
#' type = &amp;quot;jitter&amp;quot;)
# create a new data frame by extracting variables from the input data
data &amp;lt;- as.data.frame(data) %&amp;gt;%
# clean all empty cells in case there are any
compact() %&amp;gt;%
# selects non-NULL variables if they exists to keep
select(all_of(x), all_of(y), all_of(group)) %&amp;gt;%
pivot_longer(!c(all_of(y), all_of(group)),
names_to = &amp;quot;scores&amp;quot;, values_to = &amp;quot;values&amp;quot;)
# group_by() if applicable
for (index in seq_along(group)) {
group_by(data, group[[index]])
}
# plot the object using the specified features
plt &amp;lt;- data %&amp;gt;%
ggplot(aes(x = values)) +
theme_classic() +
labs(x = x, title = paste(x))
if (!is.null(y)) {
plt &amp;lt;- plt +
aes(y = get(y)) +
labs(x = x, y = y, title = paste(x, &amp;quot;vs.&amp;quot;, y))
}
if (!is.null(group)) {
plt &amp;lt;- plt +
aes(color = get(group)) +
labs(color = group)
}
# select type of graph based on type of data
# Some but not all popular geom* included from ggplot2 cheat sheet found at:
# https://raw.githubusercontent.com/rstudio/cheatsheets/main/data-visualization.pdf
if (is.null(type)) {
message(&amp;quot;Type not specified, default visualization selected.&amp;quot;)
if (is.null(y)) {
plt &amp;lt;- plt +
geom_histogram(binwidth = 5)
} else {
plt &amp;lt;- plt +
geom_jitter()
}
} else if (type == &amp;quot;area&amp;quot;) {plt &amp;lt;- plt + geom_area()
} else if (type == &amp;quot;density&amp;quot;) {plt &amp;lt;- plt + geom_density(kernel = &amp;quot;gaussian&amp;quot;)
} else if (type == &amp;quot;dotplot&amp;quot;) {plt &amp;lt;- plt + geom_dotplot()
} else if (type == &amp;quot;freqpoly&amp;quot;) {plt &amp;lt;- plt + geom_freqpoly()
} else if (type == &amp;quot;histogram&amp;quot;) {plt &amp;lt;- plt + geom_histogram(binwidth = 5)
} else if (type == &amp;quot;bar&amp;quot;) {plt &amp;lt;- plt + geom_bar()
} else if (type == &amp;quot;point&amp;quot;) {plt &amp;lt;- plt + geom_point()
} else if (type == &amp;quot;rug&amp;quot;) {plt &amp;lt;- plt + geom_rug(sides = &amp;quot;bl&amp;quot;)
} else if (type == &amp;quot;smooth&amp;quot;) {plt &amp;lt;- plt + geom_smooth(method = lm)
} else if (type == &amp;quot;col&amp;quot;) {plt &amp;lt;- plt + geom_col()
} else if (type == &amp;quot;boxplot&amp;quot;) {plt &amp;lt;- plt + geom_boxplot()
} else if (type == &amp;quot;violin&amp;quot;) {plt &amp;lt;- plt + geom_violin(scale = &amp;quot;area&amp;quot;)
} else if (type == &amp;quot;count&amp;quot;) {plt &amp;lt;- plt + geom_count()
} else if (type == &amp;quot;jitter&amp;quot;) {plt &amp;lt;- plt + geom_jitter()
} else if (type == &amp;quot;line&amp;quot;) {plt &amp;lt;- plt + geom_line()
} else {stop(&amp;quot;Graph type not included. Try ggplot geom_* instead.&amp;quot;)
}
return(plt)
}
&lt;/code>&lt;/pre>
&lt;h2 id="usage">Usage&lt;/h2>
&lt;p>df2ggplot(data = data, x = &amp;ldquo;var1&amp;rdquo;, y = &amp;ldquo;var2&amp;rdquo;, group = &amp;ldquo;var3&amp;rdquo;, type = &amp;ldquo;chart&amp;rdquo;)&lt;/p>
&lt;h3 id="arguments">Arguments&lt;/h3>
&lt;p>data (list/ tibble/ data frame): A data object which contains columns and rows. Will be coerced into a data frame anyways.&lt;/p>
&lt;p>x (char): A character string in quotes (either &amp;quot; or &amp;lsquo;) which contains the name of a column which can be found in the data object. Plotted on the x-axis.&lt;/p>
&lt;p>y (char): &lt;em>OPTIONAL.&lt;/em> A character string in quotes (either &amp;quot; or &amp;lsquo;) which contains the name of a column which can be found in the data object. Plotted on the y-axis.&lt;/p>
&lt;p>group (char): &lt;em>OPTIONAL.&lt;/em> A character string in quotes (either &amp;quot; or &amp;lsquo;) which contains the name of a column which can be found in the data object. Will be used to color separate the group.&lt;/p>
&lt;p>type (char): &lt;em>OPTIONAL.&lt;/em> A character string in quotes (either &amp;quot; or &amp;lsquo;) which contains the name of the type of chart you which to create. Refer to the ggplot2 cheat sheet for the most popular usage types (only include the type past geom*). Not all graph types are included in this function.&lt;/p>
&lt;h3 id="returns">Returns&lt;/h3>
&lt;p>plt (list): A single list object containing a ggplot compatible plot.&lt;/p>
&lt;h4 id="example">Example&lt;/h4>
&lt;p>df2ggplot(data = mtcars, x = &amp;ldquo;mpg&amp;rdquo;, y = &amp;ldquo;hp&amp;rdquo;, group = &amp;ldquo;gear&amp;rdquo;, type = &amp;ldquo;jitter&amp;rdquo;)&lt;/p>
&lt;p>See example output in knitted .Rmd to &lt;a href="https://dliang55.netlify.app/post/df2ggplot/df2ggplot.pdf" target="_blank">PDF&lt;/a>.&lt;/p></description></item><item><title>psypy2df: Data importation</title><link>https://dliang55.netlify.app/post/psypy2df/</link><pubDate>Fri, 26 Nov 2021 00:00:00 +0000</pubDate><guid>https://dliang55.netlify.app/post/psypy2df/</guid><description>&lt;h1 id="purpose">Purpose&lt;/h1>
&lt;p>For the automatation of data importation given an absolute or relative directory as a character vector. In particular, PsychoPy, a popular psychology experiment builder, allows the use of a graphical user interface (GUI) to build and program an experiment without the specific knowledge of coding. The organized data collected outputs per participant data in seperate files and formats (.csv, .log, and .psydat). This function may be used beyond this specific purpose with specific tweaks (commented in code) however it was written with this in mind. This function will read all comma seperated values (.csv) files in a given directory into R, clean up by removing rows and columns which belong to empty files, and output a single dataframe with all rows and columns with participant data.&lt;/p>
&lt;p>NOTE: This function will retain all the columns and non-empty rows whether or not they may be useful to you. If a project was started with many variables or changes were made to variable names, it is important to check which columns are useful to you.&lt;/p>
&lt;h1 id="why-use-this-function">Why use this function?&lt;/h1>
&lt;p>This function allows you to quickly import all the data from a given PsychoPy data directory to look at all the data at once. This is helpful to allow a user to check which files may or may not be useful in a single dataframe rather than opening multiple comma seperated value files one at a time. Whether your directory contains old data, partial data, or other issues with the PsychoPy output data, this function should retain all of the columns so that you can check the output dataframe to filter or select what is useful to you.&lt;/p>
&lt;h1 id="function">Function&lt;/h1>
&lt;pre>&lt;code class="language-r">#install.packages(&amp;quot;tidyverse&amp;quot;)
#install.packages(&amp;quot;readxl&amp;quot;)
library(tidyverse)
library(readxl)
psypy2df &amp;lt;- function(directory) {
#' @author Darren Liang
#' @note Last updated: 01 DEC 2021
#' @description Imports all *.csv files from a input directory as a single
#' data frame object.
#' @param directory (character) The absolute or relative path to a PsychoPy
#' data directory.
#' Usually ends with ../../data/ based on default Psychopy data structure.
#' @usage psypy2df(&amp;quot;directory&amp;quot;)
#' @return All the rows and columns from the .csv files from the directory as
#' a single data frame object.
#' @examples
#' psypy2df(&amp;quot;C:/Users/Admin/Documents/experiment/data&amp;quot;)
# preallocate dataframe for output object
raw.list &amp;lt;- list()
df &amp;lt;- data.frame()
# obtain all the files and read in files in the data directory
filelist &amp;lt;- list.files(path = directory, pattern = &amp;quot;*.csv&amp;quot;, full.names = TRUE)
for (index in 1:length(filelist)) {
raw.list[[index]] &amp;lt;- read_csv(filelist[[index]])
}
# remove empty files from the list
clean.list &amp;lt;- raw.list %&amp;gt;%
compact()
# remove all columns after framerate (framerate is usually the end of the
# useful information for Psychopy)
# if this function is being applied to another data structure, edit or remove
# the select() line
for (index in 1:length(clean.list)) {
clean.list[[index]] &amp;lt;- as.data.frame(clean.list[[index]]) %&amp;gt;%
select(1:frameRate)
df &amp;lt;- merge(df, clean.list[[index]], all = TRUE)
}
# output object as data frame
return(df)
}
&lt;/code>&lt;/pre>
&lt;h2 id="usage">Usage&lt;/h2>
&lt;p>psypy2df(&amp;lsquo;directory&amp;rsquo;)&lt;/p>
&lt;h3 id="arguments">Arguments&lt;/h3>
&lt;p>directory (char): An absolute or relative path to your directory containing all the .csv files which you wish to import into R in quotes (either &amp;quot; or &amp;lsquo;).&lt;/p>
&lt;h3 id="returns">Returns&lt;/h3>
&lt;p>df (dataframe): A single dataframe object containing all non-NA rows and columns from the imported .csv files.&lt;/p>
&lt;h4 id="example">Example&lt;/h4>
&lt;p>psypy2df(&amp;lsquo;C:/Users/dliang55/Downloads/segmentation-timing-master/data&amp;rsquo;)&lt;/p>
&lt;p>See example output in knitted .Rmd to &lt;a href="https://dliang55.netlify.app/post/psypy2df/psypy2df.pdf" target="_blank">PDF&lt;/a>.&lt;/p></description></item></channel></rss>