Chapter 10 Functions

  • return
  • invisible
  • switch
  • cut
#when we use function?
df <- tibble::tibble(
  a=rnorm(10),
  b=rnorm(10),
  c=rnorm(10),
  d=rnorm(10)
)
df
## # A tibble: 10 x 4
##         a      b        c      d
##     <dbl>  <dbl>    <dbl>  <dbl>
##  1 -2.06  -1.77   0.880   -0.624
##  2 -0.376  0.183 -1.48     1.88 
##  3  0.976 -0.793 -0.851    0.813
##  4  0.841 -1.13  -0.449    1.03 
##  5 -0.943  1.58   0.954    0.290
##  6 -0.692 -1.15  -0.0912  -0.870
##  7  0.329 -1.73  -1.10    -1.48 
##  8  0.633  1.86  -0.00422 -0.890
##  9  0.778  0.766 -0.342   -0.772
## 10 -0.168  0.143  1.63    -0.907
#rescale each column to have a range from 0 to 1
(df$a-min(df$a, na.rm = T))/
  (max(df$a, na.rm = T)-min(df$a, na.rm = T))
##  [1] 0.0000000 0.5552430 1.0000000 0.9555182 0.3689344 0.4515468 0.7871137 0.8871371 0.9346448 0.6238147
df
## # A tibble: 10 x 4
##         a      b        c      d
##     <dbl>  <dbl>    <dbl>  <dbl>
##  1 -2.06  -1.77   0.880   -0.624
##  2 -0.376  0.183 -1.48     1.88 
##  3  0.976 -0.793 -0.851    0.813
##  4  0.841 -1.13  -0.449    1.03 
##  5 -0.943  1.58   0.954    0.290
##  6 -0.692 -1.15  -0.0912  -0.870
##  7  0.329 -1.73  -1.10    -1.48 
##  8  0.633  1.86  -0.00422 -0.890
##  9  0.778  0.766 -0.342   -0.772
## 10 -0.168  0.143  1.63    -0.907
x <- df$a
(x-min(x, na.rm = T))/
  (max(x, na.rm = T)-min(x, na.rm = T))
##  [1] 0.0000000 0.5552430 1.0000000 0.9555182 0.3689344 0.4515468 0.7871137 0.8871371 0.9346448 0.6238147
(rng <- range(x, na.rm=TRUE))
## [1] -2.0649782  0.9763539
(x-rng[1])/(rng[2]-rng[1])
##  [1] 0.0000000 0.5552430 1.0000000 0.9555182 0.3689344 0.4515468 0.7871137 0.8871371 0.9346448 0.6238147
rescale01 <- function(x){
  rng <- range(x, na.rm=T)
  (x-rng[1])/(rng[2]-rng[1])
}
rescale01
## function(x){
##   rng <- range(x, na.rm=T)
##   (x-rng[1])/(rng[2]-rng[1])
## }
## <environment: 0x154b86528>
rescale01(c(0,5,10))
## [1] 0.0 0.5 1.0
rescale01(c(-10, 0, 10))
## [1] 0.0 0.5 1.0
rescale01(c(1,2,3,NA,5))
## [1] 0.00 0.25 0.50   NA 1.00
x <- c(1:10, Inf)
x
##  [1]   1   2   3   4   5   6   7   8   9  10 Inf
rescale01(x)
##  [1]   0   0   0   0   0   0   0   0   0   0 NaN
range(x, finite=T)
## [1]  1 10
rescale01 <- function(x){
  rng <- range(x, na.rm=T, finite=T)
  (x-rng[1])/(rng[2]-rng[1])
}
rescale01(x)
##  [1] 0.0000000 0.1111111 0.2222222 0.3333333 0.4444444 0.5555556 0.6666667 0.7777778 0.8888889 1.0000000       Inf
#conditions

#compute cofidence interval
set.seed(111)
x <- runif(100)
x
##   [1] 0.5929812845 0.7264811215 0.3704220036 0.5149238301 0.3776632159 0.4183373258 0.0106578451 0.5322952422 0.4321606164
##  [10] 0.0936815199 0.5557799137 0.5902284889 0.0671411434 0.0475478533 0.1562025158 0.4464277634 0.1714436871 0.9665342933
##  [19] 0.3106664298 0.6144663957 0.4310607871 0.2855270915 0.3421513471 0.3866276275 0.9675274789 0.3220267275 0.6532294548
##  [28] 0.2833034997 0.7874279192 0.5959206352 0.0585964625 0.5098998600 0.4657924296 0.4693590938 0.3597453721 0.7134103531
##  [37] 0.1163154817 0.7839926207 0.6421407105 0.8051009134 0.6411978584 0.3284916454 0.6356909545 0.9285191579 0.5752422044
##  [46] 0.3666838536 0.4366072204 0.8559219379 0.6279955737 0.7937756432 0.7251648332 0.5850447209 0.0327716474 0.3329946804
##  [55] 0.9967166614 0.5482733699 0.5758329388 0.4563152066 0.0965785654 0.8055401752 0.0009253006 0.4667440471 0.1732608730
##  [64] 0.2592225648 0.9192820815 0.2319295844 0.0525656715 0.3043926249 0.0117258150 0.3007076983 0.8775839461 0.6652787277
##  [73] 0.4537648347 0.0533223320 0.6309068091 0.4421851884 0.2673464869 0.9837744189 0.0951241532 0.7859691235 0.1198521818
##  [82] 0.8812154671 0.1310980669 0.4003378763 0.0866140136 0.3747997992 0.6847860171 0.7347726757 0.7709477365 0.5799853499
##  [91] 0.5110989846 0.8529837073 0.6298211562 0.5790059080 0.7402492894 0.3871497631 0.9935344572 0.3980894811 0.9750010339
## [100] 0.8244822009
#using normal approximation
mean_ci <- function(x, conf=0.95){
  se <- sd(x)/sqrt(length(x))
  alpha <- 1-conf
  mean(x)+se*qnorm(c(alpha/2, 1-alpha/2))
}
mean_ci(x)
## [1] 0.4354841 0.5435638
mean_ci(x, conf = .99)
## [1] 0.4185035 0.5605444
wt_mean <- function(x,w){
  if(length(x) != length(w)){
    stop("'x' and 'w' must be the same legth", call. = T)
  }
  sum(w*x)/sum(x)
}
?stop #call.=F won't return the "systematic" error message
wt_mean(1:3, 1:3)
## [1] 2.333333