View source: R/extract_from_factor.R
extract.from.factor | R Documentation |
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.
extract.from.factor(factor_vector, factor_levels, x = NA, output)
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 |
output |
Optional. The type of output of the function (default is factor if missing x). See value section below. |
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
.
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")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.