| Title: | Charles's Utility Function using Formula |
|---|---|
| Description: | Utility functions that provides wrapper to descriptive base functions like cor, mean and table. It makes use of the formula interface to pass variables to functions. It also provides operators to concatenate (%+%), to repeat (%n%) and manage character vectors for nice display. |
| Authors: | Charles-Édouard Giguère |
| Maintainer: | Charles-Édouard Giguère <[email protected]> |
| License: | GPL (>= 2) |
| Version: | 1.10 |
| Built: | 2026-06-06 09:55:43 UTC |
| Source: | https://github.com/giguerch/cuff |
This function extract coefficients tables from common statistical model (lm/glm/lme/lmer/t-test) and format them.
cf(x, addci = TRUE, pv.style = 1, signif = 2, expcf, ...)cf(x, addci = TRUE, pv.style = 1, signif = 2, expcf, ...)
x |
x is a lm/glm/lme/lmer/t.test model |
addci |
Logical value that tells R to add a 95% confidence interval to the output. True by default. |
pv.style |
Integer specifying the style (1 or 2) of p-value
formatting. See |
signif |
Either an integer specifying the number of significant digits or a dimension 3 vector for respectively the estimate, standard error and t-value |
expcf |
Logical value that tells R to add exponentiated value of estimate. Set to FALSE except if the model specifies a logistic regression (family = binomial) |
... |
Not used yet |
Returns a data.frame of formatted characters of the coefficient table.
Charles-Édouard Giguère
lm1 <- lm(Sepal.Length ~ Species, iris) cf(lm1)lm1 <- lm(Sepal.Length ~ Species, iris) cf(lm1)
This is a function that sends a table-like object to the clipboard to paste it quickly on an external program.
clip(x, sep = "\t", row.names = FALSE, quote = FALSE, ...)clip(x, sep = "\t", row.names = FALSE, quote = FALSE, ...)
x |
x is a table a matrix or a data.frame to send to clipboard |
sep |
Type of separator for the output |
row.names |
Logical value (T/F) to include or exclude row names |
quote |
logical value to print or exclude quotation marks. |
... |
other arguments passed to |
No output. The results is printed to the clipboard.
Charles-Édouard Giguère
Functions to describe continuous variables
cont.desc(x, y = NULL, ..., labels = NULL, data = NULL) ## S3 method for class 'cont.desc' print(x, ...)cont.desc(x, y = NULL, ..., labels = NULL, data = NULL) ## S3 method for class 'cont.desc' print(x, ...)
x |
Object of type |
y |
If x is a formula, |
... |
used for compatibility |
labels |
Optional vector of labels the same length as the dimension of x or the number of variables in formula |
data |
see y for details |
The cont.desc methods returns an object of type cont.desc
object with a print methods associated.
An object of type "data.frame" and "cont.desc" containing the number of variables, the number of missing variables, the mean, standard deviation, min and max.
Charles-Édouard Giguère
Ex <- data.frame(test1 = c(2, 4, NA, 3, NA), test2 = c(2, 3, 5, NA, 4), test3 = c(1, 4, 10, 23, NA)) ### Applied on a data.frame. cont.desc(Ex) ### Applied using a formula. cont.desc(~ test1 + test2, Ex)Ex <- data.frame(test1 = c(2, 4, NA, 3, NA), test2 = c(2, 3, 5, NA, 4), test3 = c(1, 4, 10, 23, NA)) ### Applied on a data.frame. cont.desc(Ex) ### Applied using a formula. cont.desc(~ test1 + test2, Ex)
This is a function that creates correlation matrix objects that can be printed with the corresponding N and p-values. It is a wrapper for cor and cor.test.
correlation(x, y = NULL, method = "pearson", alternative = "two.sided", exact = NULL, use = "pairwise.complete.obs", continuity = FALSE, data = NULL) ## S3 method for class 'corr' print(x, ... , toLatex = FALSE, cutstr = NULL, toMarkdown = FALSE)correlation(x, y = NULL, method = "pearson", alternative = "two.sided", exact = NULL, use = "pairwise.complete.obs", continuity = FALSE, data = NULL) ## S3 method for class 'corr' print(x, ... , toLatex = FALSE, cutstr = NULL, toMarkdown = FALSE)
x |
x is a matrix/ |
y |
y is a matrix/ |
method |
Method used to compute correlations. |
alternative |
Unilateral ( |
exact |
Logical value to know if a p.value is exact or
asymptotic. See |
use |
Methods to deal with missing values. |
continuity |
Logical value to know if continuity correction
must be used. See |
... |
Unused in this function |
data |
data.frame to use in conjuction with formula |
toLatex |
Logical value to know if output displayed as a latex tabular environment. |
cutstr |
Optional digits that cut the length of variable names |
toMarkdown |
Logical value to know if output should be displayed as a markdown table for report |
Returns a list with correlations, N for each pair of correlations and p.value for each correlations.
Charles-Édouard Giguère
require(CUFF) X=rnorm(10) Y=rnorm(10) correlation(cbind(X,Y))require(CUFF) X=rnorm(10) Y=rnorm(10) correlation(cbind(X,Y))
Functions to display (2 x 2) contingency table
cross(x, ...)cross(x, ...)
x |
Object of type |
... |
Arguments passed to |
The xtab functions corrects the inability to deal with missing
values in the original xtabs that comes with R base.
The cross methods returns an object of type cross with the
original table and the marginal percentages by row and by column. A
print methods is associated with a cross object. xtab returns
an object of type table (see details). Total returns a sum with
na.rm=TRUE by default and replaces NA with 0.
Charles-Édouard Giguère
require(CUFF) ### example of crosstabs cr1 <- cross( ~ N + P, npk) print(cr1, test = c("chisq.test", "fisher.test"))require(CUFF) ### example of crosstabs cr1 <- cross( ~ N + P, npk) print(cr1, test = c("chisq.test", "fisher.test"))
Functions to display frequency
freq(x, y = NULL, ..., labels = NULL, data = NULL) ## S3 method for class 'frequencies' print(x, ..., toLatex = FALSE)freq(x, y = NULL, ..., labels = NULL, data = NULL) ## S3 method for class 'frequencies' print(x, ..., toLatex = FALSE)
x |
Object of type |
y |
If x is a formula, |
... |
used for compatibility |
labels |
Optional vector of labels the same length as the dimension of x or the number of variables in formula. |
data |
see y for details |
toLatex |
Logical value that indicates if the print methods should return a tabular latex environment to use with Sweave or knitr. |
The freq methods returns an object of type frequencies
object with a print methods associated.
An object of type "frequencies" that is a list of matrix containing
the frequencies the % and the % with missing value.
Charles-Édouard Giguère
require(CUFF) ### example of crosstabs fr1 <- freq( ~ N + P, npk, c("Nitrogen", "Phosphate")) fr1 ### To use with sweave or knitr. print(fr1, toLatex = TRUE)require(CUFF) ### example of crosstabs fr1 <- freq( ~ N + P, npk, c("Nitrogen", "Phosphate")) fr1 ### To use with sweave or knitr. print(fr1, toLatex = TRUE)
La fonction retourne une table avec le contenu en caractères de la fréquence et du pourcentage
ftab(xt, margin = seq_along(dim(xt)), fmt = "%d (%5.1f %%)", quiet = FALSE)ftab(xt, margin = seq_along(dim(xt)), fmt = "%d (%5.1f %%)", quiet = FALSE)
xt |
Une table de contingence généré avec table ou xtabs |
margin |
Si 2x2, est que le pourcentage est en ligne (1) ou en colonne(2) ou total (1:2). Par défaut, pourcentage total. Ne sert à rien lorsque le tableau est à une dimension. |
fmt |
format d'affichage |
quiet |
Valeur logique qui indique si le tableau est imprimé |
Retourne une table avec le contenu en caractères de la frequence et du pourcentage
Charles-Édouard Giguère
ex <- as.table(cbind(3:4,5:6)) ftab(ex,2)ex <- as.table(cbind(3:4,5:6)) ftab(ex,2)
Methods that estimates a mean and sd and stores it into a single string
meansd(x, digits = c(1, 1))meansd(x, digits = c(1, 1))
x |
A vector of numeric value |
digits |
digits for respectively the mean and sd. If a single value is entered it applies to mean and sd |
Returns a string containing mean and sd with entered digit precisions.
Charles-Édouard Giguère
xf(Sepal.Width ~ Species, iris, meansd)xf(Sepal.Width ~ Species, iris, meansd)
A 10 color palette.
pal_CUFF(n = 10, pal = "CUFF")pal_CUFF(n = 10, pal = "CUFF")
n |
Integer indicating the number of color desired (1-10) |
pal |
The only value possible for now is CUFF |
Returns a vector of color.
Charles-Édouard Giguère
pal_CUFF(3)pal_CUFF(3)
Functions to display (2 x 2) contingency table
## S3 method for class 'cross' print(x, ..., test = "chisq.test", export = NULL)## S3 method for class 'cross' print(x, ..., test = "chisq.test", export = NULL)
x |
Object of type |
... |
Unused arguments |
test |
list of statistical tests (as character vector) passed to
the 2x2 table. By default, |
export |
Either "pdf" or "xlsx" or NUll. Crosstab is flushed into either a pdf using latex or an Excel spreadsheet using package openxlsx |
Export to "pdf", "xlsx" open the crosstabs in the corresponding formats.
Print methods associated with the cross object.
Charles-Édouard Giguère
require(CUFF) ### example of crosstabs cr1 <- cross( ~ N + P, npk) print(cr1, test = c("chisq.test", "fisher.test"))require(CUFF) ### example of crosstabs cr1 <- cross( ~ N + P, npk) print(cr1, test = c("chisq.test", "fisher.test"))
This is a function that format p-values for publication.
pv(p, style = 1)pv(p, style = 1)
p |
A vector of p-values |
style |
By default (1), formatting according to APA style guide version 6 |
(1) APA: 2 digits of significance except if p is <0.05. If p < 0.05 we use 3 digits of significance except if p < 0.001 when we print "<0.001".
(2) Other: 4 digits of significance except if p < 0.0001 when we print "<0.0001".
returns a character vector of formatted p-value.
Charles-Édouard Giguère
p <- c(0.1563,0.0122,0.00001) pv(p)p <- c(0.1563,0.0122,0.00001) pv(p)
Function %+% paste characters with other characters pairwise.
Function %n% is used to repeat a character n time.
Function numtostr converts numeric to a string in a nice
format.
x %+% y x %n% y numtostr(x,nch,digits=4)x %+% y x %n% y numtostr(x,nch,digits=4)
x |
Character vector or a numeric vector for |
y |
Character vector |
nch |
(Optionnal) length of the resulting character vector |
digits |
Number of digits in the resulting strings |
Function %+% is an operator that shortens paste(x, y,
sep="") see help(paste) for more options. Function
%n% returns the character vector x repeated y times. If both x and
y are vector each element of x are applied to each element of y.
Function numtostr converts numerical vector to a character
vector using a nice format.
Charles-Édouard Giguère
require(CUFF) "Hello " %+% "world." cat(" " %n% c(rep(1,9),2) %+% 1:10,fill=TRUE) ### Returns a * because specified length of character is unsufficient. numtostr(9048948449.94948,nch=8)require(CUFF) "Hello " %+% "world." cat(" " %n% c(rep(1,9),2) %+% 1:10,fill=TRUE) ### Returns a * because specified length of character is unsufficient. numtostr(9048948449.94948,nch=8)
Methods that estimates a sum weighted by the number of non-missing values
## S3 method for class 'n' sum(x,n = 1, ...)## S3 method for class 'n' sum(x,n = 1, ...)
x |
A vector of values possibly containing missing values. |
n |
Minimum number of valid values |
... |
extra parameters to sum |
sum(x,n) = mean(x) * length(x) / n.valid(x)
sum.n returns the values of the weighted sum.
Charles-Édouard Giguère
sum.n(c(1, 2, NA, NA), n = 2) ### [1] 6 sum.n(c(1, NA, NA, NA), n = 2) ### [1] NAsum.n(c(1, 2, NA, NA), n = 2) ### [1] 6 sum.n(c(1, NA, NA, NA), n = 2) ### [1] NA
This functions export a data frame into a csv file with a csv companion file containing formats to properly reimport data into R.
column_types(data) to_csv(data, file)column_types(data) to_csv(data, file)
data |
A |
file |
Name of the csv file to export to |
returns nothing
Charles-Édouard Giguère
# to_csv(iris,"iris.csv")# to_csv(iris,"iris.csv")
Wrapper to DT::datatable.
view(x, ...)view(x, ...)
x |
x is a matrix/ |
... |
arguments passed to datatable |
Export data to be viewed as a web page. See help(datatable,
package = "DT") for further details.
Charles-Édouard Giguère
view(iris) ### add filter on top. view(iris, filter = "top")view(iris) ### add filter on top. view(iris, filter = "top")
Methods that apply a function across a levels of one or more factors. It works like aggregate but returns a table instead. It also has a useNA options that adds NA as a level before applying the function.
xf(formula, data, FUN, ..., subset, na.action = na.omit, useNA = FALSE, addmargins = TRUE)xf(formula, data, FUN, ..., subset, na.action = na.omit, useNA = FALSE, addmargins = TRUE)
formula |
Formula defining the variables. On the left is the variable we are applying the function to, on the right, variables defining levels of the tables |
data |
Data.frame containing the variables |
FUN |
The function to apply to each subset of data |
... |
extra parameters to FUN |
subset |
Vectors defining a subset of data.frame
(see |
na.action |
Action functions to deal with NA in data file |
useNA |
Make NA a level of the factors (if any) |
addmargins |
Add function applied to the margins of each category |
xf returns an object "xf" that behaves like a table with all associated methods.
Charles-Édouard Giguère
res <- xf(Sepal.Length~Species,iris,mean) barplot(res)res <- xf(Sepal.Length~Species,iris,mean) barplot(res)
Functions to create contingency table using formula
xtab(formula, data, useNA = FALSE, exclude = c(NA,NaN), miss.char = "-", na.action = na.exclude, subset = NULL, sparse = FALSE, drop.unused.levels = FALSE) Total(x)xtab(formula, data, useNA = FALSE, exclude = c(NA,NaN), miss.char = "-", na.action = na.exclude, subset = NULL, sparse = FALSE, drop.unused.levels = FALSE) Total(x)
formula |
Object of class |
data |
data frame to use with formula |
useNA |
logical values to add NA to the levels in the table |
exclude |
levels to exclude from table |
miss.char |
Character to replace NA |
na.action |
methods to deal with NA |
subset |
subset to use in data |
sparse |
see |
drop.unused.levels |
logical values indicating whether we drop empty levels |
x |
numerical vector |
The xtab functions corrects the inability to deal with missing
values in the original xtabs that comes with R base. Total is
a utility function to use in conjunction with addmargins instead of sum.
xtab returns an object of type table (see
details). Total returns a sum with na.rm=TRUE by
default and replaces NA with 0.
Charles-Édouard Giguère
require(CUFF) ### example of crosstabs xtab( ~ N + P, npk)require(CUFF) ### example of crosstabs xtab( ~ N + P, npk)
Function %xyb% or xyboth(x, y) shows index present in x, y and both
x %xyb% y xyboth(x, y)x %xyb% y xyboth(x, y)
x |
vector(matrix/dataframe) of indices |
y |
vector(matrix/dataframe) of indices |
Returns a list with indices present only in x and y and in both.
Charles-Édouard Giguère
require(CUFF) xyboth(1:5, 3:6) ### $x ### [1] "1" "2" ### ### $y ### [1] "6" ### ### $both ### [1] "3" "4" "5"require(CUFF) xyboth(1:5, 3:6) ### $x ### [1] "1" "2" ### ### $y ### [1] "6" ### ### $both ### [1] "3" "4" "5"