Title: | Miscellaneous Tools |
---|---|
Description: | Functions for performing quick observations or evaluations of data, including a variety of ways to list objects by size, class, etc. The functions 'seqle' and 'reverse.seqle' mimic the base 'rle' but can search for linear sequences. The function 'splatnd' allows the user to generate zero-argument commands without the need for 'makeActiveBinding' . Functions provided to convert from any base to any other base, and to find the n-th greatest max or n-th least min. In addition, functions which mimic Unix shell commands, including 'head', 'tail' ,'pushd' ,and 'popd'. Various other goodies included as well. |
Authors: | Carl Witthoft |
Maintainer: | Carl Witthoft <[email protected]> |
License: | LGPL-3 |
Version: | 4.1 |
Built: | 2024-12-13 06:30:28 UTC |
Source: | CRAN |
Most of these tools are small functions to do simple tasks or provide filtered views of the current environment. In addition the function splatnd
is provided primarily as a piece of example code to show how to write zero-argument operators. It's based on the code in the package sos
, and avoids the need to use makeActiveBinding
(as in ,e.g., pracma::ans
)
Carl Witthoft, with attributions as noted in the individual help pages
Maintainer:Carl Witthoft [email protected]
This function compares two vectors (or arrays) of values and returns the near-equality status of corresponding elements. As with all.equal()
, the intent is primarily to get around machine limits of representation of floating-point numbers. For integer comparison, just use the base ==
operator.
approxeq(x, y, tolerance = .Machine$double.eps^0.5, ...)
approxeq(x, y, tolerance = .Machine$double.eps^0.5, ...)
x , y
|
The two input items, typically vectors or arrays of data. |
tolerance |
Set the precision to which |
... |
Not used at this time. |
If x
and y
are of different lengths, the shorter one is recycled and a warning issued.
A vector of the same length as the longer of x
or y
, consisting of TRUE
and FALSE
elements, depending on whether the corresponding elements of x
and y
are within the approximate equality precision desired.
Carl Witthoft, [email protected]
all.equal
, Comparison
, identical
x<-1:10 y<-x+runif(10)*1e-6 approxeq(x,y) #all FALSE approxeq(x,y,tolerance=1e-5) #mostly TRUE, probably
x<-1:10 y<-x+runif(10)*1e-6 approxeq(x,y) #all FALSE approxeq(x,y,tolerance=1e-5) #mostly TRUE, probably
This data set contains the "banner" arrays of characters to be used by the function banvax
.
ascarr
ascarr
The array must be of dimension [6,8,N] for N characters. The dimnames
for the third dimension must be the character matching each such layer.
This function was originally written to do the same as the unix rm -i
command. The user supplies a list of items and the name of a function which is optionally applied to each item in turn.
askrm(items = ls(parent.frame()), fn = "rm", ask = TRUE)
askrm(items = ls(parent.frame()), fn = "rm", ask = TRUE)
items |
A character vector of names of the objects to be acted upon (such as |
fn |
The name of the function to be applied, supplied as a character string. Possible future upgrades may allow function names to be entered without quotes. |
ask |
If |
A list with three elements.
func |
Echo back the input function, for archival reference. |
selected |
All the items from the input list to which the function |
evout |
A list of the value(s) returned by the function, if any, each time it was executed. |
Carl Witthoft, [email protected]
When interactive prompting is not desired, sapply
or its brethren are recommended.
# get rid of junky objects left around from testing foo<-1 afoo<-c(foo,2) foob <- c('a','b','d') askrm(ls(pattern="foo") ) x<- rep(1,10) y<- runif(10) askrm(c('x','y'),'sd',ask=FALSE)
# get rid of junky objects left around from testing foo<-1 afoo<-c(foo,2) foob <- c('a','b','d') askrm(ls(pattern="foo") ) x<- rep(1,10) y<- runif(10) askrm(c('x','y'),'sd',ask=FALSE)
This function takes a character string and writes the same characters as large block-letters into a text file.
banvax(msg, file = 'banner.txt', linewid = 80, bandat = cgwtools::ascarr)
banvax(msg, file = 'banner.txt', linewid = 80, bandat = cgwtools::ascarr)
msg |
A character string. See the Details section for the behavior for nonmatching characters. |
file |
A character string identifying an output file, or a |
linewid |
Defines the max number of characters per line. Change from the default value of 80 depending on the display or paper size in use. |
bandat |
The data array to use for the banner elements. In general, must be a 3-dimensional array with dimensions [x,y,N] where each n-th layer contains the banner form of one character. See the Details section. |
The supplied data file ascarr
contains all letters and numerals and a bunch of punctuation marks such as " ;" , "," "\" , etc. If a character in the input is not found in the file, a "box" is used in its place. If a user-supplied file is specified, note that each layer of the [x,y,N] array must have a name equal to the character to be invoked. Here x
specifies the number of columns and y
the number of rows in each banner-element.
The function cat
generates the output, so setting the argument file
to "" will direct the output to the console. Quoting from the help page for cat
, ' If "" (the default), cat prints to the standard output connection, the console unless redirected by sink
. If it is "|cmd", the output is piped to the command given by cmd , by opening a pipe connection. '
Nothing is returned from the function. The output is a file or whatever connection
is specified by the file
argument.
Author and Maintainer:Carl Witthoft [email protected]
https://www0.mi.infn.it/~calcolo/OpenVMS/ssb71/6015/6017p041.htm#index_x_2757
This function now resides in the package base2base
This is a Q&D way to create Pareto / histogram bins of a dataset when you want a separate bin for each value and don't want to deal with the 'breaks' or equivalent arguments in hist
or other histogram functions in R packages.
binit(samps,roundPrec=NULL)
binit(samps,roundPrec=NULL)
samps |
A vector or array of data to be binned. |
roundPrec |
The number of digits to round |
binit
sorts the input data and feeds the result to rle
. This effectively produces histogram-like results. If you want a strict Pareto order (most common first), just sort the list elements lengths
and values
by the magnitudes in lengths
.
A list containing two elements lengths: the number of items in each bin values: the data value associated with each bin
Carl Witthoft, [email protected]
x <- sample(1:100, 1000, rep=TRUE) xbin <- binit(x) plot(xbin$values,xbin$lengths, type = 'h') # without rounding, will just be grass x <- rnorm(1000) xbin <- binit(x,2) plot(xbin$values,xbin$lengths, type = 'h')
x <- sample(1:100, 1000, rep=TRUE) xbin <- binit(x) plot(xbin$values,xbin$lengths, type = 'h') # without rounding, will just be grass x <- rnorm(1000) xbin <- binit(x,2) plot(xbin$values,xbin$lengths, type = 'h')
Calculates the cumulative value of almost any function in the same manner as cumsum
cumfun(frist, FUN = sum, ...)
cumfun(frist, FUN = sum, ...)
frist |
A vector of numerical or string values |
FUN |
The function to use. Can be either the function name (as is usually done) or a single character string naming the function. Default is |
... |
Additional arguments, if any, required for the chosen FUN function. See Details. |
If additional arguments are of length one, they are applied to each value in frist
. If they are longer but not equal in length to length(frist)
they will either be truncated or recycled to match.
The builtin functions cumsum
and cumprod
generally are much faster than applying sum
or prod
to this function.
A list of the cumulative calculations generated.
Carl Witthoft, [email protected]
foo <- 'abcdefcghicklmno' grepfoo <-function(x,y) grep(y,x) cumfoo <- cumfun(unlist(strsplit(foo,'')), FUN=grepfoo, y = 'c') bar <- rnorm(1000,1) cumsd <- cumfun(bar,FUN=sd) plot(unlist(cumsd),type='l') # compare with input std dev lines(c(0,1000),c(1,1),lty=2,col='red')
foo <- 'abcdefcghicklmno' grepfoo <-function(x,y) grep(y,x) cumfoo <- cumfun(unlist(strsplit(foo,'')), FUN=grepfoo, y = 'c') bar <- rnorm(1000,1) cumsd <- cumfun(bar,FUN=sd) plot(unlist(cumsd),type='l') # compare with input std dev lines(c(0,1000),c(1,1),lty=2,col='red')
Simple overload to return dim
when it's sensible and length
otherwise
dim(item)
dim(item)
item |
The object whose dimensions are to be determined |
Either a single value as returned by length
or a vector of integers indicating the magnitude of each dimension as returned by dim
Carl Witthoft, [email protected]
x1<-1:10 x2<-matrix(1,3,4) dim(x1) dim(x2)
x1<-1:10 x2<-matrix(1,3,4) dim(x1) dim(x2)
For those times when you only want to know the local directories available, use this instead of struggling through myriad arguments to dir
. All arguments are the same as for plain old "dir" and are passed to dir
.
dirdir(path = ".", pattern = NULL, all.files = FALSE, full.names = FALSE, recursive = FALSE, ignore.case = FALSE, include.dirs = FALSE, no.. = FALSE)
dirdir(path = ".", pattern = NULL, all.files = FALSE, full.names = FALSE, recursive = FALSE, ignore.case = FALSE, include.dirs = FALSE, no.. = FALSE)
path |
a character vector of full path names; the default corresponds to the working directory, getwd(). Tilde expansion (see path.expand) is performed. Missing values will be ignored. Elements with a marked encoding will be converted to the native encoding (and if that fails, considered non-existent). |
pattern |
an optional regular expression. Only file names which match the regular expression will be returned. |
all.files |
a logical value. If FALSE, only the names of visible files are returned (following Unix-style visibility, that is files whose name does not start with a dot). If TRUE, all file names will be returned. |
full.names |
a logical value. If TRUE, the directory path is prepended to the file names to give a relative file path. If FALSE, the file names (rather than paths) are returned. |
recursive |
logical. Should the listing recurse into directories? |
ignore.case |
logical. Should pattern-matching be case-insensitive? |
include.dirs |
logical. Should subdirectory names be included in recursive listings? (They always are in non-recursive ones). |
no.. |
logical. Should both "." and ".." be excluded also from non-recursive listings? |
Note: this is directly quoted from the man page for dir
.
A character vector containing the names of the files in the specified directories (empty if there were no files). If a path does not exist or is not a directory or is unreadable it is skipped.
The files are sorted in alphabetical order, on the full path if full.names = TRUE.
Carl Witthoft, [email protected]
Finds the location of a specified sequence either of numbers or strings in the source data item. If desired, for numerical data, both the source and the named pattern can be rounded to specified number of digits.
findpat(datavec, pattern, roundit = NULL)
findpat(datavec, pattern, roundit = NULL)
datavec |
A vector of numerical or string values |
pattern |
A vector containing the sequence to search for |
roundit |
If not NULL, sets the precision for rounding numbers. Ignored if |
If datavec
and pattern
are of different types, R will automagically convert to a common type and then compare the values. This is a result of the coercion rules identified in the man page for Comparison
, "If the two arguments are atomic vectors of different types, one is coerced to the type of the other, the (decreasing) order of precedence being character, complex, numeric, integer, logical and raw."
Use of the roundit
argument is recommended whenever working with doubles (floats) to avoid the well-known and often overlooked pain of binary precision errors.
If the first element of pattern
isn't found, a message is posted and an empty integer vector is returned.
Otherwise, a vector of the indices of datavec where the desired pattern is located is returned. These are the indices of the start of the pattern.
Carl Witthoft, [email protected]
which
, nth_number_after_mth
, strfind
fooc <- letters[c(1:15,4:9,12:26)] findpat(fooc,c('d','e','f')) # 4 16 fooi <- c(1:50,5:9,60:80) findpat(fooi,6:8) # 6 52 findpat(fooi,c('6','7','8')) # also 6 52
fooc <- letters[c(1:15,4:9,12:26)] findpat(fooc,c('d','e','f')) # 4 16 fooi <- c(1:50,5:9,60:80) findpat(fooi,6:8) # 6 52 findpat(fooi,c('6','7','8')) # also 6 52
getstack
goes into the separate environment where pushd
and
popd
operate and returns the current stack of directories.
getstack()
getstack()
none
Allowing a function to modify an object in the GlobalEnvironment is frowned upon by CRAN (and most programmers), so to maintain a directory stack a separate environment is established by pushd
. Since this environment is not visible at the console level, getstack
allows the user to check on the current status of the stack.
The current directory stack is returned as a vector of strings.
Carl Witthoft [email protected]
## depends on your local directory structure and permissions getwd() getstack() #empty, probably pushd('..') getstack() pushd('.') getstack() popd() getstack() popd() getstack() getwd() #back where we started
## depends on your local directory structure and permissions getwd() getstack() #empty, probably pushd('..') getstack() pushd('.') getstack() popd() getstack() popd() getstack() getwd() #back where we started
seqle
As with inverse.rle
, this function reverses the compression performed with seqle
so long as you know the incr
value used to generate the compressed data.
inverse.seqle(x, incr = 1L)
inverse.seqle(x, incr = 1L)
x |
An object of class |
incr |
The increment between elements used to generate the compressed data object. Note that this can be either integer or float. For floating-point sequences, the reconstruction of the original series may differ at the level of floating-point precision used to generate the input object. |
a vector of values identical (or nearly so, for floats) to the original sequence.
Note: Since the concept of "increment" has no reliable meaning when dealing with characters or char strings, when x
is non-numeric the argument incr
is ignored and the function reverts to base::inverse.rle
....
The bulk of the code is taken directly from base::inverse.rle
. Thanks to "flodel" on StackOverflow for suggesting code to handle floating-point increments.
Carl Witthoft, [email protected]
x<- c(2,2,2,3:8,8,8,4,4,4,5,5.5,6) y<-seqle(x,incr=0) inverse.seqle(y,0) y <- seqle(x,incr=1) inverse.seqle(y) inverse.seqle(y,2) # not what you wanted
x<- c(2,2,2,3:8,8,8,4,4,4,5,5.5,6) y<-seqle(x,incr=0) inverse.seqle(y,0) y <- seqle(x,incr=1) inverse.seqle(y) inverse.seqle(y,2) # not what you wanted
class attribute
.
This is one of the author's collection of ls*
Q&D functions. Since anyone can define a new class at any time, there is no predefined set of legal or illegal class names. Remember that an object
can have multiple class
es. This function only allows searching for a single class name in a given call.
lsclass(type = "numeric")
lsclass(type = "numeric")
type |
The name of the class you're looking for. |
A vector of character strings containing the names of matching objects (as would be returned by the base function ls
) .
Carl Witthoft [email protected]
xyzzy<-structure(vector(),class='grue') lsclass('integer') lsclass('grue')
xyzzy<-structure(vector(),class='grue') lsclass('integer') lsclass('grue')
.Rdata
file.
This function opens an .Rdata
file, lists the contents, and cleans up after itself.
lsdata(fnam = ".Rdata")
lsdata(fnam = ".Rdata")
fnam |
the name of the datafile to be examined. |
The output of ls
applied to the objects loaded from the specified data file.
Carl Witthoft [email protected]
Various people have published similar code on Stack Overflow.
##not run because of complaints about detritus # xblue<-1 # yblue<-2 # save(xblue,yblue,file='blue.Rdata') # lsdata('blue.Rdata')
##not run because of complaints about detritus # xblue<-1 # yblue<-2 # save(xblue,yblue,file='blue.Rdata') # lsdata('blue.Rdata')
Just a toy to list the number of elements or optionally the bytesize as produced with object.size
of a specified selection of objects. I find it handy when I want to rid an environment of large (or empty) objects. In the default case, byte=FALSE
, lists and S4 objects are "taken apart" down to the lowest level so all individual elements are counted.
lssize(items, byte = FALSE)
lssize(items, byte = FALSE)
items |
A vector of character strings identifying the objects of interest as would be returned by, e.g. |
byte |
If TRUE, calculate the number of bytes taken up by an object. If FALSE, calculate the total number of elements of an object. |
A vector of the object sizes, with the object names as names for the elements
Carl Witthoft, [email protected]
Many thanks to Martin Morgan of bioconductor.org who provided the recursive function for deconstructing an S4 Object. See http://stackoverflow.com/questions/14803237/ for the original question and answer.
x1<-runif(100) x2<-runif(1000) x3<-runif(2000) lssize(ls(pattern='x[1-3]')) lssize(ls(pattern='x[1-3]'),byte=TRUE) #depending on what you have in your environment: lssize(lstype('integer'))
x1<-runif(100) x2<-runif(1000) x3<-runif(2000) lssize(ls(pattern='x[1-3]')) lssize(ls(pattern='x[1-3]'),byte=TRUE) #depending on what you have in your environment: lssize(lstype('integer'))
This is a Q&D tool to list all objects in the current environment of a specified type. As discussed in the base R documentation, these types are the vector types "logical", "integer", "double", "complex", "character", "raw" and "list", "NULL", "closure" (function), "special" and "builtin" (basic functions and operators), "environment", "S4" (some S4 objects).
lstype(type = "closure")
lstype(type = "closure")
type |
Any valid variable type, or "function," which is redirected to "closure." |
A vector of character strings as is returned by the base function ls
.
Carl Witthoft [email protected]
lstype('integer') #if you have any such in your environment.
lstype('integer') #if you have any such in your environment.
These functions behave similarly to min, max
and which(x == max/min (x)
to find the n-th greatest max or min of a dataset.
maxn(x,nth=1) minn(x,nth=1) which.maxn(x, nth = 1, arr.ind = FALSE, useNames = TRUE) which.minn(x, nth = 1, arr.ind = FALSE, useNames = TRUE)
maxn(x,nth=1) minn(x,nth=1) which.maxn(x, nth = 1, arr.ind = FALSE, useNames = TRUE) which.minn(x, nth = 1, arr.ind = FALSE, useNames = TRUE)
x |
A vector or array of numerical or string values. Note: ordering of string values may not be what is expected. See Details. |
nth |
Which lesser(greater, for min functions) to find, with default nth==1 being identical to |
arr.ind |
Same meaning as for |
useNames |
Same meaning as for |
Quoting the help page for max
:
Character versions are sorted lexicographically, and this depends on the collating sequence of the locale in use: the help for Comparison gives details. The max/min of an empty character vector is defined to be character NA. (One could argue that as "" is the smallest character element, the maximum should be "", but there is no obvious candidate for the minimum.)
For maxn, minn
, a single value which is the nth- max or min.
For the which.min,max
functions, quoting from which
:
If arr.ind == FALSE (the default), an integer vector, or a double vector if x is a long vector, with length equal to sum(x), i.e., to the number of TRUEs in x.
If arr.ind == TRUE and x is an array (has a dim attribute), the result is arrayInd(which(x), dim(x), dimnames(x)), namely a matrix whose rows each are the indices of one element of x
Carl Witthoft, [email protected]
set.seed(17) # for repeatability foo <- matrix(sample(1:10,20,replace=TRUE),5,4) maxn(foo,3) which.minn(foo,4)
set.seed(17) # for repeatability foo <- matrix(sample(1:10,20,replace=TRUE),5,4) maxn(foo,3) which.minn(foo,4)
max.col
to find for minimum or maximum of rows or columns.
These are Q&D wrappers around max.col
to make it easy to get the positions of max or the min of either rows or columns of an array. The description of the base function is, for comparison, "Find the maximum position for each row of a matrix".
maxRow(mat,ties.method = c("random", "first", "last") ) minRow(mat,ties.method = c("random", "first", "last") ) minCol(mat,ties.method = c("random", "first", "last") ) maxCol(mat,ties.method = c("random", "first", "last") )
maxRow(mat,ties.method = c("random", "first", "last") ) minRow(mat,ties.method = c("random", "first", "last") ) minCol(mat,ties.method = c("random", "first", "last") ) maxCol(mat,ties.method = c("random", "first", "last") )
mat |
A 2-D matrix, same rules as for |
ties.method |
Specify how to deal with ties, using same internal rules as |
For each of these functions, same as for max.col
:
index of a max or min value for each row or column, an integer vector of length nrow(mat) or ncol(mat).
Carl Witthoft, [email protected]
This function calculates the min, max, median, mean, standard deviation, skew
and kurtosis
for the specified object and displays the results in a semi-tabular form. An option is provided to set the number of digits displayed for the returned values.
Note: see the help pages in this package for theskew
and thekurt
for information on those implementations.
mystat(x, numdig = 3, na.rm = TRUE, printit = TRUE)
mystat(x, numdig = 3, na.rm = TRUE, printit = TRUE)
x |
A vector or vectorizable object. |
numdig |
How many digits to the right of the decimal point to display (when |
na.rm |
Does the user desire NA values to be removed. Rare is the need to set this to FALSE. |
printit |
Set to TRUE to see the results, nicely formatted, in the console. |
A data frame with scalar elements matching their names:
min |
minimum |
max |
maximum |
mean |
mean value |
median |
median |
sdev |
standard deviation |
skew |
skew |
kurtosis |
kurtosis |
Carl Witthoft, [email protected]
x <- runif(100) mystat(x) mystat(x,numdig=6)
x <- runif(100) mystat(x) mystat(x,numdig=6)
This is a Q&D tool to find the locations where two polygons, in a plane only (not 3D space), intersect.
polyInt(poly1,poly2, stopAtFirst = FALSE, plotit = FALSE, roundPrecision = 10, ...)
polyInt(poly1,poly2, stopAtFirst = FALSE, plotit = FALSE, roundPrecision = 10, ...)
poly1 |
An Nx2 or 2xN matrix with X-values in the first row/column and Y-values in the second. |
poly2 |
An Nx2 or 2xN matrix with X-values in the first row/column and Y-values in the second. |
stopAtFirst |
Boolean: if TRUE, then just return the first intersection point found. Useful time-saver when the user only needs to know if the polygons intersect. |
plotit |
Boolean: if TRUE, (and stopAtFirst is FALSE), the two polygons are plotted and the intersection points marked on the graph. |
roundPrecision |
Number of digits that data should be rounded to. This is necessary to avoid the usual floating-point precision problems when checking for possible duplicated intersection points. |
... |
Arguments to be passed along to the internal |
The function loops over all pairs of segments (one from poly1
and one from poly2
), calling segSegInt
to see if they intersect. After all pair-combinations are tested, the collected intersection points, if any, are reduced to the unique collection. This avoids repetition when an intersection point is a vertex of one (or both) of the polygons.
It is not necessary to "close" the supplied set of vertices, i.e. repeat the initial vertex at the end of the array as is needed to generate a complete line-plot of a polygon. The function will add that repeated vertex if it's not present in the input polygon(s).
Note: The supporting function segSegInt
returns NA when two segments are parallel. However, when two polygons in fact have an overlapping (and thus parallel) couple of edges, the adjoining edges of one or both polygons will not be parallel to these parallel edges and will intersect one or both, so the vertex which lies on the other polygon's edge will be reported.
A matrix of the x and y coordinates of all intersection points, or, if stopAtFirst
is TRUE, the first intersection point found. If no intersections exist, NULL is returned.
Carl Witthoft, [email protected]
There are many tools which are far faster and more flexible. I wrote this one because it uses only base functions and doesn't require converting polygon vertices into a special class variable. Here are two common packages. intersect
, st_intersection
sqone <- cbind(c(0,1,1,0),c(0,0,1,1)) sqtwo <- sqone + 0.5 foo <- polyInt(sqone, sqtwo, plotit = TRUE)
sqone <- cbind(c(0,1,1,0),c(0,0,1,1)) sqtwo <- sqone + 0.5 foo <- polyInt(sqone, sqtwo, plotit = TRUE)
bash
command with same name
popd
is based on the cygwin
bash
manpages' description of these commands.
popd(dn=FALSE, pull=0)
popd(dn=FALSE, pull=0)
dn |
Determines whether a stack "pop" is to be performed. This is the equivalent of the first argument in |
pull |
Equivalent of the latter |
Recommend reading man bash
for full details of the operations. This implementation will not change the working directory if dn
is TRUE
The directory history is stored in a file in the function's environment (not console environment).dirhist
, typically first created with pushd
.
A status value: 0
for success or 1
if there is no stack file (.dirhist
). Future upgrades may include other codes for other failure mechanisms, but for now error messages will have to suffice.
Carl Witthoft [email protected]
## depends on your local directory structure and permissions getwd() pushd("~/..") getwd() popd() getwd()
## depends on your local directory structure and permissions getwd() pushd("~/..") getwd() popd() getwd()
bash
command with same name
pushd
is based on the cygwin
bash
manpages' description of these commands.
pushd(path, dn=FALSE,rot=0)
pushd(path, dn=FALSE,rot=0)
path |
The directory to move into. |
dn |
Equivalent of the |
rot |
Equivalent of the |
Recommend reading man bash
for full details of the operations. This implementation should do nothing more than change the working directory (and store directory history in a file in the function's environment (not console environment) .dirhist
).
A status value, which is always 0
for success. A future upgrade may implement a trycatch
for conditions such as an inaccessible directory, but for now error messages will have to suffice.
Carl Witthoft [email protected]
## depends on your local directory structure and permissions getwd() pushd("~/..") getwd() popd() getwd()
## depends on your local directory structure and permissions getwd() pushd("~/..") getwd() popd() getwd()
.
ratRoot(Anum, Adenom = rep(1,times=length(Anum)) )
ratRoot(Anum, Adenom = rep(1,times=length(Anum)) )
Anum |
A vector of the polynomial coefficients' numerators, starting with the highest power. Values can be numeric or bigz. |
Adenom |
A vector of the polynomial coefficients' denominators, starting with the highest power. Values can be numeric or bigz. Default is all ones, indicating that all input coefficients are integers. |
The code makes use of the 'Rational Root Theorem,' which states that all real, rational roots p/q meet two criteria. Given a polynomial of the form An *x^n + A_(n-1) * x^(n-1) + ... + A0 = 0
, all coefficients adjusted to be integers, 'p' must be a factor of the zero-power polynomial coefficient A0, and 'q' must be a factor of the maximum-power polynomial coefficient An.
If any of the input Adenom
are not '1', the function will make this adjustment.
A vector of bigq
fractions representing all real rational roots found. If empty, all roots are irrational (or perhaps complex).
Carl Witthoft, [email protected]
https://en.wikipedia.org/wiki/Rational_root_theorem
ratRoot(c(6, -15, 261), c(1,1,49)) ratRoot(c(1,0,0,0,-1)) ratRoot(c(1,-2,1))
ratRoot(c(6, -15, 261), c(1,1,49)) ratRoot(c(1,0,0,0,-1)) ratRoot(c(1,-2,1))
.Rdata
- type file.
Take an existing myfile.Rdata
data file and add the specified objects to it. This is achieved by opening the data file in a local environment, "dumping" the new objects into that environment, and re-saving everything to the same file name.
resave(..., list = character(), file, overwrite = TRUE, loadverbose = FALSE )
resave(..., list = character(), file, overwrite = TRUE, loadverbose = FALSE )
... |
Names of objects to save. Same usage as in |
list |
A list of names of the objects to save. Can be used with or without any named arguments in |
file |
The name of the file to open and add items to. |
overwrite |
If TRUE (the default), items currently in the selected file will be overwritten with items of the same name in the console environment. If FALSE, they will not be overwritten; a warning message will be issued. |
loadverbose |
Default is FALSE. If TRUE, then the names of all objects currently in the file are echoed to the console |
Nothing is returned. This function is used solely to put objects into the file.
Code is essentially the same as that provided by "flodel" on StackOverflow, with some enhancements based on "save_append" by "nehartj" on GitHub
Carl Witthoft [email protected]
## not run to avoid creating detritus # foo<-1:4 # bar<-5:8 # save(foo,file='foo.Rdata') # resave(bar,file='foo.Rdata') # #check your work # lsdata('foo.Rdata')
## not run to avoid creating detritus # foo<-1:4 # bar<-5:8 # save(foo,file='foo.Rdata') # resave(bar,file='foo.Rdata') # #check your work # lsdata('foo.Rdata')
This function finds the intersection of the two lines containing the provided line segments, then checks whether the intersection is contained within both segments. This is an implementation of the equations presented, among many other sources, at https://paulbourke.net/geometry/pointlineplane/ . Bourke comments there, that "The equations apply to lines, if the intersection of line segments is required then it is only necessary to test if ua and ub lie between 0 and 1. Whichever one lies within that range then the corresponding line segment contains the intersection point. If both lie within the range of 0 to 1 then the intersection point is within both line segments."
segSegInt(seg1, seg2=NULL, plotit=FALSE, probParallel = 1e-10, ...)
segSegInt(seg1, seg2=NULL, plotit=FALSE, probParallel = 1e-10, ...)
seg1 |
An 2x2 or 4x2 matrix with X-values in the first column and Y-values in the second column. If 4x2, the lower 2 rows are assigned to |
seg2 |
An 2x2 or matrix with X-values in the first column and Y-values in the second column. Ignored if |
plotit |
Boolean: if TRUE, (and stopAtFirst is FALSE), the two line segments are plotted and, if one exists, the intersection point marked. |
probParallel |
A numeric value, typically quite small, used to identify line segments which probably are parallel. This basically is checking for identical slopes. |
... |
Not used at present |
The function runs a check for parallelism so as not to return an infinity of intersection points, then basically checks for intersection of the lines to which the line segments belong, and finally verifies said intersection belongs to both line segments. This is an implementation of the equations presented at, among many other sources, https://paulbourke.net/geometry/pointlineplane/ . Bourke comments there, that "The equations apply to lines, if the intersection of line segments is required then it is only necessary to test if ua and ub lie between 0 and 1. Whichever one lies within that range then the corresponding line segment contains the intersection point. If both lie within the range of 0 to 1 then the intersection point is within both line segments."
A matrix of the x and y coordinates of the intersection points. If no intersection exists, c(NA,NA)
is returned. This is done so the return value is always a 2-element vector.
Carl Witthoft, [email protected]
rle
to find and encode linear sequences.
The function rle
, or "run-length encoder," is a simple compression scheme which identifies sequences of repeating values in a vector. seqle
extends this scheme by allowing the user to specify a sequence of values with a common "slope," or delta value, between adjacent elements. seqle
with an increment of zero is the same as rle
.
seqle(x, incr = 1L, prec = .Machine$double.eps^0.5)
seqle(x, incr = 1L, prec = .Machine$double.eps^0.5)
x |
The input vector of values. |
incr |
The desired increment between elements which specifies the sequences to search for. Note that this can be either integer or float. For floating-point sequences, see the |
prec |
Defines the required precision to which elements are compared when determining whether they are part of a sequence. Note that for integer inputs, this value is more or less meaningless. |
Note: the returned value is assigned the class "rle"
. So far as I can tell, this class has only a print
method, i.e. defining what is returned to the console when the user types the name of the returned object.
Since the concept of "increment" has no reliable meaning when dealing with characters or char strings, when x
is non-numeric the argument incr
is ignored and the function reverts to base::rle
.
lengths |
a vector of the lengths (1 or greater) of all sequences found. |
values |
a vector of the initial value for each sequence. For example, if |
The bulk of the code is taken directly from base::rle
. Thanks to "flodel" on StackOverflow for suggesting code to handle floating-point increments.
Carl Witthoft, [email protected]
x<- c(2,2,2,3:8,8,8,4,4,4,5,5.5,6) seqle(x,incr=0) seqle(x,incr=1) seqle(x,incr=1.5)
x<- c(2,2,2,3:8,8,8,4,4,4,5,5.5,6) seqle(x,incr=0) seqle(x,incr=1) seqle(x,incr=1.5)
The user specifies both the number of elements to display and the number of elements at the start and end of the vector to ignore ('skip') when selecting elements. The results are displayed in a nice tabular form. There are options to set the value of N as well as the number of values to "skip" before selecting the values. short
is similar to a combination of the unix head
and tail
functions.
short(x = seq(1, 20), numel = 4, skipel = 0, ynam = deparse(substitute(x)), dorows=FALSE)
short(x = seq(1, 20), numel = 4, skipel = 0, ynam = deparse(substitute(x)), dorows=FALSE)
x |
The data vector to be examined. |
numel |
How many elements to display. Note that |
skipel |
If desired, skip the first |
ynam |
Not normally changed by the user. |
dorows |
For matrices only, return the "numel" number of rows rather than elements. |
If the argument x
happens to be a list, short
unlists everything, so the first numel
values will be taken from the first list element, going on to the second element as needed, etc.
Nothing is returned of interest. The function is called to provide what is printed directly to the console, which is a formatted table of the lead and tail values selected, with column labels identifying their location in the input vector object.
Carl Witthoft [email protected]
foo<-matrix(runif(100),nrow=20) short(foo) short(foo,numel=6,skipel=10) short(foo,numel=6,skipel=10,dorows=TRUE)
foo<-matrix(runif(100),nrow=20) short(foo) short(foo,numel=6,skipel=10) short(foo,numel=6,skipel=10,dorows=TRUE)
Execute simple zero-argument functions without having to type the "()" , and without having to go through the bother of makeActiveBinding
.
This code is provided primarily to allow the user to build his own set of command "shortcuts" by modifying the set of arguments to the switch
function in the function body. The bulk of the code is copied from the excellent package sos
.
The name splatnd
cannot be called directly, and doesn't even exist after being sourced. It serves to define a variety of operators ![your_string_here]
. If the string after !
is not in the switch-list, the function defaults to the normal splat operator, i.e. NOT[your_string_here] .
none
The strings supported, with their associated functions, as delivered are:
'newdev' = dev.new(width=4.5, height= 4.5, restoreConsole=T),
'qapla' = cat('batlh tIn chav'),
'quitn' = quit('no'),
'quity' = quit('yes'),
There's an obvious risk of undesired results should there exist an object in the environment with the same name as one of the items in the switch
options. The workaround is to enclose the object name in parentheses. See the example.
The returned value is the result of whatever function or operator was invoked.
The bulk of the code is taken directly from the sos
package.
Carl Witthoft, [email protected]
The R manuals on creating operators, findFn
in the package sos
, normally invoked as ???
# based on the default items in splatnd.R qapla <- 1:5 !qapla !(qapla)
# based on the default items in splatnd.R qapla <- 1:5 !qapla !(qapla)
Kurtosis is the next moment after skew (which is the moment after the variance). This function is provided primarily to support the function mystat
. It uses the algorithm provided in the R package e1071
thekurt(x)
thekurt(x)
x |
A vector or vectorizable data set. |
A single scalar, the kurtosis of the data provided.
Carl Witthoft, [email protected]
This function is included primarily to support mystat
. Skew is the next moment after the variance. The algorithm used here is taken from the R package e1071
.
theskew(x)
theskew(x)
x |
A vector of data to be evaluated |
A single scalar, the skew of the dataset
Carl Witthoft, [email protected]