[英]Cannot find function Split_by in R in purrr package
我记得以前在包purrr
使用过此函数purrr
。 现在,当我尝试访问它时,它说找不到cpliction Split_by
。 我尝试在purrr
软件包上执行ls,但无法在其中找到该功能。 包装中是否有满足目的的替代品?
split_by
版中不推荐使用split_by
请参见发行说明
该功能现在处于pluck
,但是您可以传递多个参数-来自pluck
文档:
library(purrr)
# pluck() supports integer positions, string names, and functions.
# Using functions, you can easily extend pluck(). Let's create a
# list of data structures:
obj1 <- list("a", list(1, elt = "foobar"))
obj2 <- list("b", list(2, elt = "foobaz"))
x <- list(obj1, obj2)
# And now an accessor for these complex data structures:
my_element <- function(x) x[[2]]$elt
# The accessor can then be passed to pluck:
pluck(x, 1, my_element)
#> [1] "foobar"
pluck(x, 2, my_element)
#> [1] "foobaz"
看到这里purrr 0.2.3
https://cran.r-project.org/web/packages/purrr/news.html
order_by(),sort_by()和split_by()已被删除。 order_by()与dplyr :: order_by()冲突,整个家族并不觉得有用。 请改用小玩意儿(#217)。
这是来自purrr 0.2.2
的原始代码:
split_by <- function(.x, .f, ...) {
vals <- map(.x, .f, ...)
split(.x, simplify_all(transpose(vals)))
}
和原始示例:
l2 <- rerun(5, g = sample(2, 1), y = rdunif(5, 10))
l2 %>% split_by("g") %>% str()
改用小球
我以这种方式理解“使用小贴士”的方向:
您的列表中有几个项目具有相同的结构,因此列表不是适当的结构,您可以转换为tibble
以遵循"one row by observation, one column by variable"
的整洁规则,如上例所示:
t2 <- as_tibble(transpose(l2)) %>% mutate(g=unlist(g))
然后,您可以将其拆分:
split(t2,t2$g)
# $`1`
# # A tibble: 3 x 2
# g y
# <int> <list>
# 1 1 <dbl [5]>
# 2 1 <dbl [5]>
# 3 1 <dbl [5]>
#
# $`2`
# # A tibble: 2 x 2
# g y
# <int> <list>
# 1 2 <dbl [5]>
# 2 2 <dbl [5]>
或使用dplyr::group_by
(并在整洁的原则上保持整洁):
t2 %>% group_by(g) %>% your_code
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.