Learn R Programming

userfriendlyscience (version 0.1-2)

userfriendlyscienceBasics: userfriendlyscience basics

Description

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

Usage

safeRequire(packageName)
trim(str)
noZero(str)
formatR(r, digits)
repeatStr(str = " ", n = 1)
ifelseObj(condition, ifTrue, ifFalse)
invertItem(item, range=NULL, ignorePreviousInversion = FALSE)
is.odd(vector)
is.even(vector)
convertToNumeric(vector, byFactorLabel = FALSE)
massConvertToNumeric(dat, byFactorLabel = FALSE, ignoreCharacter = TRUE)

Arguments

packageName
The name of the package, as character string.
str
The character string to process.
r
The Pearson correlation to format.
digits
The number of digits to use when formatting the Pearson correlation.
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
range
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 :-)).

Value

  • safeRequire returns nothing. trim, 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.

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 formatR function format a Pearson correlation for pretty printing (using noZero). The repeatStr 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.

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 DataLab