Title: | A Collection of Handy, Useful Functions |
---|---|
Description: | A set of little functions that have been found useful to do little odds and ends such as plotting the results of K-means clustering, substituting special text characters, viewing parts of a data.frame, constructing formulas from text and building design and response matrices. |
Authors: | Jared P. Lander [cre, aut], Nicholas Galasinao [ctb], Joshua Kraut [ctb], Daniel Chen [ctb] |
Maintainer: | Jared P. Lander <[email protected]> |
License: | BSD_3_clause + file LICENSE |
Version: | 1.2.7 |
Built: | 2024-10-25 02:59:58 UTC |
Source: | https://github.com/jaredlander/useful |
Plot acf objects
## S3 method for class 'acf' autoplot(object, xlab = x, ylab = y, title = sprintf("%s Plot", y), ...)
## S3 method for class 'acf' autoplot(object, xlab = x, ylab = y, title = sprintf("%s Plot", y), ...)
object |
An |
xlab |
X-axis label. |
ylab |
y-axis label. |
title |
Graph title. |
... |
Further arguments. |
Plot acf (and pacf) objects.
A ggplot object.
Jared P. Lander
autoplot(acf(sunspot.year, plot=FALSE)) autoplot(pacf(sunspot.year, plot=FALSE))
autoplot(acf(sunspot.year, plot=FALSE)) autoplot(pacf(sunspot.year, plot=FALSE))
Flip binary numbers
binary.flip(x)
binary.flip(x)
x |
A vector of 0/1 numbers. |
X with 0's flipped to 1's and 1's flipped to 0's
Jared P. Lander
binary.flip(c(1,1,0,1,0,0,1))
binary.flip(c(1,1,0,1,0,0,1))
Display the bottom left corner of a rectangular data set
bottomleft(x, r = 5L, c = 5L, ...)
bottomleft(x, r = 5L, c = 5L, ...)
x |
The data |
r |
Number of rows to display |
c |
Number of columns to show |
... |
Arguments passed on to other functions |
Displays the bottom left corner of a rectangular data set.
This is a wrapper function for corner
... The bottom left corner of the data set that was requested. The size depends on r and c.
Jared P. Lander www.jaredlander.com
head
tail
corner
topright
topleft
bottomright
left
right
data(diamonds) head(diamonds) # displays all columns bottomleft(diamonds) # displays last 5 rows and only the first 5 columns
data(diamonds) head(diamonds) # displays all columns bottomleft(diamonds) # displays last 5 rows and only the first 5 columns
Display the bottom right corner of a rectangular data set
bottomright(x, r = 5L, c = 5L, ...)
bottomright(x, r = 5L, c = 5L, ...)
x |
The data |
r |
Number of rows to display |
c |
Number of columns to show |
... |
Arguments passed on to other functions |
Displays the bottom right corner of a rectangular data set.
This is a wrapper function for corner
... The bottom right corner of the data set that was requested. The size depends on r and c.
Jared P. Lander www.jaredlander.com
head
tail
corner
topright
bottomleft
topleft
left
right
data(diamonds) head(diamonds) # displays all columns bottomright(diamonds) # displays last 5 rows and only the last 5 columns
data(diamonds) head(diamonds) # displays all columns bottomright(diamonds) # displays last 5 rows and only the last 5 columns
Formula Builder
build.formula(lhs, rhs)
build.formula(lhs, rhs)
lhs |
Character vector for left side of formula |
rhs |
Character vector for right side of formula |
Builds a formula easily given the left and right hand sides. Right now it only handles additive formulas and not interactions unless that is specified in the character.
A formula object
Jared P. Lander www.jaredlander.com
formula as.formula
build.formula("Y", "X") build.formula(c("Y", "Z"), "X") build.formula("Z", c("X", "Q")) build.formula(c("Y", "Z"), c("X", "Q"))
build.formula("Y", "X") build.formula(c("Y", "Z"), "X") build.formula("Z", c("X", "Q")) build.formula(c("Y", "Z"), c("X", "Q"))
Build the x matrix for a glmnet model
build.x(formula, data, contrasts = TRUE, sparse = FALSE)
build.x(formula, data, contrasts = TRUE, sparse = FALSE)
formula |
A formula |
data |
A data.frame |
contrasts |
Logical indicating whether a factor's base level is removed. Can be either one single value applied to every factor or a value for each factor. Values will be recycled if necessary. |
sparse |
Logical indicating if result should be sparse. |
Given a formula and a data.frame build the predictor matrix
A matrix of the predictor variables specified in the formula
Jared P. Lander
require(ggplot2) head(mpg) head(build.x(hwy ~ class + cyl + year, data=mpg)) testFrame <- data.frame(First=sample(1:10, 20, replace=TRUE), Second=sample(1:20, 20, replace=TRUE), Third=sample(1:10, 20, replace=TRUE), Fourth=factor(rep(c("Alice","Bob","Charlie","David"), 5)), Fifth=ordered(rep(c("Edward","Frank","Georgia","Hank","Isaac"), 4)), Sixth=factor(rep(c("a", "b"), 10)), stringsAsFactors=F) head(build.x(First ~ Second + Fourth + Sixth, testFrame, contrasts=c("Fourth"=TRUE, "Fifth"=FALSE, "Sixth"=TRUE))) head(build.x(First ~ Second + Fourth + Fifth + Sixth, testFrame, contrasts=c(Fourth=TRUE, Fifth=FALSE, Sixth=TRUE))) head(build.x(First ~ Second + Fourth + Fifth + Sixth, testFrame, contrasts=TRUE)) head(build.x(First ~ Second + Fourth + Fifth + Sixth, testFrame, contrasts=FALSE)) head(build.x(First ~ Second + Fourth + Fifth + Sixth - 1, testFrame, contrasts=TRUE)) build.x(First ~ Second + Fourth + Fifth + Sixth - 1, testFrame, contrasts=TRUE, sparse=TRUE) head(build.x(First ~ Second + Fourth + Fifth + Fourth*Sixth, testFrame, contrasts=TRUE)) head(build.x(First ~ Second + Fourth + Fifth + Third*Sixth, testFrame, contrasts=TRUE)) #' head(build.x(First ~ Second + Fourth + Fifth + Fourth*Sixth, testFrame, contrasts=FALSE)) head(build.x(First ~ Second + Fourth + Fifth + Third*Sixth, testFrame, contrasts=FALSE)) build.x(First ~ Second + Fourth + Fifth + Third*Sixth, testFrame, contrasts=FALSE, sparse=TRUE) ## if contrasts is a list then you can specify just certain factors
require(ggplot2) head(mpg) head(build.x(hwy ~ class + cyl + year, data=mpg)) testFrame <- data.frame(First=sample(1:10, 20, replace=TRUE), Second=sample(1:20, 20, replace=TRUE), Third=sample(1:10, 20, replace=TRUE), Fourth=factor(rep(c("Alice","Bob","Charlie","David"), 5)), Fifth=ordered(rep(c("Edward","Frank","Georgia","Hank","Isaac"), 4)), Sixth=factor(rep(c("a", "b"), 10)), stringsAsFactors=F) head(build.x(First ~ Second + Fourth + Sixth, testFrame, contrasts=c("Fourth"=TRUE, "Fifth"=FALSE, "Sixth"=TRUE))) head(build.x(First ~ Second + Fourth + Fifth + Sixth, testFrame, contrasts=c(Fourth=TRUE, Fifth=FALSE, Sixth=TRUE))) head(build.x(First ~ Second + Fourth + Fifth + Sixth, testFrame, contrasts=TRUE)) head(build.x(First ~ Second + Fourth + Fifth + Sixth, testFrame, contrasts=FALSE)) head(build.x(First ~ Second + Fourth + Fifth + Sixth - 1, testFrame, contrasts=TRUE)) build.x(First ~ Second + Fourth + Fifth + Sixth - 1, testFrame, contrasts=TRUE, sparse=TRUE) head(build.x(First ~ Second + Fourth + Fifth + Fourth*Sixth, testFrame, contrasts=TRUE)) head(build.x(First ~ Second + Fourth + Fifth + Third*Sixth, testFrame, contrasts=TRUE)) #' head(build.x(First ~ Second + Fourth + Fifth + Fourth*Sixth, testFrame, contrasts=FALSE)) head(build.x(First ~ Second + Fourth + Fifth + Third*Sixth, testFrame, contrasts=FALSE)) build.x(First ~ Second + Fourth + Fifth + Third*Sixth, testFrame, contrasts=FALSE, sparse=TRUE) ## if contrasts is a list then you can specify just certain factors
Build the y object from a formula and data
build.y(formula, data)
build.y(formula, data)
formula |
A formula |
data |
A data.frame |
Given a formula and a data.frame build the y object
The y object from a formula and data
Jared P. Lander
require(ggplot2) head(mpg) head(build.y(hwy ~ class + cyl + year, data=mpg))
require(ggplot2) head(mpg) head(build.y(hwy ~ class + cyl + year, data=mpg))
Converts polar coordinates to cartesian coordinates
cart2pol(x, y, degrees = FALSE)
cart2pol(x, y, degrees = FALSE)
x |
The x-coordinate of the point |
y |
The y-coordinate of the point |
degrees |
Logical indicating if theta should be returned in degrees |
Converts polar coordinates to cartesian coordinates using a simple conversion. The angle, theta
must be in radians.
Somewhat inspired by http://www.r-bloggers.com/convert-polar-coordinates-to-cartesian/ and https://www.mathsisfun.com/polar-cartesian-coordinates.html
A data.frame holding the polar coordinates and the original (x,y) coordinates
Jared P. Lander
library(dplyr) x1 <- c(1, sqrt(3)/2, sqrt(2)/2, 1/2, 0) y1 <- c(0, 1/2, sqrt(2)/2, sqrt(3)/2, 1) d1 <- tibble::tibble(x=x1, y=y1, Q='I') x2 <- c(0, -1/2, -sqrt(2)/2, -sqrt(3)/2, -1) y2 <- c(1, sqrt(3)/2, sqrt(2)/2, 1/2, 0) d2 <- tibble::tibble(x=x2, y=y2, Q='II') x3 <- c(-1, -sqrt(3)/2, -sqrt(2)/2, -1/2, 0) y3 <- c(0, -1/2, -sqrt(2)/2, -sqrt(3)/2, -1) d3 <- tibble::tibble(x=x3, y=y3, Q='III') x4 <- c(0, 1/2, sqrt(2)/2, sqrt(3)/2, 1) y4 <- c(-1, -sqrt(3)/2, -sqrt(2)/2, -1/2, 0) d4 <- tibble::tibble(x=x4, y=y4, Q='IV') dAll <- bind_rows(d1, d2, d3, d4) cart2pol(dAll$x, dAll$y) cart2pol(dAll$x, dAll$y, degrees=TRUE)
library(dplyr) x1 <- c(1, sqrt(3)/2, sqrt(2)/2, 1/2, 0) y1 <- c(0, 1/2, sqrt(2)/2, sqrt(3)/2, 1) d1 <- tibble::tibble(x=x1, y=y1, Q='I') x2 <- c(0, -1/2, -sqrt(2)/2, -sqrt(3)/2, -1) y2 <- c(1, sqrt(3)/2, sqrt(2)/2, 1/2, 0) d2 <- tibble::tibble(x=x2, y=y2, Q='II') x3 <- c(-1, -sqrt(3)/2, -sqrt(2)/2, -1/2, 0) y3 <- c(0, -1/2, -sqrt(2)/2, -sqrt(3)/2, -1) d3 <- tibble::tibble(x=x3, y=y3, Q='III') x4 <- c(0, 1/2, sqrt(2)/2, sqrt(3)/2, 1) y4 <- c(-1, -sqrt(3)/2, -sqrt(2)/2, -1/2, 0) d4 <- tibble::tibble(x=x4, y=y4, Q='IV') dAll <- bind_rows(d1, d2, d3, d4) cart2pol(dAll$x, dAll$y) cart2pol(dAll$x, dAll$y, degrees=TRUE)
Get class information for each column in a data.frame
.
classdf(data, cols)
classdf(data, cols)
data |
|
cols |
The columns (named or numeric) to be included in the check. |
Get class information for each column in a data.frame
.
A vector detailing the class of each column.
Jared P. Lander
classdf(CO2) classdf(iris) classdf(mtcars)
classdf(CO2) classdf(iris) classdf(mtcars)
Moves column names to the front or back of the names
colsToFront(data, cols = names(data)) colsToBack(data, cols = names(data))
colsToFront(data, cols = names(data)) colsToBack(data, cols = names(data))
data |
data.frame or tbl |
cols |
Columns that should be moved |
Moves column names to the front or back of the names
Character vector of column names
Jared P. Lander
theDF <- data.frame(A=1:10, B=11:20, C=1:10, D=11:20) colsToFront(theDF, c('B', 'C')) colsToFront(theDF, c('C', 'B')) colsToFront(theDF, c('C', 'C')) colsToBack(theDF, c('C', 'C')) colsToBack(theDF, c('C', 'B')) colsToBack(theDF, c('C', 'C'))
theDF <- data.frame(A=1:10, B=11:20, C=1:10, D=11:20) colsToFront(theDF, c('B', 'C')) colsToFront(theDF, c('C', 'B')) colsToFront(theDF, c('C', 'C')) colsToBack(theDF, c('C', 'C')) colsToBack(theDF, c('C', 'B')) colsToBack(theDF, c('C', 'C'))
List Comparison
## S3 method for class 'list' compare(a, b)
## S3 method for class 'list' compare(a, b)
a |
A List |
b |
A List |
Compare elements of two equal length lists.
A vector with a logical indicator for equality of each element author Jared P. Lander www.jaredlander.com
vect <- c(mean, mode, mean) vect2 <- c(mean, mode, max) vect3 <- c(mean, mean) compare.list(vect, vect) compare.list(vect, vect2) tryCatch(compare.list(vect, vect3), error=function(e) print("Caught error"))
vect <- c(mean, mode, mean) vect2 <- c(mean, mode, max) vect3 <- c(mean, mean) compare.list(vect, vect) compare.list(vect, vect2) tryCatch(compare.list(vect, vect3), error=function(e) print("Caught error"))
Runs the computation found in http://www.stat.columbia.edu/~madigan/DM08/descriptive.ppt.pdf
ComputeHartigan(FitActualWSS, FitPlus1WSS, nrow)
ComputeHartigan(FitActualWSS, FitPlus1WSS, nrow)
FitActualWSS |
the WSS from a kmeans fit |
FitPlus1WSS |
the WSS from a kmeans fit |
nrow |
the number of rows in the original dataset |
Not exported, only used by FitKMeans
The computed Hartigan Number
Jared P. Lander www.jaredlander.com
http://www.stat.columbia.edu/~madigan/DM08/descriptive.ppt.pdf
data(iris) hartiganResults <- FitKMeans(iris[, -ncol(iris)]) PlotHartigan(hartiganResults)
data(iris) hartiganResults <- FitKMeans(iris[, -ncol(iris)]) PlotHartigan(hartiganResults)
Helper function for imputing constants
constant(n = 1)
constant(n = 1)
n |
The value to return |
Returns a function that always returns the value of n.
A function that when used simply returns n.
Jared P. Lander
constant(4)(1:10) theDF <- data.frame(A=1:10, B=1:10, C=1:10) theDF[c(1, 4, 6), c(1)] <- NA theDF[c(3, 4, 8), c(3)] <- NA simple.impute(theDF, constant(4))
constant(4)(1:10) theDF <- data.frame(A=1:10, B=1:10, C=1:10) theDF[c(1, 4, 6), c(1)] <- NA theDF[c(3, 4, 8), c(3)] <- NA simple.impute(theDF, constant(4))
Display a corner section of a rectangular data set
corner(x, ...) ## S3 method for class 'data.frame' corner(x, r = 5L, c = 5L, corner = "topleft", ...) ## S3 method for class 'matrix' corner(x, r = 5L, c = 5L, corner = "topleft", ...) ## S3 method for class 'table' corner(x, r = 5L, c = 5L, corner = "topleft", ...) ## Default S3 method: corner(x, r = 5L, ...)
corner(x, ...) ## S3 method for class 'data.frame' corner(x, r = 5L, c = 5L, corner = "topleft", ...) ## S3 method for class 'matrix' corner(x, r = 5L, c = 5L, corner = "topleft", ...) ## S3 method for class 'table' corner(x, r = 5L, c = 5L, corner = "topleft", ...) ## Default S3 method: corner(x, r = 5L, ...)
x |
The data |
... |
Arguments passed on to other functions |
r |
Number of rows to display |
c |
Number of columns to show |
corner |
Which corner to grab. Possible values are c("topleft", "bottomleft", "topright", "bottomright") |
Grabs a corner of a data set
Display a corner section of a rectangular data set
Displays a corner of a rectangular data set such as a data.frame, matrix or table. If showing the right side or bottom, the order of the data is preserved.
The default method reverts to simply calling head
corner of a rectangular data set such as a data.frame, matrix or table. If showing the right side or bottom, the order of the data is preserved.
... The part of the data set that was requested. The size depends on r and c and the position depends on corner.
Jared P. Lander
head
tail
topleft
topright
bottomleft
bottomright
left
right
data(diamonds) head(diamonds) # displays all columns corner(diamonds) # displays first 5 rows and only the first 5 columns corner(diamonds, corner="bottomleft") # displays the last 5 rows and the first 5 columns corner(diamonds, corner="topright") # displays the first 5 rows and the last 5 columns
data(diamonds) head(diamonds) # displays all columns corner(diamonds) # displays first 5 rows and only the first 5 columns corner(diamonds, corner="bottomleft") # displays the last 5 rows and the first 5 columns corner(diamonds, corner="topright") # displays the first 5 rows and the last 5 columns
Checks if strings are all upper or all lower case
find.case(string, case = c("upper", "lower", "mixed", "numeric"))
find.case(string, case = c("upper", "lower", "mixed", "numeric"))
string |
Character vector of strings to check cases |
case |
Whether checking for upper or lower case |
Checks if strings are all upper or all lower case. If string is all numbers it returns TRUE.
A vector of TRUE AND FALSE
Jared P. Lander
upper.case lower.case numeric.case mixed.case
toCheck <- c('BIG', 'little', 'Mixed', 'BIG WITH SPACE', 'little with space', 'MIXED with SPACE') find.case(toCheck, 'upper') find.case(toCheck, 'lower')
toCheck <- c('BIG', 'little', 'Mixed', 'BIG WITH SPACE', 'little with space', 'MIXED with SPACE') find.case(toCheck, 'upper') find.case(toCheck, 'lower')
Given a numeric dataset this function fits a series of kmeans clusterings with increasing number of centers. k-means is compared to k+1-means using Hartigan's Number to determine if the k+1st cluster should be added.
FitKMeans( x, max.clusters = 12L, spectral = FALSE, nstart = 1L, iter.max = 10L, algorithm = c("Hartigan-Wong", "Lloyd", "Forgy", "MacQueen"), seed = NULL )
FitKMeans( x, max.clusters = 12L, spectral = FALSE, nstart = 1L, iter.max = 10L, algorithm = c("Hartigan-Wong", "Lloyd", "Forgy", "MacQueen"), seed = NULL )
x |
The data, numeric, either a matrix or data.frame |
max.clusters |
The maximum number of clusters that should be tried |
spectral |
logical; If the data being fit are eigenvectors for spectral clustering |
nstart |
The number of random starts for the kmeans algorithm to use |
iter.max |
Maximum number of tries before the kmeans algorithm gives up on conversion |
algorithm |
The desired algorithm to be used for kmeans. Options are c("Hartigan-Wong", "Lloyd", "Forgy", "MacQueen"). See |
seed |
If not null, the random seed will be reset before each application of the kmeans algorithm |
A consecutive series of kmeans is computed with increasing k (number of centers). Each result for k and k+1 are compared using Hartigan's Number. If the number is greater than 10, it is noted that having k+1 clusters is of value.
A data.frame consisting of columns, for the number of clusters, the Hartigan Number and whether that cluster should be added, based on Hartigan's Number.
Jared P. Lander www.jaredlander.com
http://www.stat.columbia.edu/~madigan/DM08/descriptive.ppt.pdf
data(iris) hartiganResults <- FitKMeans(iris[, -ncol(iris)]) PlotHartigan(hartiganResults)
data(iris) hartiganResults <- FitKMeans(iris[, -ncol(iris)]) PlotHartigan(hartiganResults)
Force matrix and arrays to data.frame
ForceDataFrame(data)
ForceDataFrame(data)
data |
matrix, data.frame, array, list, etc. . . |
This is a helper function for build.x and build.y to convert arrays and matrices–which are not accepted in model.frame–into data.frames
a data.frame of the data
Jared P. Lander
Fortify an acf/pacf object
## S3 method for class 'acf' fortify(model, data = NULL, ...)
## S3 method for class 'acf' fortify(model, data = NULL, ...)
model |
An |
data |
Not used. Just for consistency with the fortify method. |
... |
Other arguments |
Prepares acf (and pacf) objects for plotting with ggplot.
data.frame
for plotting with ggplot.
Jared P. Lander
fortify(acf(sunspot.year, plot=FALSE)) fortify(pacf(sunspot.year, plot=FALSE))
fortify(acf(sunspot.year, plot=FALSE)) fortify(pacf(sunspot.year, plot=FALSE))
Fortify a kmeans model with its data
## S3 method for class 'kmeans' fortify(model, data = NULL, ...)
## S3 method for class 'kmeans' fortify(model, data = NULL, ...)
model |
|
data |
Data used to fit the model |
... |
Not Used |
Prepares a kmeans object to be plotted using cmdscale
to compute the projected x/y coordinates. If data
is not provided, then just the center points are calculated.
The original data with extra columns:
.x |
The projected x position. |
.y |
The projected y position. |
.Cluster |
The cluster that point belongs to. |
Jared P. Lander
kmeans fortify ggplot plot.kmeans
k1 <- kmeans(x=iris[, 1:4], centers=3) hold <- fortify(k1, data=iris) head(hold) hold2 <- fortify(k1) head(hold2)
k1 <- kmeans(x=iris[, 1:4], centers=3) hold <- fortify(k1, data=iris) head(hold) hold2 <- fortify(k1) head(hold2)
Fortify a ts object.
## S3 method for class 'ts' fortify(model, data = NULL, name = as.character(m[[2]]), ...)
## S3 method for class 'ts' fortify(model, data = NULL, name = as.character(m[[2]]), ...)
model |
A |
data |
A vector of the same length of |
name |
Character specifying the name of x if it is to be different that the variable being inputed. |
... |
Further arguments. |
Prepares a ts object for plotting with ggplot.
data.frame
for plotting with ggplot.
Jared P. Lander
fortify(sunspot.year)
fortify(sunspot.year)
Given a long matrix index convert to row and column positions
indexToPosition(x, nrow = 1)
indexToPosition(x, nrow = 1)
x |
Position of indices |
nrow |
The number of rows in the matrix |
Using which
on a matrix returns a number that iterates down rows then across columns. This function returns the (row, column) position of that index.
A Matrix with row and column columns and a row for each value of x
Jared P. Lander
indexToPosition(3, 2) indexToPosition(c(1, 4, 5, 7, 9), 3) indexToPosition(1:16, 4) indexToPosition(c(1, 3, 5, 6, 8, 10, 11, 13, 15), 5)
indexToPosition(3, 2) indexToPosition(c(1, 4, 5, 7, 9), 3) indexToPosition(1:16, 4) indexToPosition(c(1, 3, 5, 6, 8, 10, 11, 13, 15), 5)
Check which interval a number belongs to
interval.check(data, input = "Stop", times, fun = "<=")
interval.check(data, input = "Stop", times, fun = "<=")
data |
data.frame |
input |
character name of column we wish to compare |
times |
vector in ascending order where the differences between sequential elements are the intervals |
fun |
character containing comparator |
This function takes in a data.frame with a specified column and compares that to a vector of times
Vector indicating which element of times
that row belongs to. If the row is beyond any element NA is in it's spot.
Jared P. Lander
head(cars) interval.check(cars, input="speed", times=seq(min(cars$speed), max(cars$speed), length=10))
head(cars) interval.check(cars, input="speed", times=seq(min(cars$speed), max(cars$speed), length=10))
Display the left side of a rectangular data set
left(x, c = 5L, ...)
left(x, c = 5L, ...)
x |
The data |
c |
Number of columns to show |
... |
Arguments passed on to other functions |
Displays the left side of a rectangular data set.
This is a wrapper function for corner
... The left side of the data set that was requested. The size depends on c.
Jared P. Lander www.jaredlander.com
head
tail
corner
topright
bottomleft
bottomright
topleft
right
data(diamonds) head(diamonds) # displays all columns left(diamonds) # displays all rows and only the first 5 columns
data(diamonds) head(diamonds) # displays all columns left(diamonds) # displays all rows and only the first 5 columns
Checks if strings are all lower case
lower.case(string)
lower.case(string)
string |
Character vector of strings to check cases |
Checks if strings are all lower case. This is a wrapper for find.case('text', 'lower')
. If string is all numbers it returns TRUE.
A vector of TRUE AND FALSE
Jared P. Lander
find.case upper.case mixed.case numeric.case
toCheck <- c('BIG', 'little', 'Mixed', 'BIG WITH SPACE', 'little with space', 'MIXED with SPACE') lower.case(toCheck)
toCheck <- c('BIG', 'little', 'Mixed', 'BIG WITH SPACE', 'little with space', 'MIXED with SPACE') lower.case(toCheck)
Maps a range of numbers to a given interval
MapToInterval(nums, start = 1, stop = 10)
MapToInterval(nums, start = 1, stop = 10)
nums |
The vector of numbers to be mapped |
start |
The start of the interval |
stop |
The end of the interval |
formula: a + (x - min(x)) * (b - a) / (max(x) - min(x))
The original numbers mapped to the given interval
Jared P. Lander www.jaredlander.com
MapToInterval(1:10, start=0, stop=1) mapping(1:10, start=0, stop=1)
MapToInterval(1:10, start=0, stop=1) mapping(1:10, start=0, stop=1)
Checks if strings are all lower case
mixed.case(string)
mixed.case(string)
string |
Character vector of strings to check cases |
Checks if strings are a mix of upper and lower case. This is a wrapper for find.case('text', 'mixed')
. If string is all numbers it returns FALSE.
A vector of TRUE AND FALSE
Jared P. Lander
find.case all.upper
toCheck <- c('BIG', 'little', 'Mixed', 'BIG WITH SPACE', 'little with space', 'MIXED with SPACE') mixed.case(toCheck)
toCheck <- c('BIG', 'little', 'Mixed', 'BIG WITH SPACE', 'little with space', 'MIXED with SPACE') mixed.case(toCheck)
Rearranges column order by moving specified columns to the front or back.
moveToFront(data, cols) moveToBack(data, cols)
moveToFront(data, cols) moveToBack(data, cols)
data |
data.frame |
cols |
Character vector specifying the columns to be moved to the front or back |
Rearranges column order by moving specified columns to the front or back.
A data.frame with the columns in the right order
Jared P. Lander
theDF <- data.frame(A=1:10, B=11:20, C=1:10, D=11:20) moveToFront(theDF, c('B', 'C')) moveToFront(theDF, c('C', 'B')) moveToFront(theDF, c('C', 'C')) moveToBack(theDF, c('C', 'C')) moveToBack(theDF, c('C', 'B')) moveToBack(theDF, c('C', 'C'))
theDF <- data.frame(A=1:10, B=11:20, C=1:10, D=11:20) moveToFront(theDF, c('B', 'C')) moveToFront(theDF, c('C', 'B')) moveToFront(theDF, c('C', 'C')) moveToBack(theDF, c('C', 'C')) moveToBack(theDF, c('C', 'B')) moveToBack(theDF, c('C', 'C'))
Order of Magnitude Formatter
multiple( x, multiple = c("K", "M", "B", "T", "H", "k", "m", "b", "t", "h"), extra = scales::comma, digits = 0, ... )
multiple( x, multiple = c("K", "M", "B", "T", "H", "k", "m", "b", "t", "h"), extra = scales::comma, digits = 0, ... )
x |
Vector of numbers to be formatted. |
multiple |
The multiple to display numbers in. This symbol will be added to the end of the numbers. |
extra |
Function for perform any further formatting. |
digits |
Number of decimal places for rounding. This does not
necessarily affect the number digits displayed, for this, supply
|
... |
Extra arguments passed to |
This divides the number by the appropriate amount and adds on the corresponding symbol at the end of the number.
Character vector of formatted numbers.
Jared P. Lander
require(scales) vect <- c(1000, 1500, 23450, 21784, 875003780) multiple(vect) multiple(vect, extra=dollar) multiple(vect, extra=identity) require(ggplot2) data(diamonds) ggplot(diamonds, aes(x=x, y=y, color=price*100)) + geom_point() + scale_color_gradient2(labels=multiple)
require(scales) vect <- c(1000, 1500, 23450, 21784, 875003780) multiple(vect) multiple(vect, extra=dollar) multiple(vect, extra=identity) require(ggplot2) data(diamonds) ggplot(diamonds, aes(x=x, y=y, color=price*100)) + geom_point() + scale_color_gradient2(labels=multiple)
Multiple Style Formatting
multiple_format(...)
multiple_format(...)
... |
Arguments to be passed onto |
Since ggplot requires a function for formatting this allows the user to specify the function's arguments, which will return a function that can be used by ggplot.
The function multiple
.
Jared P. Lander
library(scales) vect <- c(1000, 1500, 23450, 21784, 875003780) multiple_format()(vect) multiple_format(extra=dollar)(vect) multiple_format(extra=identity)(vect) require(ggplot2) data(diamonds) ggplot(diamonds, aes(x=x, y=y, color=price*100)) + geom_point() + scale_color_gradient2(labels=multiple_format(extra=dollar))
library(scales) vect <- c(1000, 1500, 23450, 21784, 875003780) multiple_format()(vect) multiple_format(extra=dollar)(vect) multiple_format(extra=identity)(vect) require(ggplot2) data(diamonds) ggplot(diamonds, aes(x=x, y=y, color=price*100)) + geom_point() + scale_color_gradient2(labels=multiple_format(extra=dollar))
Order of Magnitude Formatter
multiple.comma(x, digits = 0, ...)
multiple.comma(x, digits = 0, ...)
x |
Vector of numbers to be formatted. |
digits |
Number of decimal places for rounding. |
... |
Further arguments to be passed on to |
Simply a wrapper for multiple that prespecifies the extra comma.
Character vector of comma formatted numbers.
Jared P. Lander
library(scales) vect <- c(1000, 1500, 23450, 21784, 875003780) multiple.comma(vect) multiple.comma(vect, multiple="k") multiple.comma(vect, multiple="h") library(ggplot2) data(diamonds) ggplot(diamonds, aes(x=x, y=y, color=price*100)) + geom_point() + scale_color_gradient2(labels=multiple.comma)
library(scales) vect <- c(1000, 1500, 23450, 21784, 875003780) multiple.comma(vect) multiple.comma(vect, multiple="k") multiple.comma(vect, multiple="h") library(ggplot2) data(diamonds) ggplot(diamonds, aes(x=x, y=y, color=price*100)) + geom_point() + scale_color_gradient2(labels=multiple.comma)
Order of Magnitude Formatter
multiple.dollar(x, ...)
multiple.dollar(x, ...)
x |
Vector of numbers to be formatted. |
... |
Further arguments to be passed on to |
Simply a wrapper for multiple that prespecifies the extra dollar.
Character vector of dollar formatted numbers.
Jared P. Lander
require(scales) vect <- c(1000, 1500, 23450, 21784, 875003780) multiple.dollar(vect) multiple.dollar(vect, multiple="k") multiple.dollar(vect, multiple="h") require(ggplot2) data(diamonds) ggplot(diamonds, aes(x=x, y=y, color=price*100)) + geom_point() + scale_color_gradient2(labels=multiple.dollar)
require(scales) vect <- c(1000, 1500, 23450, 21784, 875003780) multiple.dollar(vect) multiple.dollar(vect, multiple="k") multiple.dollar(vect, multiple="h") require(ggplot2) data(diamonds) ggplot(diamonds, aes(x=x, y=y, color=price*100)) + geom_point() + scale_color_gradient2(labels=multiple.dollar)
Order of Magnitude Formatter
multiple.identity(x, ...)
multiple.identity(x, ...)
x |
Vector of numbers to be formatted. |
... |
Further arguments to be passed on to |
Simply a wrapper for multiple that prespecifies the extra identity.
Character vector of formatted numbers.
Jared P. Lander
vect <- c(1000, 1500, 23450, 21784, 875003780) multiple.identity(vect) multiple.identity(vect, multiple="k") multiple.identity(vect, multiple="h") require(ggplot2) data(diamonds) ggplot(diamonds, aes(x=x, y=y, color=price*100)) + geom_point() + scale_color_gradient2(labels=multiple.identity)
vect <- c(1000, 1500, 23450, 21784, 875003780) multiple.identity(vect) multiple.identity(vect, multiple="k") multiple.identity(vect, multiple="h") require(ggplot2) data(diamonds) ggplot(diamonds, aes(x=x, y=y, color=price*100)) + geom_point() + scale_color_gradient2(labels=multiple.identity)
Checks if strings are all numbers or spaces
numeric.case(string)
numeric.case(string)
string |
Character vector of strings to check cases |
Checks if strings are all numbers and spaces. This is a wrapper for find.case('text', 'numeric')
.
A vector of TRUE AND FALSE
Jared P. Lander
find.case upper.case lower.case numeric.case
toCheck <- c('BIG', 'little', 'Mixed', 'BIG WITH SPACE', 'little with space', 'MIXED with SPACE', '17') numeric.case(toCheck)
toCheck <- c('BIG', 'little', 'Mixed', 'BIG WITH SPACE', 'little with space', 'MIXED with SPACE', '17') numeric.case(toCheck)
Plotting an ACF object
## S3 method for class 'acf' plot(x, ...)
## S3 method for class 'acf' plot(x, ...)
x |
An ACF object |
... |
Arguments passed on to autoplot |
This function has been deprecated in favor of autoplot
A ggplot2 object
Jared P. Lander
Plot the results from a k-means object
## S3 method for class 'kmeans' plot( x, data = NULL, class = NULL, size = 2, legend.position = c("right", "bottom", "left", "top", "none"), title = "K-Means Results", xlab = "Principal Component 1", ylab = "Principal Component 2", ... )
## S3 method for class 'kmeans' plot( x, data = NULL, class = NULL, size = 2, legend.position = c("right", "bottom", "left", "top", "none"), title = "K-Means Results", xlab = "Principal Component 1", ylab = "Principal Component 2", ... )
x |
A |
data |
The data used to kit the |
class |
Character name of the "true" classes of the data. |
size |
Numeric size of points |
legend.position |
Character indicating where the legend should be placed. |
title |
Title for the plot. |
xlab |
Label for the x-axis. |
ylab |
Label for the y-axis. |
... |
Not Used. |
Plots the results of k-means with color-coding for the cluster membership. If data
is not provided, then just the center points are calculated.
A ggplot object
Jared P. Lander
kmeans fortify ggplot plot.kmeans
k1 <- kmeans(x=iris[, 1:4], centers=3) plot(k1) plot(k1, data=iris)
k1 <- kmeans(x=iris[, 1:4], centers=3) plot(k1) plot(k1, data=iris)
After fitting a series of Hartigan's Numbers (see FitKMeans
) this will plot the results so it is easy to visualize
PlotHartigan( hartigan, title = "Hartigan's Rule", smooth = FALSE, linecolor = "grey", linetype = 2L, linesize = 1L, minor = TRUE )
PlotHartigan( hartigan, title = "Hartigan's Rule", smooth = FALSE, linecolor = "grey", linetype = 2L, linesize = 1L, minor = TRUE )
hartigan |
The results from
|
title |
Title to be used in the plot |
smooth |
logical; if true a smoothed line will be fit to the points, otherwise it will be a piecewise line |
linecolor |
Color of the horizontal line denoting 10 |
linetype |
Type of the horizontal line denoting 10 |
linesize |
Size of the horizontal line denoting 10 |
minor |
logical; if true minor grid lines will be plotted |
Displays a graphical representation of the results of FitKMeans
a ggplot object
Jared P. Lander www.jaredlander.com
#' http://www.stat.columbia.edu/~madigan/DM08/descriptive.ppt.pdf
data(iris) hartiganResults <- FitKMeans(iris[, -ncol(iris)]) PlotHartigan(hartiganResults)
data(iris) hartiganResults <- FitKMeans(iris[, -ncol(iris)]) PlotHartigan(hartiganResults)
Plot ts object
plotTimesSeries( x, time = NULL, acf = FALSE, lag.max = NULL, na.action = na.fail, demean = TRUE, title = sprintf("%s Plot", name), xlab = "Time", ylab = name, ... )
plotTimesSeries( x, time = NULL, acf = FALSE, lag.max = NULL, na.action = na.fail, demean = TRUE, title = sprintf("%s Plot", name), xlab = "Time", ylab = name, ... )
x |
a |
time |
A vector of the same length of |
acf |
Logical indicating if the acf and pacf should be plotted. |
lag.max |
maximum lag at which to calculate the acf. Default is 10*log10(N/m) where N is the number of observations and m the number of series. Will be automatically limited to one less than the number of observations in the series. |
na.action |
function to be called to handle missing values. na.pass can be used. |
demean |
logical. Should the covariances be about the sample means? |
title |
Graph title. |
xlab |
X-axis label. |
ylab |
Y-axis label. |
... |
Further arguments. |
Plot a ts object and, if desired, it's acf and pacf.
A ggplot object if acf
is FALSE
, otherwise TRUE
indicating success.
Jared P. Lander
ts.plotter plot.acf fortify.ts
plot(sunspot.year) plot(sunspot.year, acf=TRUE)
plot(sunspot.year) plot(sunspot.year, acf=TRUE)
Converts polar coordinates to cartesian coordinates
pol2cart(r, theta, degrees = FALSE)
pol2cart(r, theta, degrees = FALSE)
r |
The radius of the point |
theta |
The angle of the point, in radians |
degrees |
Logical indicating if theta is specified in degrees |
Converts polar coordinates to cartesian coordinates using a simple conversion. The angle, theta
must be in radians.
Somewhat inspired by http://www.r-bloggers.com/convert-polar-coordinates-to-cartesian/ and https://www.mathsisfun.com/polar-cartesian-coordinates.html
A data.frame holding the (x,y) coordinates and original polar coordinates
Jared P. Lander
polarRadPosTop <- data.frame(r=c(3, 5, 3, 5, 4, 6, 4, 6, 2), theta=c(0, pi/6, pi/4, pi/3, pi/2, 2*pi/3, 3*pi/4, 5*pi/6, pi)) polarRadPosBottom <- data.frame(r=c(3, 5, 3, 5, 4, 6, 4, 6, 2), theta=c(pi, 7*pi/6, 5*pi/4, 4*pi/3, 3*pi/2, 5*pi/3, 7*pi/4, 9*pi/6, 2*pi)) polarRadNegTop <- data.frame(r=c(3, 5, 3, 5, 4, 6, 4, 6, 2), theta=-1*c(0, pi/6, pi/4, pi/3, pi/2, 2*pi/3, 3*pi/4, 5*pi/6, pi)) polarRadNegBottom <- data.frame(r=c(3, 5, 3, 5, 4, 6, 4, 6, 2), theta=-1*c(pi, 7*pi/6, 5*pi/4, 4*pi/3, 3*pi/2, 5*pi/3, 7*pi/4, 9*pi/6, 2*pi)) pol2cart(polarRadPosTop$r, polarRadPosTop$theta) pol2cart(polarRadPosBottom$r, polarRadPosBottom$theta) pol2cart(polarRadNegTop$r, polarRadNegTop$theta) pol2cart(polarRadNegBottom$r, polarRadNegBottom$theta)
polarRadPosTop <- data.frame(r=c(3, 5, 3, 5, 4, 6, 4, 6, 2), theta=c(0, pi/6, pi/4, pi/3, pi/2, 2*pi/3, 3*pi/4, 5*pi/6, pi)) polarRadPosBottom <- data.frame(r=c(3, 5, 3, 5, 4, 6, 4, 6, 2), theta=c(pi, 7*pi/6, 5*pi/4, 4*pi/3, 3*pi/2, 5*pi/3, 7*pi/4, 9*pi/6, 2*pi)) polarRadNegTop <- data.frame(r=c(3, 5, 3, 5, 4, 6, 4, 6, 2), theta=-1*c(0, pi/6, pi/4, pi/3, pi/2, 2*pi/3, 3*pi/4, 5*pi/6, pi)) polarRadNegBottom <- data.frame(r=c(3, 5, 3, 5, 4, 6, 4, 6, 2), theta=-1*c(pi, 7*pi/6, 5*pi/4, 4*pi/3, 3*pi/2, 5*pi/3, 7*pi/4, 9*pi/6, 2*pi)) pol2cart(polarRadPosTop$r, polarRadPosTop$theta) pol2cart(polarRadPosBottom$r, polarRadPosBottom$theta) pol2cart(polarRadNegTop$r, polarRadNegTop$theta) pol2cart(polarRadNegBottom$r, polarRadNegBottom$theta)
Given row and column positions calculate the index.
positionToIndex(row, col, nrow = max(row))
positionToIndex(row, col, nrow = max(row))
row |
Vector specifying row positions |
col |
Vector specifying column positions |
nrow |
The number of rows in the matrix |
With row and column positions this computes the index, starting at (1,1) working down rows then across columns.
A vector of indices
Jared P. Lander
positionToIndex(1, 2, 2) positionToIndex(row=c(1, 1, 2, 1, 3), col=c(1, 2, 2, 3, 3), nrow=3) positionToIndex(rep(1:4, 4), rep(1:4, each=4), nrow=4) positionToIndex(rep(c(1, 3, 5), 3), rep(1:3, each=3), nrow=5)
positionToIndex(1, 2, 2) positionToIndex(row=c(1, 1, 2, 1, 3), col=c(1, 2, 2, 3, 3), nrow=3) positionToIndex(rep(1:4, 4), rep(1:4, each=4), nrow=4) positionToIndex(rep(c(1, 3, 5), 3), rep(1:3, each=3), nrow=5)
Adds a class to an x.
reclass(x, value) reclass(x) <- value
reclass(x, value) reclass(x) <- value
x |
The x getting the new class |
value |
The new class |
Adds a class to an x by putting the new class at the front of the vector of classes for the x.
The original x with the class containing value
in addition to the previous class(es)
Jared P. Lander
theDF <- data.frame(A=1:10, B=1:10) reclass(theDF) <- 'newclass' class(theDF) theDF <- reclass(theDF, 'another') class(theDF)
theDF <- data.frame(A=1:10, B=1:10) reclass(theDF) <- 'newclass' class(theDF) theDF <- reclass(theDF, 'another') class(theDF)
Display the right side of a rectangular data set
right(x, c = 5L, ...)
right(x, c = 5L, ...)
x |
The data |
c |
Number of columns to show |
... |
Arguments passed on to other functions |
Displays the right side of a rectangular data set.
This is a wrapper function for corner
... The left side of the data set that was requested. The size depends on c.
Jared P. Lander www.jaredlander.com
head
tail
corner
topright
bottomleft
bottomright
topleft
topleft
data(diamonds) head(diamonds) # displays all columns right(diamonds) # displays all rows and only the last 5 columns
data(diamonds) head(diamonds) # displays all columns right(diamonds) # displays all rows and only the last 5 columns
Shift a column of data
shift.column( data, columns, newNames = sprintf("%s.Shifted", columns), len = 1L, up = TRUE )
shift.column( data, columns, newNames = sprintf("%s.Shifted", columns), len = 1L, up = TRUE )
data |
|
columns |
Character vector specifying which columns to shift. |
newNames |
Character vector specifying new names for the columns that will be created by the shift. Must be same length as |
len |
Integer specifying how many rows to shift the data. |
up |
logical indicating if rows should be shifted up or down. |
Shifts a column of data up or down a certain number of rows
data.frame
with the specified columns shifted.
Jared P. Lander
myData <- data.frame(Upper=LETTERS, lower=letters) shift.column(data=myData, columns="lower") shift.column(data=myData, columns="lower", len=2)
myData <- data.frame(Upper=LETTERS, lower=letters) shift.column(data=myData, columns="lower") shift.column(data=myData, columns="lower", len=2)
Generic function for simple imputation.
simple.impute(x, fun = median, ...)
simple.impute(x, fun = median, ...)
x |
An object to be imputed |
fun |
The function with which to fill in missing values |
... |
Further arguments |
Provides the ability to simply impute data based on a simple measure such as mean or median. For more robust imputation see the packages Amelia, mice or mi.
An object with the missing values imputed.
Jared P. Lander
theDF <- data.frame(A=1:10, B=1:10, C=1:10) theDF[c(1, 4, 6), c(1)] <- NA theDF[c(3, 4, 8), c(3)] <- NA simple.impute(theDF$A) simple.impute(theDF$A, mean) simple.impute(theDF$A, constant(4)) simple.impute(theDF) simple.impute(theDF, mean) simple.impute(theDF, constant(4))
theDF <- data.frame(A=1:10, B=1:10, C=1:10) theDF[c(1, 4, 6), c(1)] <- NA theDF[c(3, 4, 8), c(3)] <- NA simple.impute(theDF$A) simple.impute(theDF$A, mean) simple.impute(theDF$A, constant(4)) simple.impute(theDF) simple.impute(theDF, mean) simple.impute(theDF, constant(4))
Function for imputing a data.frame with missing data.
## S3 method for class 'data.frame' simple.impute(x, fun = stats::median, ...)
## S3 method for class 'data.frame' simple.impute(x, fun = stats::median, ...)
x |
A data.frame |
fun |
The function with which to fill in missing values |
... |
Further arguments |
Provides the ability to simply impute data based on a simple measure such as mean or median. For more robust imputation see the packages Amelia, mice or mi.
Each column is imputed independently.
A data.frame with the missing values imputed.
Jared P. Lander
theDF <- data.frame(A=1:10, B=1:10, C=1:10) theDF[c(1, 4, 6), c(1)] <- NA theDF[c(3, 4, 8), c(3)] <- NA simple.impute.data.frame(theDF) simple.impute.data.frame(theDF, mean) simple.impute.data.frame(theDF, constant(4))
theDF <- data.frame(A=1:10, B=1:10, C=1:10) theDF[c(1, 4, 6), c(1)] <- NA theDF[c(3, 4, 8), c(3)] <- NA simple.impute.data.frame(theDF) simple.impute.data.frame(theDF, mean) simple.impute.data.frame(theDF, constant(4))
Function for imputing a vector with missing data.
## Default S3 method: simple.impute(x, fun = median, ...)
## Default S3 method: simple.impute(x, fun = median, ...)
x |
A numeric or integer vector |
fun |
The function with which to fill in missing values |
... |
Further arguments |
Provides the ability to simply impute data based on a simple measure such as mean or median. For more robust imputation see the packages Amelia, mice or mi.
An object with the missing values imputed.
Jared P. Lander
theDF <- data.frame(A=1:10, B=1:10, C=1:10) theDF[c(1, 4, 6), c(1)] <- NA theDF[c(3, 4, 8), c(3)] <- NA simple.impute.default(theDF$A) simple.impute.default(theDF$A, mean) simple.impute.default(theDF$A, constant(4))
theDF <- data.frame(A=1:10, B=1:10, C=1:10) theDF[c(1, 4, 6), c(1)] <- NA theDF[c(3, 4, 8), c(3)] <- NA simple.impute.default(theDF$A) simple.impute.default(theDF$A, mean) simple.impute.default(theDF$A, constant(4))
Function for imputing a tbl_df with missing data.
## S3 method for class 'tbl_df' simple.impute(x, fun = median, ...)
## S3 method for class 'tbl_df' simple.impute(x, fun = median, ...)
x |
A data.frame |
fun |
The function with which to fill in missing values |
... |
Further arguments |
Provides the ability to simply impute data based on a simple measure such as mean or median. For more robust imputation see the packages Amelia, mice or mi.
Each column is imputed independently.
A data.frame with the missing values imputed.
Jared P. Lander
theDF <- data.frame(A=1:10, B=1:10, C=1:10) theDF[c(1, 4, 6), c(1)] <- NA theDF[c(3, 4, 8), c(3)] <- NA simple.impute.data.frame(theDF) simple.impute.data.frame(theDF, mean) simple.impute.data.frame(theDF, constant(4))
theDF <- data.frame(A=1:10, B=1:10, C=1:10) theDF[c(1, 4, 6), c(1)] <- NA theDF[c(3, 4, 8), c(3)] <- NA simple.impute.data.frame(theDF) simple.impute.data.frame(theDF, mean) simple.impute.data.frame(theDF, constant(4))
Converts each of the special characters to their escaped equivalents in each element of a single vector.
subOut(toAlter, specialChars = c("!", "(", ")", "-", "=", "*", "."))
subOut(toAlter, specialChars = c("!", "(", ")", "-", "=", "*", "."))
toAlter |
Character vector that will be altered by subbing the special characters with their escaped equivalents |
specialChars |
The characters to be subbed out |
Each element in the specialChar vector is subbed for its escaped equivalent in each of the elements of toAlter
toAlter is returned with any of the defined specialChars subbed out for their escaped equivalents
Jared P. Lander www.jaredlander.com
subOut(c("Hello", "(parens)", "Excited! Mark")) subOut(c("Hello", "(parens)", "Excited! Mark"), specialChars=c("!", "("))
subOut(c("Hello", "(parens)", "Excited! Mark")) subOut(c("Hello", "(parens)", "Excited! Mark"), specialChars=c("!", "("))
Converts each of the special characters to their escaped equivalents in each element of each vector.
subSpecials(..., specialChars = c("!", "(", ")", "-", "=", "*", "."))
subSpecials(..., specialChars = c("!", "(", ")", "-", "=", "*", "."))
... |
Character vectors that will be altered by subbing the special characters with their escaped equivalents |
specialChars |
The characters to be subbed out |
Each element in the specialChar vector is subbed for its escaped equivalent in each of the elements of each vector passed in
The provided vectors are returned with any of the defined specialChars subbed out for their escaped equivalents. Each vector is returned as an element of a list.
Jared P. Lander www.jaredlander.com
subSpecials(c("Hello", "(parens)", "Excited! Mark")) subSpecials(c("Hello", "(parens)", "Excited! Mark"), specialChars=c("!", "(")) subSpecials(c("Hello", "(parens)", "Excited! Mark"), c("This is a period. And this is an asterisk *"), specialChars=c("!", "(")) subSpecials(c("Hello", "(parens)", "Excited! Mark"), c("This is a period. And this is an asterisk *"), specialChars=c("!", "(", "*"))
subSpecials(c("Hello", "(parens)", "Excited! Mark")) subSpecials(c("Hello", "(parens)", "Excited! Mark"), specialChars=c("!", "(")) subSpecials(c("Hello", "(parens)", "Excited! Mark"), c("This is a period. And this is an asterisk *"), specialChars=c("!", "(")) subSpecials(c("Hello", "(parens)", "Excited! Mark"), c("This is a period. And this is an asterisk *"), specialChars=c("!", "(", "*"))
Substitutes multiple patterns and corresponding replacements
subVector(x, toSub) subMultiple(x, pattern, replacement)
subVector(x, toSub) subMultiple(x, pattern, replacement)
x |
Vector of text to search |
toSub |
Named vector where the elements are the pattern and the names are the replacement values |
pattern |
Vector of patterns to find in each element of x |
replacement |
Vector of replacement values corresponding to each value of pattern |
Given a vector of text replaces all patterns each each element
The text in x with substitutions made
Jared P. Lander
theText <- c('Hi Bob & Cooper how is life today', 'Anything happening now?', 'Sally & Dave are playing with Jess & Julio | with their kids') subVector(theText, toSub=c("and"='&', 'or'='\\|')) subVector(theText) theText <- c('Hi Bob & Cooper how is life today', 'Anything happening now?', 'Sally & Dave are playing with Jess & Julio | with their kids') subMultiple(theText, pattern=c('&', '\\|'), replacement=c('and', 'or'))
theText <- c('Hi Bob & Cooper how is life today', 'Anything happening now?', 'Sally & Dave are playing with Jess & Julio | with their kids') subVector(theText, toSub=c("and"='&', 'or'='\\|')) subVector(theText) theText <- c('Hi Bob & Cooper how is life today', 'Anything happening now?', 'Sally & Dave are playing with Jess & Julio | with their kids') subMultiple(theText, pattern=c('&', '\\|'), replacement=c('and', 'or'))
Convenience function that takes in a time object and calculates a difference with a user specified prompt
timeSingle( string = "Time difference", startTime, endTime = Sys.time(), sep = ":" )
timeSingle( string = "Time difference", startTime, endTime = Sys.time(), sep = ":" )
string |
string of what was timed |
startTime |
"POSIXct" "POSIXt" object, usually from |
endTime |
"POSIXct" "POSIXt" object, usually from |
sep |
string, usually character that is used as the separator between user prompt and time difference |
prompt_string string user prompt with time difference
Daniel Y. Chen
x <- 3.14 strt <- Sys.time() sq <- x ** 2 timeSingle('Squaring value', strt)
x <- 3.14 strt <- Sys.time() sq <- x ** 2 timeSingle('Squaring value', strt)
Display the top left corner of a rectangular data set
topleft(x, r = 5L, c = 5L, ...)
topleft(x, r = 5L, c = 5L, ...)
x |
The data |
r |
Number of rows to display |
c |
Number of columns to show |
... |
Arguments passed on to other functions |
Displays the top left corner of a rectangular data set.
This is a wrapper function for corner
... The top left corner of the data set that was requested. The size depends on r and c.
Jared P. Lander www.jaredlander.com
head
tail
corner
topright
bottomleft
bottomright
left
right
data(diamonds) head(diamonds) # displays all columns topleft(diamonds) # displays first 5 rows and only the first 5 columns
data(diamonds) head(diamonds) # displays all columns topleft(diamonds) # displays first 5 rows and only the first 5 columns
Display the top right corner of a rectangular data set
topright(x, r = 5L, c = 5L, ...)
topright(x, r = 5L, c = 5L, ...)
x |
The data |
r |
Number of rows to display |
c |
Number of columns to show |
... |
Arguments passed on to other functions |
Displays the top right corner of a rectangular data set.
This is a wrapper function for corner
... The top right corner of the data set that was requested. The size depends on r and c.
Jared P. Lander www.jaredlander.com
head
tail
corner
topleft
bottomleft
bottomright
left
right
data(diamonds) head(diamonds) # displays all columns topright(diamonds) # displays first 5 rows and only the last 5 columns
data(diamonds) head(diamonds) # displays all columns topright(diamonds) # displays first 5 rows and only the last 5 columns
Plot a ts object
ts.plotter( data, time = NULL, title = "Series Plot", xlab = "Time", ylab = "Rate" )
ts.plotter( data, time = NULL, title = "Series Plot", xlab = "Time", ylab = "Rate" )
data |
A |
time |
A vector of the same length of |
title |
Title of plot. |
xlab |
X-axis label. |
ylab |
Y-axis label. |
Fortifies, then plots a ts
object.
A ggplot object
Jared P. Lander
ts.plotter(sunspot.year)
ts.plotter(sunspot.year)
Find unique rows of a data.frame regardless of the order they appear
uniqueBidirection(x)
uniqueBidirection(x)
x |
a data.frame |
Sorts individual rows to get uniques regardless of order of appearance.
A data.frame that is unique regardless of direction
Jared P. Lander
ex <- data.frame(One=c('a', 'c', 'a', 'd', 'd', 'c', 'b'), Two=c('b', 'd', 'b', 'e', 'c', 'd', 'a'), stringsAsFactors=FALSE) # make a bigger version exBig <- ex for(i in 1:1000) { exBig <- rbind(exBig, ex) } dim(exBig) uniqueBidirection(ex) uniqueBidirection(exBig) ex3 <- dplyr::bind_cols(ex, tibble::tibble(Three=rep('a', nrow(ex)))) uniqueBidirection(ex3)
ex <- data.frame(One=c('a', 'c', 'a', 'd', 'd', 'c', 'b'), Two=c('b', 'd', 'b', 'e', 'c', 'd', 'a'), stringsAsFactors=FALSE) # make a bigger version exBig <- ex for(i in 1:1000) { exBig <- rbind(exBig, ex) } dim(exBig) uniqueBidirection(ex) uniqueBidirection(exBig) ex3 <- dplyr::bind_cols(ex, tibble::tibble(Three=rep('a', nrow(ex)))) uniqueBidirection(ex3)
Checks if strings are all upper case
upper.case(string)
upper.case(string)
string |
Character vector of strings to check cases |
Checks if strings are all upper case. This is a wrapper for find.case('text', 'upper')
. If string is all numbers it returns TRUE.
A vector of TRUE AND FALSE
Jared P. Lander
find.case lower.case mixed.case numeric.case
toCheck <- c('BIG', 'little', 'Mixed', 'BIG WITH SPACE', 'little with space', 'MIXED with SPACE') upper.case(toCheck)
toCheck <- c('BIG', 'little', 'Mixed', 'BIG WITH SPACE', 'little with space', 'MIXED with SPACE') upper.case(toCheck)
Viewport
vplayout(x, y)
vplayout(x, y)
x |
The x cell of the viewport to push into. |
y |
The y cell of the viewport to push into. |
Creates viewport for pushing ggplot objects to parts of a console.
An R object of class viewport.
Jared P. Lander
library(ggplot2) library(grid)
library(ggplot2) library(grid)
Function to build the right row selection depending on the desired corner.
WhichCorner( corner = c("topleft", "bottomleft", "topright", "bottomright"), r = 5L, c = 5L, object = "x" )
WhichCorner( corner = c("topleft", "bottomleft", "topright", "bottomright"), r = 5L, c = 5L, object = "x" )
corner |
(character) which corner to display c("topleft", "bottomleft", "topright", "bottomright") |
r |
(numeric) the number of rows to show |
c |
(numeric) the number of columns to show |
object |
The name of the object that is being subsetted |
Function to build the right row selection depending on the desired corner. Helper function for getting the indexing for data.frame's, matrices
An expression that is evaluated to return the proper portion of the data
Jared P. Lander
## Not run: WhichCorner('topleft') WhichCorner('bottomleft') WhichCorner('topright') WhichCorner('bottomright') WhichCorner('topleft', r=6) WhichCorner('bottomleft', r=6) WhichCorner('topright', r=6) WhichCorner('bottomright', r=6) WhichCorner('topleft', c=7) WhichCorner('bottomleft', c=7) WhichCorner('topright', c=7) WhichCorner('bottomright', c=7) WhichCorner('topleft', r=8, c=3) WhichCorner('bottomleft', r=8, c=3) WhichCorner('topright', r=8, c=3) WhichCorner('bottomright', r=8, c=3) ## End(Not run)
## Not run: WhichCorner('topleft') WhichCorner('bottomleft') WhichCorner('topright') WhichCorner('bottomright') WhichCorner('topleft', r=6) WhichCorner('bottomleft', r=6) WhichCorner('topright', r=6) WhichCorner('bottomright', r=6) WhichCorner('topleft', c=7) WhichCorner('bottomleft', c=7) WhichCorner('topright', c=7) WhichCorner('bottomright', c=7) WhichCorner('topleft', r=8, c=3) WhichCorner('bottomleft', r=8, c=3) WhichCorner('topright', r=8, c=3) WhichCorner('bottomright', r=8, c=3) ## End(Not run)