R package miscset version 1.1.0
.
A collection of miscellaneous methods to simplify various tasks, including plotting, data.frame and matrix transformations, environment functions, regular expression methods, and string and logical operations, as well as numerical and statistical tools.
Most of the methods are simple but useful wrappers of common base
R
functions, which extend S3 generics or provide default
values for important parameters.
Install the latest version from CRAN via:
Install the development version from github via:
install.packages('devtools')
devtools::install_github('setempler/miscset@develop', build_vignettes = TRUE)
After installation, load the package via
If you like to contribute to the development of the packages, please
Get help in an R session via
help.index(miscset)
?
+ function nameciplot
Plot a bargraph with error bars. Input data is a list with numeric
vectors. Functions to calculate bar heights (e.g. mean
by
default) and error bar sizes (e.g. confint.numeric
by
default) can be modified (e.g. sd
for error bars).
ggplotGrid
Arrange ggplots on a grid (plot window or pdf file). Supply a list
with ggplot
objects and define number of rows and/or
columns. If a path
is supplied, the plot is written to that
file instead of the internal graphics device.
library(ggplot2)
plots <- list(
ggplot(d, aes(x = b, y= -c, col = b)) + geom_line(),
ggplot(d, aes(x = b, y = -c, shape = factor(b))) + geom_point())
ggplotGrid(plots, ncol = 2)
The function ggplotGridA4
supports direct output to DIN
A4 sized pdfs.
gghcl
Generate a character vector with html values from a color hue as in
ggplot
.
[1] "#F8766D" "#00BA38" "#619CFF"
sort
Sort data.frame
objects. This extends the functionality
of the base R distributed generic sort
. Define multiple
columns by column names as character vector or expression.
a b c
1 2 2 5
2 1 3 4
3 3 4 3
4 NA 5 2
5 1 6 1
a b c
5 1 6 1
2 1 3 4
1 2 2 5
3 3 4 3
do.rbind
Note: This function is now deprecated. It is recommended to use
rbindlist
from the data.table
package.
A wrapper function to row-bind data.frame
objects in a
list with do.call
and rbind
. Object names from
the list are inserted as additional column.
a b c
1 2 2 5
2 1 3 4
3 3 4 3
Warning in do.rbind(list(first = d[1:2, ], second = d[1:3, ])): 'do.rbind' is deprecated.
Use 'data.table::rbindlist' instead.
See help("Deprecated")
Name a b c
1 first 2 2 5
2 first 1 3 4
3 second 2 2 5
4 second 1 3 4
5 second 3 4 3
enpaire
Generate a pairwise list (data.frame
) of a matrix
containing row and column id and upper and lower triangle values.
1 2 3
1 "a" "d" "g"
2 "b" "e" "h"
3 "c" "f" "i"
row col lower upper
1 1 2 b d
2 1 3 c g
3 2 3 f h
squarematrix
Generate a symmetric (square) matrix from an unsymmetric one using
column and row names. Fills empty cells with NA
.
1 2 3
2 "b" "e" "h"
3 "c" "f" "i"
1 2 3
1 NA NA NA
2 "b" "e" "h"
3 "c" "f" "i"
textable
Print a data.frame
as latex table. Extends
xtable
by optionally including a latex header, and if
desired writing the output to a file directly and calling a system
command to convert it to a .pdf
file, for example.
d <- data.frame(a=c(2,1,3,NA,1), b=2:6, c=5:1)
textable(d, caption = 'miscset vignette example data.frame', as.document = TRUE)
% output by function 'textable' from package miscset 1.1.0
% latex table generated in R 4.4.2 by xtable 1.8-4 package
% Sun Feb 16 04:47:12 2025
\documentclass[a4paper,10pt]{article}
\usepackage[a4paper,margin=2cm]{geometry}
\begin{document}
\begin{table}[ht]
\centering
\caption{miscset vignette example data.frame}
\begin{tabular}{rrr}
\hline
a & b & c \\
\hline
2.00 & 2 & 5 \\
1.00 & 3 & 4 \\
3.00 & 4 & 3 \\
& 5 & 2 \\
1.00 & 6 & 1 \\
\hline
\end{tabular}
\end{table}
\end{document}
help.index
Show the help index page of a package (with the list of all help pages of a package).
lload
Load multiple R data objects into a list. List is of same length as number of files provided. Sublists contain all respective objects. Simplification is possible if all names are unique.
lsall
Return all current workspace (or any custom) object names, lengths,
classes, modes and sizes in a data.frame
.
Environment: R_GlobalEnv
Objects:
Name Length Class Mode Size Unit
1 d 3 data.frame list 1.1 Kb
2 m 9 matrix character 1.5 Kb
3 n 1 integer numeric 56.0 byte
4 plots 2 list list 17.8 Kb
mgrepl
Search for multiple patterns in a character vector. Merge results by
(custom) logical functions (e.g. any
, all
) and
use mutlicore support from the parallel
package. Optionally
return the index (as with which
). Use identity
to return a matrix with the results of each pattern per row.
[1] TRUE FALSE FALSE FALSE
[1] TRUE TRUE TRUE FALSE
[1] 2 1 1 0
[,1] [,2] [,3] [,4]
[1,] TRUE TRUE FALSE FALSE
[2,] TRUE FALSE TRUE FALSE
collapse
To collapse vectors, usually a call to paste
or
paste0
setting the argument collapse
is
applied. The collapse function is a wrapper of this functionality
applied to a single vector. It can be extended with the
.unique
, .sort
and .decreasing
arguments, to return only unique and sorted values.
[1] "abcdefghijklmnopqrstuvwxyz"
[1] "abcdefghijklmnopqrstuvwxyz"
The data.frame
method allows to collapse a data frame by
identifier/grouping columns (specified with by
). Each group
piece has then all value columns collapsed with the default method.
In addition, the value columns can be collapsed to vectors, when
sep = NULL
is selected, keeping a list of vectors for this
column in the returned data frame. .sortby
allows to choose
if the result should be sorted by the grouping columns.
.unlist
provides a way to unlist value columns per group,
which is useful if the input has list columns.
# create example data
set.seed(12)
s <- s2 <- sample(LETTERS[1:4], 9, replace = TRUE)
s2[1:2] <- rev(s2[1:2])
d <- data.frame(group = rep(letters[c(3,1,2)], each = 3),
value = s,
level = factor(s2),
stringsAsFactors = FALSE)
print(d)
group value level
1 c B B
2 c B B
3 c D D
4 a C C
5 a C C
6 a B B
7 b A A
8 b A A
9 b D D
The following (default settings) collapses by all columns, which
results in an output similar to unique(d)
, but the row
names are not kept.
group value level
1 c B B
2 c D D
3 a C C
4 a B B
5 b A A
6 b D D
Specifying no grouping columns (setting by
to
0
or NULL
) collapses all columns.
group value level
1 cccaaabbb BBDCCBAAD BBDCCBAAD
Specifying at least one and maximum less than the total columns
groups the data.frame
, splits it into group pieces, and
applies the collapsing to all remaining columns.
group value level
1 c B/B/D B/B/D
2 a C/C/B C/C/B
3 b A/A/D A/A/D
If the separator sep
is not specified, the
data.frame
method allows to return list columns, containing
vectors of values per group. With the .sortby
argument, the
ouptut can be sorted on the grouping values.
group level value
1 a B B
2 a C C, C
3 b A A, A
4 b D D
5 c B B, B
6 c D D
The data.frame
method also works on
data.table
objects, since it uses the methods from the
package of the same name to split the input into group pieces. If the
input inherits from data.table
, the class is retained.
leading0
Prepend 0
characters to numbers to generate equally
sized strings.
[1] "0009" "0112" "5009"
strextr
Note: This function is now deprecated. It is recommended to use
str_extract
or str_extract_all
from the stringr
package.
Split strings by a separator (sep
) and extract all
substrings matching a pattern
. Optionally allow multiple
matches, and use multicore support from the parallel
package.
Warning in strextr(s, "n$", ","): 'strextr' is deprecated and will be removed with the release of miscset version 2.
Use 'stringr::str_extract' instead.
See examples in ?strextr
[1] "xn"
Warning in strextr(s, "^x", ",", mult = T): 'strextr' is deprecated and will be removed with the release of miscset version 2.
Use 'stringr::str_extract' instead.
See examples in ?strextr
[[1]]
[1] "xa" "xb" "xn"
[1] "xn"
[[1]]
[1] "xa" "xb" "xn"
str_part
Similar to strextr
, but extracting substrings is done by
setting an index value n
. Optionally roll the last value to
n
if it’s index is less.
[1] "xn"
str_rev
Create reverse version of strings of a character
vector.
[1] "Hello" "World!"
duplicates
and duplicatei
Determine duplicates. Return either a logical vector
(duplicates
) or an integer index (duplicatei
).
Extends the base method duplicated
by also returning
TRUE
for the first occurence of a value.
data.frame(
duplicate = d$a,
".d" = duplicated(d$a), # standard R function
".s" = duplicates(d$a),
".i" = duplicatei(d$a))
[1] .d .s .i
<0 rows> (or 0-length row.names)
p2star
Asign range symbols to values, e.g. convert p-values to significance characters.
[1] "**" "*" "." "n.s."
confint.numeric
Calculate confidence intervals. Extends the base method
confint
to numeric vectors.
[1] 0.8392064
ntri
Generate a series of triangular numbers of length n
according to OEIS#A000217. The
series for 12 rows of a triangle, for example, can be returned as in the
following example.
[1] 0 1 3 6 10 15 21 28 36 45 55 66
scale0
and scaler
Scale numeric vectors to a range of 0 to 1 with
scale0
or to a custom output range r
and input
range b
with scaler
.
[1] 1.00 0.75 0.50 0.25 0.00
[1] 3.777778 3.333333 2.888889 2.444444 2.000000