Learn R Programming

fctutils (version 0.0.7)

ft_combine: Combine Two Vectors of Unequal Lengths and Sort Based on Specified Levels

Description

Combines two vectors, which may be of unequal lengths, into a factor vector and sorts based on the levels of either the first or second vector.

Usage

ft_combine(vector1, vector2, sort_by = 1, decreasing = FALSE)

Value

A factor vector combining both vectors and sorted based on specified levels.

Arguments

vector1

The first vector to combine.

vector2

The second vector to combine.

sort_by

An integer (1 or 2) indicating which vector's levels to use for sorting. Default is 1.

decreasing

Logical. Should the sorting be in decreasing order? Default is FALSE.

Examples

Run this code
# Example vectors of unequal lengths
vector1 <- c('apple', 'banana', 'cherry')
vector2 <- c('date', 'fig', 'grape', 'honeydew')

# Combine and sort based on vector1 levels
combined_factor1 <- ft_combine(vector1, vector2, sort_by = 1)
print(combined_factor1)

# Combine and sort based on vector2 levels
combined_factor2 <- ft_combine(vector1, vector2, sort_by = 2)
print(combined_factor2)

# Combine with decreasing order based on vector1
combined_factor3 <- ft_combine(vector1, vector2, sort_by = 1, decreasing = TRUE)
print(combined_factor3)

Run the code above in your browser using DataLab