Learn R Programming

pedometrics (version 0.12.1)

cont2cat: Categorize/stratify numerical variable(s)

Description

Create break points, compute strata proportions, and stratify numerical variable(s) to create categorical variable(s).

Usage

cont2cat(x, breaks, integer = FALSE)

breakPoints(x, n, type = "area", prop = FALSE)

stratify(x, n, type = "area", integer = FALSE)

Arguments

x

Vector, data frame or matrix with data on the numerical variable(s) to be categorized/stratified.

breaks

Vector or list containing the lower and upper limits that should be used to break the numerical variable(s) into categories. See ‘Details’ for more information.

integer

Logical value indicating if the categorical variable(s) should be returned as integers. Defaults to integer = FALSE, i.e. the variable(s) will be returned as factors.

n

Integer value indicating the number of categories/strata that should be created.

type

Character value indicating the type of categories/strata that should be used, with options "area" (default), for equal-area, and "range", for equal-range strata.

prop

Logical value indicating if the strata proportions should be returned? Defaults to prop = FALSE.

Value

A vector, data frame, or matrix, depending on the class of x.

Dependencies

The SpatialTools package, provider of tools for spatial data analysis in R, is required for breakPoints() and stratify() to work. The development version of the SpatialTools package is available on https://github.com/jfrench/SpatialTools while its old versions are available on the CRAN archive at https://cran.r-project.org/src/contrib/Archive/SpatialTools/.

Reverse dependencies

The spsann package, provider of methods for the optimization of sample configurations using spatial simulated annealing in R, requires breakPoints(), cont2cat() and stratify() for some of its functions to work. The development version of the spsann package is available on https://github.com/Laboratorio-de-Pedometria/spsann-package.

Details

Argument breaks must be a vector if x is a vector, but a list if x is a data frame or matrix. Using a list allows breaking each column of x into different number of categories.

References

B. Minasny and A. B. McBratney. A conditioned Latin hypercube method for sampling in the presence of ancillary information. Computers & Geosciences, vol. 32, no. 9, pp. 1378<U+2013>1388, Nov. 2006, doi: 10.1016/j.cageo.2005.12.009.

T. Hengl, D. G. Rossiter, and A. Stein. Soil sampling strategies for spatial prediction by correlation with auxiliary maps. Australian Journal of Soil Research, vol. 41, no. 8, pp. 1403<U+2013>1422, 2003, doi: 10.1071/SR03005.

Examples

Run this code
# NOT RUN {
if (require(SpatialTools)) {
  ## Compute the break points of marginal strata
  x <- data.frame(x = round(rnorm(10), 1), y = round(rlnorm(10), 1))
  x <- breakPoints(x = x, n = 4, type = "area", prop = TRUE)

  ## Convert numerical data into categorical data
  # Matrix
  x <- y <- c(1:10)
  x <- cbind(x, y)
  breaks <- list(c(1, 2, 4, 8, 10), c(1, 5, 10))
  y <- cont2cat(x, breaks)

  # Data frame
  x <- y <- c(1:10)
  x <- data.frame(x, y)
  breaks <- list(c(1, 2, 4, 8, 10), c(1, 5, 10))
  y <- cont2cat(x, breaks, integer = TRUE)

  # Vector
  x <- c(1:10)
  breaks <- c(1, 2, 4, 8, 10)
  y <- cont2cat(x, breaks, integer = TRUE)

  ## Stratification
  x <- data.frame(x = round(rlnorm(10), 1), y = round(rnorm(10), 1))
  x <- stratify(x = x, n = 4, type = "area", integer = TRUE)
  x
}
# }

Run the code above in your browser using DataLab