This pseudo-generic function iterates a function on the subelements of a list of objects that have the same class and matching dimensions/names and reorganizes the result to match the structure of the replicates or a prototype template.
Usage
repmatch(x, FUN = NULL, proto = NULL, direct = c("dim", "name"), ...)
Arguments
- x
(
list
): Alist
of replicates.- FUN
(
function
): A function to merge with and to be applied to the values of identical positions in different replicates. This function must have a single output value,vectors
are not allowed. The defaultNULL
option returns an element-wise reorganization of the data.- proto
(
same as x[[1]]
): The prototype for matching/merging. The prototype is used as a check ("dim"
) or a template ("name"
) during the matching process, depending on the used directive (direct
argument). It is an object with the same class as the replicates, and have the same dimensions and/or overlapping names. If the"name"
directive is used and aprototype
is provided, the funtion will force the output to have the same structure as the prototype, by omitting unnecessary information and inserting missing values (NAs
). The prototype is expected to be an object that has more or equal elements than the replicates, otherwise the call will result in a warning.- direct
(
character
): Matching directive(s). Can either be dimension-based ("dim"
) and/or name-based (name
). Dimension-based directive matches the replicates if they have the same dimesions. The"name"
directive requires named input (formatrices
anddata.frames
colnames
andrownames
attributes). Replicates will be matched if the values have the same names. In case both directives are specified (default), dimension-based directive takes higher priority, if matching is unsuccessful with dimensions, names will be tried after.- ...
arguments passed to
FUN
.
Value
If FUN
is a function
, the output is vector
for vector
-like replicates, matrix
when x
is a list
of matrix
objects, and data.frame
s for data.frame
replicates. In case FUN=NULL
: if x
is a list of vectors
, the function will return a matrix
; an array
is returned, if x
is a list
of matrix
class obejcts; if x
is a list of data.frame
objects, the function returns a data.frame
.
Details
The function is designed to unify/merge objects that result from the same function applied to different source data (e.g. the results of subsample()
). In its current form, the function supports vectors
(including one-dimensional tables
and arrays
), matrix
and data.frame
objects.
Examples
# basic example
vect <- rnorm(100)
# make 50 replicates
repl <- rep(list(vect), 50)
repmatch(repl, FUN=mean, direct="dim")
#> [1] 0.34555663 0.21193475 0.85943363 -1.46270933 0.45808211 0.93120029
#> [7] -1.16399872 -1.64767276 1.82738309 0.52709708 0.78430370 -0.47528247
#> [13] 0.07790114 0.34037140 0.56214030 0.15815519 0.01739045 0.47345553
#> [19] 0.04539528 0.48263365 0.12259883 -0.04101814 -1.99163226 1.72479034
#> [25] 1.30351018 1.44145260 0.78692136 0.56016516 -0.20165660 -1.05955928
#> [31] -0.85720845 -0.98485341 -0.92896983 -1.52907711 -1.11629364 -0.60151636
#> [37] -0.16061176 0.93403406 -2.28522928 0.33059962 0.68594225 -0.24001439
#> [43] -0.12141336 0.60129668 1.02040809 0.52694955 -0.52213211 0.57715822
#> [49] 0.24254920 -1.35095594 -0.71934918 -2.01744035 -0.52249265 0.36124786
#> [55] 2.53790603 0.58898658 -1.30478427 0.08819530 -1.66775220 -0.49498423
#> [61] 0.98836718 -1.03506021 0.02072693 0.25420227 -0.27989001 0.34561189
#> [67] 1.03011935 0.15431641 0.30615782 -1.48830538 0.21371816 1.13527624
#> [73] -0.64021064 1.08906508 1.51440242 1.35712556 -0.52907719 0.72002152
#> [79] 1.25140588 0.10184550 1.80032594 1.38757299 -0.16565126 -0.37637451
#> [85] -0.77494773 0.09669717 0.77364363 -0.24915889 0.13028962 1.06649401
#> [91] -0.91104075 0.68359833 -0.43531541 -0.00278854 -0.40031225 0.13232767
#> [97] 0.55000633 0.34194945 0.13693550 -0.15284263
# named input
# two vectors
# a
a<- 1:10
names(a) <- letters[1:length(a)]
a[c(3,5,8)] <- NA
a <- a[!is.na(a)]
#b
b<- 10:1
names(b) <- letters[length(b):1]
b[c(1, 3,6, length(b))]<- NA
b <- b[!is.na(b)]
# list
x2 <- rep(c(list(a),list(b)), 3)
# simple match - falling through "dim" to "name" directive
repmatch(x2, FUN=NULL)
#> [,1] [,2] [,3] [,4] [,5] [,6]
#> a 1 NA 1 NA 1 NA
#> b 2 2 2 2 2 2
#> c NA 3 NA 3 NA 3
#> d 4 4 4 4 4 4
#> f 6 6 6 6 6 6
#> g 7 7 7 7 7 7
#> i 9 9 9 9 9 9
#> j 10 NA 10 NA 10 NA
# prototyped
prot <- 1:10
names(prot) <-letters[1:10]
repmatch(x2, FUN=mean, proto=prot, na.rm=TRUE)
#> a b c d e f g h i j
#> 1 2 3 4 NA 6 7 NA 9 10