11 自定义函数

函数功能使我们尽可能避免复制粘贴代码,而且需要更改的时候不需要大面积修改代码仅需要调整函数参数,使代码整体更加模块化.

假设有工作任务需要给商品SKU排名,在代码中需要重复以下代码5次,当区间需要修改的时候就是灾难.

原始代码示例如下:

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

当然以上的函数本质是用base R中cut():

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]

参考资料