在purrr包中的R中找不到函数Split_by

时间:2017-10-06 17:14:33

标签: r purrr

我记得在包purrr之前使用此函数split_by。现在,当我尝试访问它时,它说无法找到连接点Split_by。我尝试在包purrr上做一个ls,但我找不到该函数。是否有任何替代方案可用于包装中的目的?

2 个答案:

答案 0 :(得分:0)

版本0.2.3中不推荐使用

split_by - 请参阅the release notes

该功能现在位于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"

答案 1 :(得分:0)

请参阅此处查看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()冲突,完整的家庭感觉不到   那很有用。改为使用tibbles(#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()

使用tibbles

我理解“使用tibbles”这个方向:

您的列表中有多个项目共享相同的结构,因此列表不是合适的结构,您可以转换为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