README.md

powers

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.

Description & Examples

In this section I will explain the arguments of each function and give some examples for each function.

First, load my powers package.

library(powers)

(1) The function square(x,na.omit) squares a vector.

num_vec <- c(0, -4.6, 3.4, NA)
square(num_vec)
#> [1]  0.00 21.16 11.56    NA

square(num_vec, na.omit=TRUE)
#> [1]  0.00 21.16 11.56

num_vec2 <- c(0, -4.6, 3.4, "a")
square(num_vec2)
#> [1] "invalid input"

(2) The functioncube(x,na.omit) raises a vector to the power of 3.

num_vec <- c(0, -4.6, 3.4, NA)
cube(num_vec)
#> [1]   0.000 -97.336  39.304      NA

cube(num_vec, na.omit=TRUE)
#> [1]   0.000 -97.336  39.304

num_vec2 <- c(0, -4.6, 3.4, "a")
cube(num_vec2)
#> [1] "invalid input"

(3) The functionbox_cox(y, lambda1, lambda2, na.omit) calculate the box cox transform of a vector. Here is the formula of the transformation: cox box (formula source: wikipedia)

num_vec <- c(0, -4.6, 3.4, NA)
box_cox(num_vec,4,6) # not omit NA
#> [1] -0.2500  0.7104 33.1584      NA

box_cox(num_vec,4,6,na.omit=TRUE) # omit NA
#> [1] -0.2500  0.7104 33.1584

box_cox(num_vec,4,2,na.omit=TRUE) # lambda2 too small
#> [1] "invalid value of lambda2"

box_cox(num_vec,6,2,na.omit=TRUE) # lambda1 >5
#> [1] "invalid value of lambda1"

num_vec2 <- c(0, -4.6, 3.4, "a")
box_cox(num_vec2,4,6)
#> [1] "invalid input"

(4) The functionmy_log(x,base=exp(1),na.omit=FALSE) perform log transformation.

my_log(exp(7))
#> [1] 7
num_vec <- c(0, 8,64,NA)
my_log(num_vec,2) # not omit NA
#> [1] -Inf    3    6   NA

my_log(num_vec,2, na.omit = TRUE) # omit NA
#> [1] -Inf    3    6

num_vec1 <- c(0, 4.6, 900000000000000000)
my_log(num_vec1,1.5) # can not calculate x > base^100
#> [1] "can not calculate"

my_log(-10,2) # x can not be negative
#> [1] "x needs to be greater than or equal to 0"

num_vec2 <- c(0, -4.6, 3.4, "a")
my_log(num_vec2,4)
#> [1] "invalid input"


hannahdxz/powers documentation built on May 29, 2019, 12:04 p.m.