find_max: Utility functions for factors and compositional data

View source: R/find_max.R

find_maxR Documentation

Utility functions for factors and compositional data

Description

Utility functions for factors and compositional data.

Usage

compare_sets(x, y)
find_max(x)
find_min(x)
reclass(x, map, all = FALSE, allow_NA = FALSE)
redistribute(x, source, target = NULL)

Arguments

x, y

any type for compare_sets, matrix for find_max, find_min, and redistribute, a factor for reclass.

map

a reclassification matrix with 2 columns (1st: original levels, 2nd: output levels mapped to original levels).

all

logical, whether all levels from mapping matrix should be applied on the return object.

allow_NA

logical, whether NAs are allowed as part of map.

source

numeric or character, single column index for input matrix x.

target

numeric or character, column index or indices for input matrix x.

Value

A matrix compare_sets.

A data frame for find_max and find_min.

A reclassified factor for reclass.

A matrix for redistribute where the source column values are redistributed among the target columns proportionally.

Author(s)

Peter Solymos <solymos@ualberta.ca>

See Also

intersect, setdiff, union, relevel, reorder

Examples

## numeric vector
compare_sets(1:10, 8:15)
## factor with 'zombie' labels
compare_sets(factor(1:10, levels=1:10), factor(8:15, levels=1:15))

(mat <- matrix(rnorm(10*5), 10, 5))
(m <- find_max(mat))
## column indices
as.integer(m$index)
find_min(mat)

map <- cbind(c("a","b","c","d","e","f","g"),
             c("A","B","B","C","D","D","E"))
#x <- factor(sample(map[1:6,1], 100, replace=TRUE), levels=map[,1])
x <- as.factor(sample(map[1:6,1], 100, replace=TRUE))
x[2] <- NA
table(x, reclass(x, map, all = FALSE), useNA="always")
table(x, reclass(x, map, all = TRUE), useNA="always")

map[c(4, 7), 2] <- NA
table(x, reclass(x, map, all = FALSE, allow_NA = TRUE), useNA="always")
table(x, reclass(x, map, all = TRUE, allow_NA = TRUE), useNA="always")

(mat2 <- exp(mat) / rowSums(exp(mat)))
(rmat2 <- redistribute(mat2, source = 1, target = 2:4))
colMeans(mat2)
colMeans(rmat2)
stopifnot(abs(sum(mat2) - sum(rmat2)) < 10^-6)


mefa4 documentation built on June 22, 2024, 11:01 a.m.