![跳跃猫引导关注](https://img-blog.csdnimg.cn/img_convert/63621e15b2a782acfe2b25e3e79d5c5f.gif&wxfrom=5&wx_lazy=1)
作者:邬书豪,车联网数据挖掘工程师 ,人工智能爱好者社区负责人。
微信ID:tsaiedu
知乎专栏:https://www.zhihu.com/people/wu-shu-hao-67/activities
![分割线](https://img-blog.csdnimg.cn/img_convert/eaa77fe7b10809eae0e29bfa0bd477f6.png;wxfrom=5&wx_lazy=1)
往期回顾:
head_while() 找到所有满足头部匹配的值
tail_while() 找到所有满足尾部匹配的值
mtcars[1,]
mtcars[1,] %>% head_while(~. >5)
mtcars[1,] %>% head_while(~. >15)
mtcars[,1]
mtcars[,1] %>% tail_while(~. >25)
mtcars[,1] %>% tail_while(~. >21)
fun <- function(x) x >= 1
head_while(5:-5, fun)
head_while(-1:-5, fun)
x <- sample(10)
x
x %>% head_while(~. >5)
x %>% tail_while(~. >5)
transpose() 将列表对变成列表/将一个列表对成一个清单
mtcars %>% transpose()
mtcars %>% transpose() %>%simplify_all()
x <- list(list(a=1,b=2),list(a=3,b=4))
x
x %>% transpose()
x %>% transpose() %>%simplify_all()
ll <- list(
list(x = 1, y = "one"),
list(z = "deux", x = 2)
)
ll %>% transpose()
ll %>% transpose() %>%simplify_all()
keep() 使用函数保留元素
discard() 使用函数放弃元素 保存于放弃是对立的
head(mtcars)
mtcars[1:5,] %>% keep(function(x) mean(x) > 10)
mtcars[1:5,] %>% keep(function(x) mean(x) < 10)
mtcars[1:5,] %>% discard(function(x) mean(x) < 10)
mtcars[1:5,] %>% discard(function(x) mean(x) > 10)
rep(3, 2) %>% # rep(a,time=)
map(rnorm, n=5) %>% #每行个数
keep(function(x) mean(x) > 2)
rep(3, 2) %>% # rep(a,time=)
map(rnorm, n=5) %>% #每行个数
discard(function(x) mean(x) >4)
safely #函数包装功能,不通过打印输出消息,警告和错误
# safely() 返回一个带有组件结果和错误的列表
# quietly() 返回一个包含组件结果/输出/警告和消息
# possibly() 包装函数在发生错误时使用默认值(否则)
fun_log <- safely(log) #log
str(fun_log(10))
list("a",100,9) %>% map(fun_log)
list(99,100,9) %>% map(fun_log) %>% transpose()
fun_log(mtcars[1,])
mean_fun <-quietly(mean) #mean
mean_fun(c(1,3)) %>% transpose()
mean_fun(mtcars[,1])
mean(mtcars[,1])
x <- list(1, 10, "a") #log
x %>% map_dbl(possibly(log, NA_real_)) # otherwise
walk 无形的返回第一个参数,经常与管道函数并用
# walk()
# pwalk()
# walk2()
x <- list(1, "a", 3)
x %>% walk(print)
往期精彩内容整理合集:
相关课程推荐
Kaggle十大案例精讲(连载中)
☟☟☟ 猛戳阅读原文,即刻加入课程。