userfriendlyscience (version 0.5-2)

userfriendlyscienceBasics: userfriendlyscience basics

Description

The userfriendlyscience basics functions are some very basic functions to make life that little bit easier.

Usage

safeRequire(packageName, mirrorIndex=NULL) trim(str) noZero(str) formatPvalue(values, digits = 3, spaces=TRUE, includeP = TRUE) formatR(r, digits) repStr(n = 1, str = " ") repeatStr(n = 1, str = " ") ifelseObj(condition, ifTrue, ifFalse) invertItem(item, fullRange=NULL, ignorePreviousInversion = FALSE) is.odd(vector) is.even(vector) convertToNumeric(vector, byFactorLabel = FALSE) massConvertToNumeric(dat, byFactorLabel = FALSE, ignoreCharacter = TRUE, stringsAsFactors = FALSE) vecTxt(vector, delimiter = ", ", useQuote = "", firstDelimiter = NULL, lastDelimiter = " & ", firstElements = 0, lastElements = 1, lastHasPrecedence = TRUE) vecTxtQ(vector, useQuote = "'", ...) find %IN% table cat0(..., sep="") addToLog(fullLog, ..., showLog = FALSE);

Arguments

packageName
The name of the package, as character string.
mirrorIndex
The index of the mirror to use, in case you want to specify the mirror in the call (see e.g. /code/linkgetCRANmirrors()[, 1:4] for an overview of these mirrors. For example, at the time of writing, Antwerp is 7, Amsterdam is 60, and Auckland is 62).
str
The character string to process.
values
The p-values to format.
digits
For formatPvalue, number of digits to round to. Numbers smaller than this number will be shown as <.001 or="" <.0001="" etc.<="" p="">

For formatR, the number of digits to use when formatting the Pearson correlation.

spaces
Whether to include spaces between symbols, operators, and digits.
includeP
Whether to include the 'p' and '='-symbol in the results (the '
r
The Pearson correlation to format.
n
The number of times to repeat the string.
condition
Condition to evaluate.
ifTrue
Object to return if the condition is true.
ifFalse
Object to return if the condition is false.
item
Item to invert
fullRange
If provided it must be a numeric vector with the minimum and the maximum of the scale. If not provided, the range function is used (so, use this range argument if the scale minimum and/or maximum do not occur in the data).
ignorePreviousInversion
If this item has already been inverted, the function will halt with an error unless it's told to ignore previous inversions with this boolean.
dat, vector
The dataframe of vector to process.
byFactorLabel
If TRUE, convertToNumeric and massConvertToNumeric use the factor labels, interpreted as character vectors, to determine the numeric value, instead of the level's indices (which is what as.numeric() does).
ignoreCharacter
If TRUE, character vectors are ignored. If FALSE, character vectors are converted (or, an attempt is made :-)).
stringsAsFactors
If TRUE, strings (character vectors) in the dataframe will be converted to factors (by as.data.frame, after the function called lapply).
find
The element(s) to look up in the vector or matrix.
table
The vector or matrix in which to look up the element.
delimiter, firstDelimiter, lastDelimiter
The delimiters to use for respectively the middle, first firstElements, and last lastElements elements.
useQuote
This character string is pre- and appended to all elements; so use this to quote all elements (useQuote="'"), doublequote all elements (useQuote='"'), or anything else (e.g. useQuote='|'). The only difference between vecTxt and vecTxtQ is that the latter by default quotes the elements.
firstElements, lastElements
The number of elements for which to use the first respective last delimiters
lastHasPrecedence
If the vector is very short, it's possible that the sum of firstElements and lastElements is larger than the vector length. In that case, downwardly adjust the number of elements to separate with the first delimiter (TRUE) or the number of elements to separate with the last delimiter (FALSE)?
sep
The separator to pass to cat, of course, "" by default.
fullLog
The full log - the character vector(s) provided are appended to this character vector.
showLog
Whether to cat the log.
...
Extra arguments are passed to whatever function is wrapped (e.g. cat for cat0). For addToLog, the dots are used to provide character vectors that are concatenated using paste0 and (potentially shown and) added to the log.

Value

safeRequire returns nothing.trim, formatPvalue, noZero, formatR, and repeatStr return a string.ifelseObj return an object.The invertItem function returns the inverted item vector, with an attribute "inverted" set to TRUE.is.odd and is.even return a logical vector.%IN% returns a logical vector of the same length as its first argument.cat0 returns a string.addToLog returns a string.

Details

The safeRequire function checks whether a package is already installed. If so, it loads the package (using require/library). If not, it first installs it, and then loads it.

The trim function removes whitespaces from the start and end of a text string.

The noZero function removes the first zero from a string that was originally a number.

The formatPvalue function formats a P value, roughly according to APA style guidelines. This means that the noZero is used to remove the zero preceding the decimal point, and p values that would round to zero given the requested number of digits are shown as e.g. p<.001.< p="">

The formatR function format a Pearson correlation for pretty printing (using noZero).

The repeatStr (or repStr) function repeats a string a given number of times.

The ifelseObj function just evaluates a condition, returning one object if it's true, and another if it's false.

The invertItem function 'unmirrors' an inverted item (i.e. for a 1-3 item, 1 becomes 3, 2 stays 2, and 3 becomes 1).

is.odd and is.even check whether a number is, or numbers in a vector are, odd or even.

The infix function %IN% is a case-insensitive version of %in%.

The cat0 function is to cat what paste0 is to paste; it simply makes concatenating many strings without a separator easier.

The addToLog function adds a character vector to a log.

Examples

Run this code

### load a package
safeRequire('ggplot2');

### trim a string
trim(' this is a string with whitespace in front and at the end               ');
### Returns "this is a string with whitespace in front and at the end"

repeatStr("-", 8);
### Returns "--------" (incredibly useful, no? :-))

tempVector <- c(1,2,3,3,2,4,3,2,1,1,3,4,5,4,3,2,2,1,1,2);
invertedTempVector <- invertItem(tempVector);

### We can also invert it back, but then we have to override the security
### that prevents accidently inverting items back.
invertItem(tempVector, ignorePreviousInversion=TRUE);

Run the code above in your browser using DataCamp Workspace