l_scale3D
scales its argument in a variety of ways
used for 3D visualization.
l_scale3D(x, center = TRUE, method = c("box", "sphere"))
x | the matrix or data.frame whose columns are to be scaled.
Any |
---|---|
center | either a logical value or numeric-alike vector of length equal
to the number of columns of |
method | the scaling method to use.
If |
a data.frame whose columns are centred and scaled according to
the given arguments. For method = "sphere")
, the three variable names are
x1
, x2
, and x3
.
##### Iris data # # All variables (including Species as a factor) result_box <- l_scale3D(iris) head(result_box, n = 3)#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> 1 -0.2777778 0.12500000 -0.4322034 -0.4583333 -0.5 #> 2 -0.3333333 -0.08333333 -0.4322034 -0.4583333 -0.5 #> 3 -0.3888889 0.00000000 -0.4491525 -0.4583333 -0.5#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> [1,] -0.5 -0.5 -0.5 -0.5 -0.5 #> [2,] 0.5 0.5 0.5 0.5 0.5#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> -0.07129630 -0.05944444 -0.03254237 -0.04194444 0.00000000# Sphering only on 3D data. result_sphere <- l_scale3D(iris[, 1:3], method = "sphere") head(result_sphere, n = 3)#> x1 x2 x3 #> 1 -0.10665359 -0.03635358 -0.039744517 #> 2 -0.09153699 0.06124692 -0.080344512 #> 3 -0.11208702 0.03517574 -0.009633385#> x1 x2 x3 #> [1,] -0.1423020 -0.2338372 -0.1627308 #> [2,] 0.1737036 0.2262592 0.2203348#> x1 x2 x3 #> -2.074395e-17 3.842760e-18 1.056550e-16# With NAs x <- iris x[c(1, 3), 1] <- NA x[2, 3] <- NA result_box <- l_scale3D(x) head(result_box, n = 5)#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> 1 NA 0.12500000 -0.4322034 -0.4583333 -0.5 #> 2 -0.3333333 -0.08333333 NA -0.4583333 -0.5 #> 3 NA 0.00000000 -0.4491525 -0.4583333 -0.5 #> 4 -0.4166667 -0.04166667 -0.4152542 -0.4583333 -0.5 #> 5 -0.3055556 0.16666667 -0.4322034 -0.4583333 -0.5#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> [1,] -0.5 -0.5 -0.5 -0.5 -0.5 #> [2,] 0.5 0.5 0.5 0.5 0.5# Sphering only on 3D data. result_sphere <- l_scale3D(x[, 1:3], method = "sphere") # Rows having had any NA are all NA after sphering. head(result_sphere, n = 5)#> x1 x2 x3 #> 1 NA NA NA #> 2 NA NA NA #> 3 NA NA NA #> 4 -0.1097604 -0.05847447 0.021141688 #> 5 -0.1188225 0.04803699 0.001622979# Note with NAs mean is no longer numerically zero. # because centring was based on all non-NAs in each column apply(result_sphere, 2, FUN = function(x) {mean(x, na.rm = TRUE)})#> x1 x2 x3 #> 0.0012171739 -0.0002142758 0.0019700846