Learn R Programming

rENA (version 0.3.0)

fun_sphere_norm: Row-wise L2 (Sphere) Normalization

Description

Normalizes each row of a numeric dataframe or matrix to have unit L2 norm (Euclidean length). Each row is divided by its own length, projecting all rows onto the unit hypersphere. Useful for analyses where direction is important but magnitude should be removed.

Usage

fun_sphere_norm(dfM)

Value

A numeric matrix with the same dimensions as `dfM`, with each row normalized to unit length (L2 norm = 1), unless the row is all zeros (in which case it remains zeros).

Arguments

dfM

A data.frame or matrix. Each row is treated as a vector to compute its L2 norm.

Details

Row-wise L2 (Sphere) Normalization

This function computes the L2 norm (Euclidean length) of each row and divides the row by this value. Rows with zero length are left unchanged.

Examples

Run this code
df <- data.frame(a = c(3, 4), b = c(0, 0))
fun_sphere_norm(df)

Run the code above in your browser using DataLab