background preloader

Functions

Facebook Twitter

Find hidden code for functions. Retrieve an R Object, Including from a Namespace. Description These functions locate all objects with name matching their argument, whether visible on the search path, registered as an S3 method or in a namespace but not exported. getAnywhere() returns the objects and argsAnywhere() returns the arguments of any objects that are functions.

Retrieve an R Object, Including from a Namespace

Usage getAnywhere(x) argsAnywhere(x) Arguments Details These functions look at all loaded namespaces, whether or not they are associated with a package on the search list. Finding the code for a ‘hidden’ function in R. Finding the code for a ‘hidden’ function in R If you want to understand what a certain function is doing, or you want to adapt it, you can type in the function without bracket on the command line.

Finding the code for a ‘hidden’ function in R

However, sometimes functions are ‘hidden’. You can use methods(function), which will give you the methods for that function (see ? Methods for more detailed information). Take for example the kruskal.test. Generate and Retrieve Many Objects with Sequential Names. Applying an Operation to a List of Variables. Just a quick note on a short hack that I cobbled together this morning.

Applying an Operation to a List of Variables

I have an analysis where I need to perform the same set of operations to a list of variables. In order to do this in a compact and robust way, I wanted to write a loop that would run through the variables and apply the operations to each of them in turn. This can be done using get() and assign(). To illustrate the procedure, I will use the simple example of squaring the numerical values stored in three variables. First we initialise the variables. Then we loop over the variable names (as strings), creating a temporary copy of each one and applying the operation to the copy. Finally we check that the operation has been executed as expected. This is perhaps a little wasteful in terms of resources (creating the temporary variables), but does the job.

Following up on the numerous insightful responses to this post, there are a number of other ways of skinning the same cat. Perform a Function on Each File in R. Sometimes you might have several data files and want to use R to perform the same function across all of them.

Perform a Function on Each File in R

Or maybe you have multiple files and want to systematically combine them into one file without having to open each file and manually copy the data out. Fortunately, it's not complicated to use R to systematically iterate across files. Finding or Choosing the Names of Data Files There are multiple ways to find or choose the names of the files you want to analyze. Loading Multiple Shapefiles to the R-Console Simultaneously. Invisible {base} Substitute. Substituting and Quoting Expressions Description substitute returns the parse tree for the (unevaluated) expression expr, substituting any variables bound in env. quote simply returns its argument.

substitute

The argument is not evaluated and can be any R expression. Introducing ‘propagate’ Get. Magic empty bracket. Addmargins {stats} Puts Arbitrary Margins on Multidimensional Tables or Arrays Description For a given table one can specify which of the classifying factors to expand by one or more levels to hold margins to be calculated.

addmargins {stats}

One may for example form sums and means over the first dimension and medians over the second. The resulting table will then have two extra levels for the first dimension and one extra level for the second. The default is to sum over all margins in the table. Symmetric set differences in R. My .Rprofile contains a collection of convenience functions and function abbreviations.

Symmetric set differences in R

These are either functions I use dozens of times a day and prefer not to type in full: ## my abbreviation of head() h <- function(x, n=10) head(x, n) ## and summary() ss <- summary Or problems that I'd rather figure out once, and only once: ## example: ## between( 1:10, 5.5, 6.5 ) between <- function(x, low, high, ineq=F) { ## like SQL between, return logical index if (ineq) { x >= low & x <= high } else { x > low & x < high } } One of these "problems" that's been rattling around in my head is the fact that setdiff(x, y) is asymmetric, and has no options to modify this. Setdiff.data.frame. Data Preparation - Part I. The R language provides tools for modeling and visualization, but is still an excellent tool for handling/preparing data.

Data Preparation - Part I

As C++ or python, there is some tricks that bring performance, make the code clean or both, but especially with R these choices can have a huge impact on performance and the “size” of your code. A seasoned R user can manage this effectively, but this can be a headache to a new user. SO, in this series of posts i will present some data preparation techniques that anyone should know about, at least the ones i know! 1. Using apply, lappy, tapply Sometimes the apply’s can make your code faster, sometimes just cleaner. 3 Quick and Simple Data Cleaning Helper Functions (December 2013) As I go about cleaning and merging data sets with R I often end up creating and using simple functions over and over.

3 Quick and Simple Data Cleaning Helper Functions (December 2013)

When this happens, I stick them in the DataCombine package. Reading tables that need a little cleaning. Sometimes one needs to read tables that are a bit messy, so that read.table doesn’t immediately recognize the content as numerical.

Reading tables that need a little cleaning

Maybe some weird characters are sprinkled in the table (ever been given a table with significance stars in otherwise numerical columns?). Some search and replace is needed. Identifying Records in Data Frame A That Are Not Contained In Data Frame B – A Comparison. > XinY <- + function(x, y, by = intersect(names(x), names(y)), by.x = by, by.y = by, + notin = FALSE, incomparables = NULL, Summarize content of a vector or data.frame every n entries. CompactPDF {tools}

Description Re-save PDF files (especially vignettes) more compactly. Support function for R CMD build --compact-vignettes. Usage compactPDF(paths, qpdf = Sys.which(Sys.getenv("R_QPDF", "qpdf")), gs_cmd = Sys.getenv("R_GSCMD", ""), gs_quality = Sys.getenv("GS_QUALITY", "none"), gs_extras = character()) ## S3 method for class 'compactPDF': format((x, ratio = 0.9, diff = 1e4, ...))

Compare. Description Compare two objects and, if they are not the same, attempt to transform them to see if they are the same after being transformed. Boolean operators && and.