knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "README-" )
The goal of the powers
package is to perform basic computations on numerical vectors.
This package has four exported functions: square()
, cube()
, box_cox()
and my_log()
.
There is one internal function pow()
which these four functions depend on.
These four functions only work for numerical input. When the input is of other type, an "invalid input" message will be returned.
First, load my powers
package.
library(powers)
(1) The function square(x,na.omit)
squares a vector.
x
is the vector to be squared.
na.omit
is whether or not to remove NAs, the default is FALSE.
num_vec <- c(0, -4.6, 3.4, NA) square(num_vec) square(num_vec, na.omit=TRUE) num_vec2 <- c(0, -4.6, 3.4, "a") square(num_vec2)
(2) The functioncube(x,na.omit)
raises a vector to the power of 3.
x
is the vector to be cubed.
na.omit
is whether or not to remove NAs, the default is FALSE.
num_vec <- c(0, -4.6, 3.4, NA) cube(num_vec) cube(num_vec, na.omit=TRUE) num_vec2 <- c(0, -4.6, 3.4, "a") cube(num_vec2)
(3) The functionbox_cox(y, lambda1, lambda2, na.omit)
calculate the box cox transform of a vector. Here is the formula of the transformation:
(formula source: wikipedia)
y
is the vector to be transformed.
lambda1
is the first parameter of the transformation, range from -5 to 5.
lambda2
is the second parameter of the transformation, lambda2 is introduced for negative
values of y, it must be greater than -y.
na.omit
is whether or not to remove NAs, the default is FALSE.
num_vec <- c(0, -4.6, 3.4, NA) box_cox(num_vec,4,6) # not omit NA box_cox(num_vec,4,6,na.omit=TRUE) # omit NA box_cox(num_vec,4,2,na.omit=TRUE) # lambda2 too small box_cox(num_vec,6,2,na.omit=TRUE) # lambda1 >5 num_vec2 <- c(0, -4.6, 3.4, "a") box_cox(num_vec2,4,6)
(4) The functionmy_log(x,base=exp(1),na.omit=FALSE)
perform log transformation.
x
is the vector to be transformed. x>=0
base
is the base of the log transformation, the default is exp(1)
na.omit
is whether or not to remove NAs, the default is FALSE.
Note: This function can only calculate the log transformation of x in the range: base^(-100) <= x <= base^100
my_log(exp(7)) num_vec <- c(0, 8,64,NA) my_log(num_vec,2) # not omit NA my_log(num_vec,2, na.omit = TRUE) # omit NA num_vec1 <- c(0, 4.6, 900000000000000000) my_log(num_vec1,1.5) # can not calculate x > base^100 my_log(-10,2) # x can not be negative num_vec2 <- c(0, -4.6, 3.4, "a") my_log(num_vec2,4)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.