format(x, trim=FALSE, digits=NULL)
Format an R object (e.g. a data frame) to print helpful information from a function.
- x – A data frame, matrix, or other R object.
- trim – Whether to prevent right-justification.
- digits – Number of digits to show.
Obtain the first several rows of a matrix or data frame using head, and use tail to obtain the last several rows. These functions may also be applied to obtain the first or last values in a vector.
- x – A matrix, data frame, or vector.
- n – The first n rows (or values if x is a vector) will be returned.
- x – A matrix, data frame, or vector.
- n – The last n rows (or values if x is a vector) will be returned.
merge(x, y, all=FALSE)
Merge two data frames together. The use described here assumes that the two data frames being merged have one common column name. The Tip describes how to handle other scenarios.
- x – Data frame to be merged with y
- y – Data frame to be merged with x
- all – Indicate whether to include all rows from both data frames. The default (FALSE) specifies that only the rows that share a common ID in the overlapping column should be included.
smoothScatter(x, y, bandwidth, nrpoints=100)
Create a density plot of bivariate data using smoothScatter.
- x – Numeric vector for x-coordinate.
- y – Numeric vector for y-coordinate.
- bandwidth – A vector of length 1 or 2 specifying the bandwidth in the kernel density estimate. A default bandwidth will be chosen if this argument is not specified.
- nrpoints – The number of points to explicitly show on the plot.
missing is used within a function to check whether a call to the function is missing a particular argument (returning TRUE) or whether a value was specified for the argument (FALSE).
- x – An argument name of the parent function.
ifelse(test, yes, no)
The ifelse function is used to assign one object or another depending on whether the first argument, test, is TRUE or FALSE. It even works as one would hope when test is a vector.
- test – A logical expression, which may be a vector.
- yes – What to return if test is TRUE.
- no – What to return if test is FALSE.
colSums(x), rowSums(x), colMeans(x), rowMeans(x)
The function apply
was described yesterday. The four functions for today’s post provide even faster ways to perform sums and means across rows and columns.
apply(x, margin, function, …)
apply implements a function across the rows or columns of a matrix. It also works on multidimensional arrays and can be used to apply functions across one or more dimensions of those arrays.
- x – A matrix or array.
- margin – The dimension of the matrix or array x to preserve. If x is an array, then this may be a vector indicating each dimension that should be preserved.
- function – A function (e.g. mean) to be applied across each dimension not listed in margin. In a matrix, the function would be applied across the rows or columns of x, whichever dimension is not listed in the margin argument.
Create a progress bar for a loop using the txtProgressBar function, then use the setTxtProgressBar command within the loop to print the progress bar, which updates as the loop progresses.
txtProgressBar(min, max, style=1)
- min – The minimum value in the loop.
- max – The maximum value in the loop.
- style – A number representing the style, where 3 is my favorite.
- pb – A progress bar object that was output from txtProgressBar.
- value – The current iteration value.
Save a particular set of objects using save, or save all objects in the current R session via save.image. Load an R data file into a session using load.
- … – List all objects that should be saved.
- file – Provide the file name, typically ending in .rda. Also, because the list of objects to save is arbitrarily long, the file argument must begin with the argument name, i.e. include file= in this argument’s declaration.
- file – Provide the file name, typically ending in .rda or .RData.
- file – The name of the file to be loaded.
In each case, the file name may also include a path to the file.