Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

category_vector

Category vector


Description

Convert an object containing binary (integer) fields (columns) into a integer vector indicating the column index where each row is 1.

Usage

category_vector(x)

## S3 method for class 'data.frame'
category_vector(x)

## S3 method for class 'sf'
category_vector(x)

## S3 method for class 'Spatial'
category_vector(x)

## S3 method for class 'matrix'
category_vector(x)

Arguments

x

matrix, data.frame, Spatial, or sf::sf() object.

Details

This function is conceptually similar to base::max.col() except that rows with no values equal to 1 values are assigned a value of zero. Also, note that in the argument to x, each row must contain only a single value equal to 1.

Value

integer vector

See Also

Examples

# create matrix with logical fields
x <- matrix(c(1, 0, 0, NA, 0, 1, 0, NA, 0, 0, 0, NA), ncol = 3)

# print matrix
print(x)

# convert to category vector
y <- category_vector(x)

# print category vector
print(y)

prioritizr

Systematic Conservation Prioritization in R

v7.0.1
GPL-3
Authors
Jeffrey O Hanson [aut] (<https://orcid.org/0000-0002-4716-6134>), Richard Schuster [aut, cre] (<https://orcid.org/0000-0003-3191-7869>), Nina Morrell [aut], Matthew Strimas-Mackey [aut] (<https://orcid.org/0000-0001-8929-7776>), Matthew E Watts [aut], Peter Arcese [aut] (<https://orcid.org/0000-0002-8097-482X>), Joseph Bennett [aut] (<https://orcid.org/0000-0002-3901-9513>), Hugh P Possingham [aut] (<https://orcid.org/0000-0001-7755-996X>)
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.