extract.from.factor: Extract a vector with some levels of an initial factor vector

View source: R/extract_from_factor.R

extract.from.factorR Documentation

Extract a vector with some levels of an initial factor vector

Description

The main goal of this function is to return a subset or a vector able to subset a data vector linked to a factor vector according to some levels of this factor vector.

Usage

extract.from.factor(factor_vector, factor_levels, x = NA, output)

Arguments

factor_vector

The initial factor vector

factor_levels

The vector containing the levels to extract

x

Optional. The data (of any kind) whose values are to be extracted from the factor_vector

output

Optional. The type of output of the function (default is factor if missing x). See value section below.

Value

If output is "factor" (the default condition), the function will return a shorter factor vector with the levels specified in the factor_levels argument only. If output is "logical", the function will return a logical vector of same length than the factor_vector specifying if each level is equal to the specified factor_levels. This can be especially useful when subsetting a vector according to multiple factor with (optionally) multiple levels for each. If output is "data", the function will return the data in x if the levels of the factor_vector of the same location are aqual to the specified factor_levels.

Examples

factor_vector<-as.factor(c(rep("A",10),rep("B",5),rep("A",4),rep("C",6)))
factor_levels<-c("B","C")
x<-c(rep(10,10),rep(5,5),rep(11,4),rep(9,6))

extract.from.factor(factor_vector,factor_levels,output="factor")
extract.from.factor(factor_vector,factor_levels,output="logical")
extract.from.factor(factor_vector,factor_levels,x,output="data")


jacobmaugoust/ULT documentation built on May 16, 2023, 1:29 p.m.