Selected functions I've written for use with R. Full documentation available in the "docs
" folder (or as a PDF).
All functions can be downloaded from the "scripts
" folder.
Alternatively, if you have the "RCurl" package installed, you can use:
library(RCurl)
baseURL = c("https://raw.github.com/mrdwab/2657-R-Functions/master/")
source(textConnection(getURL(paste0(baseURL, "scripts/---script-name---.R"))))
All scripts mentioned in the snippets
section are found in a single snippets.R
script.
Functions include:
-
Use to split cells which contain concatenated data into separate columns. Works with string and numeric data. For numeric data, the splitted output can retain original values or be recoded as "1" and "NA" to facilitate frequency calculations. Can also split data into a
list
within adata.frame
. -
Sort a
data.frame
by rows, columns, or both. Can also be used to subset data. -
Takes columns from a
data.frame
containing Boolean multiple-response data and tabulates the output. -
Uses data from the Genealogy Data: Frequently Occurring Surnames from Census 1990--Names Files web page to generate a
data.frame
with random names. -
Use to extract rows which contan a specified column's
min
,median
, ormax
values, or to extract rows with specified quantiles. -
Used to determine the desired sample size of a given population, or the confidence interval for a given population and sample.
-
Used to sample from a
data.frame
according to a grouping (or stratification) variable. -
The
stringseed.sampling()
function is designed as a batch sampling function that allows the user to specify any alphanumeric input as the seed per sample in the batch. -
The
table2df()
function takes an object of classtable
,ftable
, orxtabs
and converts it to adata.frame
or alist
ofdata.frame
s.
The snippets.R
script includes several small "utility" functions. In cases where they are functions I've found online, I've mentioned the source in the head of the function.
Snippets include:
-
load.scripts.and.data
Loads all scripts and data files from a specified set of directories matching a specified pattern.
-
unlist.dfs
Takes a list of
data.frame
s and assigns them as individualdata.frame
s in the current workspace. -
dfcols.list
Takes a
data.frame
and converts it to a list where each list item represents one of the columns from the originaldata.frame
. -
mv
Renames objects in the workspace in one step, instead of having to copy the object and remove the original object.
-
tidyHTML
Reformats a web page using HTML Tidy (the online service) and uses the XML package to parse the resulting file. Can optionally save the reformatted page.
-
round2
Rounds numbers according to the rule you might have learned in school, not according to "round to even" (which is less biased and is used by R).
-
CBIND
Binds (by columns)
data.frame
s with differing number of rows, filling the extra rows withNA
values. -
randomNamesOnline
Like the
RandomNames()
function, but uses an online service (http://random-name-generator.info/) to generate the names. -
stringseed.basic
Like the
stringseed.sampling()
function, but uses a much more basic approach to generating the seed. Also known asTDASample.R
.