• 遍历所有文件

      1. list.files(root_dir)
    • 字符串和变量名的转化

      1. # 取字符串名称的变量
      2. get(varible_name)
      3. # 取变量的名称为字符串
      4. as.character(substitute(variable_name))
      5. # 用字符串新定义变量
      6. assign(character_name, varible_content)
    • 多个变量拼接的输出方式

      1. sprintf("指标激活率:%f", length(idx_act) / length(coef_all))
    • mutate中字符串相关操作

      1. # mutate 中切分字符串
      2. # 更多方法见:https://stackoverflow.com/questions/42565539/using-strsplit-and-subset-in-dplyr-and-mutate
      3. us_tmp %>%
      4. mutate(sub_label = sapply(str_split(variable_name, "\\."), function(x) x[1]))
    • 批量加载包的简单写法

      1. library(pacman)
      2. pacman::p_load(R451talk, dplyr, stringr, reshape2, corrplot, ggplot2,
      3. plotly, knitr, data.table, Hmisc, kableExtra, Matrix,
      4. caret, glmnet, xlsx)
    • &和&&区别

      1. # https://stackoverflow.com/questions/23316161/the-condition-has-length-1-and-only-the-first-element-will-be-used
      2. c(TRUE, TRUE) & c(TRUE, FALSE)
      3. # [1] TRUE FALSE
      4. # c(TRUE, TRUE) && c(TRUE, FALSE)
      5. [1] TRUE
    • 找 Rscripts 路径

      1. R.home()