# Scale Function - https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/scale

Does anyone know and can guarantee that the scale function does not scramble the data? Where can I find documentation on this? In the function documentation itself there is not.

You can look at the code of the function by entering its name in the console without parentheses. In this case, that just pushes the question to whether the sweep function maintains the order of the data. You can inspect the code of sweep using the same method.
If you want a guarantee, you could add a column to your data that numbers the rows and sort on that column after scaling.

``````scale.default
function (x, center = TRUE, scale = TRUE)
{
x <- as.matrix(x)
nc <- ncol(x)
if (is.logical(center)) {
if (center) {
center <- colMeans(x, na.rm = TRUE)
x <- sweep(x, 2L, center, check.margin = FALSE)
}
}
else {
if (!is.numeric(center))
center <- as.numeric(center)
if (length(center) == nc)
x <- sweep(x, 2L, center, check.margin = FALSE)
else stop("length of 'center' must equal the number of columns of 'x'")
}
if (is.logical(scale)) {
if (scale) {
f <- function(v) {
v <- v[!is.na(v)]
sqrt(sum(v^2)/max(1, length(v) - 1L))
}
scale <- apply(x, 2L, f)
x <- sweep(x, 2L, scale, "/", check.margin = FALSE)
}
}
else {
if (!is.numeric(scale))
scale <- as.numeric(scale)
if (length(scale) == nc)
x <- sweep(x, 2L, scale, "/", check.margin = FALSE)
else stop("length of 'scale' must equal the number of columns of 'x'")
}
if (is.numeric(center))
attr(x, "scaled:center") <- center
if (is.numeric(scale))
attr(x, "scaled:scale") <- scale
x
}
<bytecode: 0x0000016afc9199a8>
<environment: namespace:base>
``````

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.