50% off: Unlimited data and AI learning.
State of Data and AI Literacy Report 2025

featForge (version 0.1.2)

cyclical_cos: Convert a Cyclical Variable to Cosine Representation

Description

This function transforms a cyclic variable (e.g., hour of the day, day of the week, month of the year) into its cosine representation. This transformation ensures that machine learning models respect the cyclical nature of such features.

Usage

cyclical_cos(x, period)

Value

A numeric vector representing the cosine-transformed values.

Arguments

x

A numeric vector representing a cyclic variable (e.g., hour, month, day).

period

A positive numeric scalar representing the period of the cycle. For example, use 24 for hours of the day, 7 for days of the week, and 12 for months.

Details

This function applies the transformation: cos(2πx/period) This encoding ensures that the first and last values in the cycle are smoothly connected.

Examples

Run this code
# Convert hours of the day to cosine encoding
hours <- 0:23
cyclical_cos(hours, 24)

# Convert months of the year to cosine encoding
months <- 1:12
cyclical_cos(months, 12)

Run the code above in your browser using DataLab