R手册(Syntax)--purrr

purrr : A functional programming(FP) toolkit for R

Apply Functions

map(.x, .f, ...) Apply a function to each element of a list or vector
map2(.x,.y,.f,…) Apply a function to pairs of elements from two lists
pmap(.x, .f, ...) Apply a function to groups of elements from list of lists
imap(.x, .f, ...) Apply a function to each element of a vector, and its index
invoke_map(.f, .x = list(NULL) ) 调用不同的函数
walk(.x, .f), walk2(), pwalk() 并行处理

map_if(.x, .p, .f, ...) map_at(.x, .at, .f, ...) 其他系列同map

Parameters:

.f : a function, formula, or atomic vector
: 其他参数(.x为没有被...指定的第一个参数)

.f 参数:

functionas.character %>% map(iris)

formula~ .xbecomes function(x)
map(l, ~ 2 +.x)
~ .x .y becomes function(.x, .y), e.g.
character
map(x,"y")becomes x[["y"]]
map(x,c("a","b")) becomes x[["a"]][["b"]]
integer
map(x,2) becomes x[[2]]

Output

functionreturns
maplist
map_chrcharacter vector
map_dbldouble (numeric) vector
map_dfdata frame(auto)
map_dfcdata frame (column bind)
map_dfrdata frame (row bind)
map_intinteger vector
map_lgllogical vector
walk返回指定的类型

map2, pmap, imap, invoke_map 系列函数同map

Examples:

1:10 %>%
 map(rnorm, n = 10) %>%
 map_dbl(mean)
 
tribble(mean=c(5,10,3),sd=c(1,5,3),n=c(2,3,8)%>%
  pmap(rnorm) #生成三组不同参数的正态分布

df <- tibble(
  f = c("runif", "rpois", "rnorm"),
  params = list(
    list(n = 10),
    list(n = 5, lambda = 10),
    list(n = 10, mean = -3, sd = 10))
)
invoke_map(df$f, df$params)

`list(1,"a",3)%>% walk(print)` #并行打印

Work with Lists

filter lists说明
pluck(.x, …, .default=NULL)按名字或者索引筛选x, pluck(x,"b")
keep(.x, .p, …)
discard(.x, .p, …)
逻辑值筛选keep(x, is.na)
keep的否定结果discard(x, is.na)
compact(.x, .p = identity)删除空元素
head_while(.x, .p, …)
tail_while
找到所有头部/尾部满足匹配的值
logic说明
every(.x, .p, …)
some(.x, .p, …)
列表中的每一个/某些元素是否满足要求(返回一个FALSE/TRUE)
mtcars %>% some(is_numeric)
has_element(.x, .y)列表包含一个元素has_element(x, "foo")
detect(.x, .f, …, .right=FALSE,.p)
detect_index(.x, .f, …, .right= FALSE, .p)
返回第一个判断为TRUE的元素detect(x, is.character)
返回索引
vec_depth(x)Return depth(number of levels of indexes)
reshape lists说明
flatten(.x)将列表降低一个维度
flatten_lgl(), flatten_int(), flatten_dbl(), flatten_chr()
flatten_df,flatten_dfr(),flatten_dfc()
返回向量
返回data.frame
transpose(.list, .names = NULL)转置一个列表
simplify(.x, .type = NULL)列表强制成向量
simplify_all(.x, .type = NULL)
combine说明
append(x, values, afer =length(x))末尾追加append(x, list(d = 1))
prepend(x, values, before =1)起始追加prepend(x, list(d = 1))
splice(…)合并到一个列表

Reduce Lists

递归说明
reduce(.x,.f,…init)将.x的子元素递归,返回单个值
.x列表或原子矢量,
init开始积累的第一个值
reduce_right(.x,.f,…init)从右往左递归
reduce2(.x, .y, .f, …, .init)
reduce2_right(.x, .y, .f, …, .init)
三参数递归
accumulate(.x, .f, …, .init)将.x的子元素递归,返回中间值
accumulate_right(.x, .f, …, .init)从右往左递归
list(data1,data2,data3) %>% 
   reduce(left_join,by = c("first","last"))

Debug

调试说明
safely(.f, otherwise = NULL, quiet = TRUE)return list of results and errors
quietly(.f)return list of results, output, messages, warnings.
possibly(.f, otherwise, quiet = TRUE)continue with default value (instead of error) and return list of results,error
base::stop(message)stop and return message
base::stopifnot(logit1,logit2,logit3…)检查每个参数为TRUE,否则停止执行当前表达式返回message

Work with Tibbles

Nested Data

nest

tidyr::nest(data, ..., .key = data)
For grouped data, moves groups into cells as data frames.

n_iris <- iris %>% group_by(Species) %>% nest()

tidyr::unnest(data, ..., .drop = NA, .id=NULL, .sep=NULL)
Unnests a nested data frame.

n_iris %>% unnest()

List Column Workflow

# step 1 Make a list column
n_iris <- iris %>%
 group_by(Species) %>%
 nest()

# step 2 Work with list columns
mod_fun <- function(df)
 lm(Sepal.Length ~ ., data = df)
m_iris <- n_iris %>%
 mutate(model = map(data, mod_fun))
 
# step 3 Simplify the list column
b_fun <- function(mod)
 coefficients(mod)[[1]]
m_iris %>% transmute(Species,
 beta = map_dbl(model, b_fun))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值