# 11 自定义函数

library(tidyverse)
num <- sample(1:1000,1000)
res1 <- if_else(num <= 50,"1-50",
if_else(num <= 100,"51-100",
if_else(num <= 150,"101-150",
if_else(num <= 200 ,"151-200",
if_else(num >200,"200以上",'其他')))))

# same above
# case_when(num <= 50 ~ '1-50',
#           num <= 100 ~ '51-100',
#           num <= 150 ~ '101-150',
#           num <= 200 ~ '151-200',
#           num > 100 ~ '200以上'
#           )

# 个人倾向data.table
# data.table::fifelse()
# data.table::fcase() 是sql中case when的实现


cut_function <- function(vecto,x,n){
vec <- c(0)
for(i in 1:n){
kong <-  i*x
vec <- c(vec,kong)
}
vec <- c(vec,Inf)
labels <- c()
j <- 1

while (j<=n) {
labels[j] <- str_c(vec[j]+1,"-",vec[j+1])
j <- j+1
}
labels <- c(labels,paste0(vec[j],'以上'))
res <- cut(x = vecto,breaks = vec,labels = labels) %>% as.character()
}

res2 <- cut_function(num,50,4)

identical(res1,res2)
#> [1] TRUE

cut(1:201,breaks = c(0,50,100,150,200,Inf))
#>   [1] (0,50]    (0,50]    (0,50]    (0,50]    (0,50]    (0,50]    (0,50]
#>   [8] (0,50]    (0,50]    (0,50]    (0,50]    (0,50]    (0,50]    (0,50]
#>  [15] (0,50]    (0,50]    (0,50]    (0,50]    (0,50]    (0,50]    (0,50]
#>  [22] (0,50]    (0,50]    (0,50]    (0,50]    (0,50]    (0,50]    (0,50]
#>  [29] (0,50]    (0,50]    (0,50]    (0,50]    (0,50]    (0,50]    (0,50]
#>  [36] (0,50]    (0,50]    (0,50]    (0,50]    (0,50]    (0,50]    (0,50]
#>  [43] (0,50]    (0,50]    (0,50]    (0,50]    (0,50]    (0,50]    (0,50]
#>  [50] (0,50]    (50,100]  (50,100]  (50,100]  (50,100]  (50,100]  (50,100]
#>  [57] (50,100]  (50,100]  (50,100]  (50,100]  (50,100]  (50,100]  (50,100]
#>  [64] (50,100]  (50,100]  (50,100]  (50,100]  (50,100]  (50,100]  (50,100]
#>  [71] (50,100]  (50,100]  (50,100]  (50,100]  (50,100]  (50,100]  (50,100]
#>  [78] (50,100]  (50,100]  (50,100]  (50,100]  (50,100]  (50,100]  (50,100]
#>  [85] (50,100]  (50,100]  (50,100]  (50,100]  (50,100]  (50,100]  (50,100]
#>  [92] (50,100]  (50,100]  (50,100]  (50,100]  (50,100]  (50,100]  (50,100]
#>  [99] (50,100]  (50,100]  (100,150] (100,150] (100,150] (100,150] (100,150]
#> [106] (100,150] (100,150] (100,150] (100,150] (100,150] (100,150] (100,150]
#> [113] (100,150] (100,150] (100,150] (100,150] (100,150] (100,150] (100,150]
#> [120] (100,150] (100,150] (100,150] (100,150] (100,150] (100,150] (100,150]
#> [127] (100,150] (100,150] (100,150] (100,150] (100,150] (100,150] (100,150]
#> [134] (100,150] (100,150] (100,150] (100,150] (100,150] (100,150] (100,150]
#> [141] (100,150] (100,150] (100,150] (100,150] (100,150] (100,150] (100,150]
#> [148] (100,150] (100,150] (100,150] (150,200] (150,200] (150,200] (150,200]
#> [155] (150,200] (150,200] (150,200] (150,200] (150,200] (150,200] (150,200]
#> [162] (150,200] (150,200] (150,200] (150,200] (150,200] (150,200] (150,200]
#> [169] (150,200] (150,200] (150,200] (150,200] (150,200] (150,200] (150,200]
#> [176] (150,200] (150,200] (150,200] (150,200] (150,200] (150,200] (150,200]
#> [183] (150,200] (150,200] (150,200] (150,200] (150,200] (150,200] (150,200]
#> [190] (150,200] (150,200] (150,200] (150,200] (150,200] (150,200] (150,200]
#> [197] (150,200] (150,200] (150,200] (150,200] (200,Inf]
#> Levels: (0,50] (50,100] (100,150] (150,200] (200,Inf]