# #设置,包括= FALSE ----------------------------------------------------- knitr: opts_chunk美元集(echo = TRUE)图书馆(purrr)图书馆(dplyr)图书馆(tidyr)图书馆(ggplot2)库(CellBench ) ## ----------------------------------------------------------------------------- 库(CellBench)库(purrr) #函数数量提高到一个权力战俘< -函数(x, n) {x ^ n} pow2 < -部分(战俘,n = 2) pow3 < -部分(战俘,n = 3) pow2 (2) pow3 (2 ) ## ---- eval = FALSE ------------------------------------------------------------ # pow2 < -函数(x) {# x ^ 2 #} # # pow3 < -函数(x) {# x ^ 3 # } ## ---- eval = FALSE ------------------------------------------------------------ # 战俘< -函数(x, n) {# x ^ n #} # # pow2 < -函数(x){#战俘(x, 2 ) # } # # pow3 < -函数(x){#战俘(x,3 ) # } ## ----------------------------------------------------------------------------- # 发现的最大绝对值max_absolute < -组成(max,abs) max_absolute (rnorm (100 )) ## ---- eval = FALSE ------------------------------------------------------------ # method1 < - x (x){#函数< -正常化(x) # method_func1 (x ) # } # # method2 < -函数(x) {# method_func2 (x ) # } # # method3 < - x (x){#函数< -正常化(x) # method_func3 (x ) # } ## ---- eval = FALSE ------------------------------------------------------------ # # 身份只是返回它的参数,在这里有用代码一致性# method1 < -组成(method_func1,正常化)# method2 < - <组成(method_func2身份)# method3组成(method_func3正常化 ) ## ----------------------------------------------------------------------------- x < -列表(1、2、3)地图(x, x (x){函数* 2 }) ## ----------------------------------------------------------------------------- # 列表不同分布的随机值x < -列表(rpois(100年,λ= 5)rpois(100年,λ= 5)rgamma(100年,形状= 5)rgamma(100年Shape = 5)) #附加参数列表y <- list("mean", "median", "mean", "median") #接受值和一个模式参数中心性的函数<- function(x, mode = c("mean", "median")) {mode <- match.arg(mode) if (mode == "mean") {mean = mean(x)} else if (mode == "median") {median = median(x)}} #使用map2将函数应用到两个列表map2(x, y, "中心 ) ## ----------------------------------------------------------------------------- 库(dplyr) #列表数据的数据集< - (set2 =中的rnorm(500年,意味着= 2,sd = 1),关于我校= rnorm(500年,意味着= 1,sd = 2)) #列表函数add_noise < -(=没有身份,add_bias =函数(x) {x + 1}) res < apply_methods(数据集,add_noise)类(res) res ## ----------------------------------------------------------------------------- # 过滤只行数据集1 res % > %过滤器(data = =“set2”中的)#过滤只行add_bias方法res % > %过滤器(add_noise = =“add_bias”)#变异数据列预谋“数据”数据集名称res % > %变异(数据= paste0(“数据”,数据 )) ## ----------------------------------------------------------------------------- 规< -列表(意味着=的意思是,中位数=中位数)#简单地应用指标结果在单列res %>% apply_methods(metric) #跨列传播指标res %>% apply_methods(metric) %>% spread(metric,结果)## -----------------------------------------------------------------------------库(tidyr)库(ggplot2) #我更喜欢我自己的主题为ggplot2,下面的主题代码是可选的theme_set(theme_bw() +主题(plot。Title = element_text(face = "plain", size = rel(20/12), hjust = 1/2, margin = margin(t = 10, b = 20)),轴。Text = element_text(size = rel(14/12)), strip.text.x = element_text(size = rel(16/12)),轴。Title = element_text(size = rel(16/12)))) scale_color_discrete <- function(…)scale_color_brewer(…),调色板="Set1") scale_fill_discrete <- function(…) , palette="Set1") ## ----------------------------------------------------------------------------- # pipeline collapse constructs a single string from the pipeline steps, # unnest expands the list-column of results, transforming the result # into a flat table. collapsed_res <- pipeline_collapse(res) %>% unnest() ggplot(collapsed_res, aes(x = pipeline, y = result)) + geom_boxplot() ## ----------------------------------------------------------------------------- # remember that we have to unnest the data before it's appropriate # for plotting ggplot(unnest(res), aes(x = add_noise, y = result)) + geom_boxplot() + facet_grid(~data)