Learn R Programming

loon (version 1.4.1)

l_scale3D: Scale for 3d plotting

Description

l_scale3D scales its argument in a variety of ways used for 3D visualization.

Usage

l_scale3D(x, center = TRUE, method = c("box", "sphere"))

Value

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.

Arguments

x

the matrix or data.frame whose columns are to be scaled. Any NA entries will be preserved but ignored in calculations. x must have exactly 3 columns for method = "sphere".

center

either a logical value or numeric-alike vector of length equal to the number of columns of x, where ‘numeric-alike’ means that as.numeric(.) will be applied successfully if is.numeric(.) is not true.

method

the scaling method to use. If method = "box" (the default) then the columns are scaled to have equal ranges and, when center = TRUE, to be centred by the average of the min and max; If method = "sphere" then x must be three dimensional. For sphering, on each of the original 3 dimensions x is first centred (mean centred when center = TRUE) and scaled to equal standard deviation on. The V matrix of the singular value decomposition (svd) is applied to the right resulting in uncorrelated variables. Coordinates are then divided by (non-zero as tested by !all.equal(0, .)) singular values. If x contains no NAs, the resulting coordinates are simply the U matrix of the svd.

See Also

l_plot3D, scale, and prcomp.

Other three-dimensional plotting functions: l_plot3D()

Examples

Run this code

##### Iris data
#
# All variables (including Species as a factor)
result_box <- l_scale3D(iris)
head(result_box, n = 3)
apply(result_box, 2, FUN = range)
# Note mean is not zero.
apply(result_box, 2, FUN = mean)


# Sphering only on 3D data.
result_sphere <- l_scale3D(iris[, 1:3], method = "sphere")
head(result_sphere, n = 3)
apply(result_sphere, 2, FUN = range)
# Note mean is numerically zero.
apply(result_sphere, 2, FUN = mean)


#  With NAs
x <- iris
x[c(1, 3), 1] <- NA
x[2, 3] <- NA

result_box <- l_scale3D(x)
head(result_box, n = 5)
apply(result_box, 2, FUN = function(x) {range(x, na.rm = TRUE)})

# 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)
# 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)})


Run the code above in your browser using DataLab