'convert df into a pairwise distance matrix

I'm using code (not my own) that converts a data frame (Mol.Info) of mass spec peaks and associated elemental ratios into a pairwise distance matrix between mass peaks. The code works beautifully, but I don't understand how it works. I see that the row.names = row.names(Mol.Info) will ensure the new data frame will contain the same row names, but in the hopes of understanding code better, could someone break down the line for me, apply(Mol.Info, 2, scale)?

# Pairwise distance between peaks
Mol.Info = as.data.frame(apply(Mol.Info, 2, scale), row.names = row.names(Mol.Info))

> dput(head(Mol.Info))
structure(list(C = c(7L, 4L, 8L, 8L, 9L, 7L), H = c(7L, 12L, 
11L, 11L, 7L, 11L), O = c(4L, 4L, 5L, 5L, 1L, 4L), N = c(1L, 
1L, 1L, 1L, 5L, 3L), S = c(1L, 1L, 0L, 0L, 0L, 0L), P = c(0L, 
1L, 0L, 0L, 0L, 0L), DBE = c(5L, 0L, 4L, 4L, 9L, 4L), AI_Mod = c(0.333333333, 
0, 0.111111111, 0.111111111, 1, 0), kdefect.CH2 = c(0.221048311, 
0.208231648, 0.166968949, 0.16684561, 0.165547015, 0.155639304
)), row.names = c("200.002276", "200.015107", "200.0564158", 
"200.0565393", "200.0578394", "200.0677581"), class = "data.frame")
r


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source