[英]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.