purrr::possively 函数可能不适用于 map2_chr 函数 [英] purrr::possibly function possibly not working with map2_chr function
问题描述
我怀疑这是 purrr 包中的一个错误,但想先检查我在 StackOverflow 中的逻辑,请.
在我看来,可能
函数在 map2_chr
函数中不起作用.我正在使用 purrr 版本 0.2.5
考虑这个例子:
库(dplyr)图书馆(咕噜咕噜)让 <- tibble(posn = 2:0,let_list = 列表(字母[1:5]、字母[1:5]、字母[1:5]))%>%一瞥()
返回
观察:3变量:2$ posn<int>2, 1, 0$lets_list <list>[<"a", "b", "c", "d", "e">, <"a", "b", "c", "d", "e">, <;"a", "b", "c", "d", "e">]
在此示例中,我想使用 mutate 创建另一列,以根据posn"中的值返回列表lets_list"中的元素.
让%>%变异(lets_sel = map2_chr(lets_list,posn,~.x[.y]))
失败并显示此错误消息,因为第三行的 posn = 0.
<代码>>让 %>%+ 变异(lets_sel = map2_chr(lets_list,posn,~.x[.y]))# mutate_impl(.data, dots) 中的错误:# 评估错误:结果 3 不是长度为 1 的原子向量.
将 possably
函数与 map2_chr
一起使用也会返回错误.
让%>%变异(lets_sel = map2_chr(lets_list,posn,可能(~.x[.y],NA_character_)))# mutate_impl(.data, dots) 中的错误:# 评估错误:结果 3 不是长度为 1 的原子向量.
然而,map2
函数工作正常:
<代码>>让 %>%+ 变异(lets_sel = map2(lets_list,posn,可能(~.x[.y],NA_character_)))# 小费:3 x 3posnlets_listlets_sel<int><列表><列表>1 2 <chr [5]><chr [1]>2 1 <chr [5]><chr [1]>3 0 <chr [5]><chr [0]>
一种解决方法是先使用 map2,然后使用 map_chr,但我怀疑这是一个错误.
<代码>>让 %>%+ mutate(lets_sel = map2(lets_list, posn, ~.x[.y]),+lets_sel = map_chr(lets_sel, 可能(~.x[1], NA_character_)))# 小费:3 x 3posnlets_listlets_sel<int><列表><chr>1 2 <chr [5]>乙2 1 <chr [5]>一种3 0 <chr [5]>不适用
我在这里遗漏了什么吗?谢谢.
possably()
不起作用,因为使用 0
进行索引不会引发错误;它只返回一个长度为 0 的向量:
nth_letter <- function(n) 字母[n]可能(nth_letter,未返回")(0)#>字符(0)第n个字母(0)#>字符(0)
在这种情况下,用 NA
替换无效索引可能会更容易(使用例如 dplyr::na_if()
或普通的旧 ifelse
如果真正的问题更复杂)得到你想要的:
让%>%变异(lets_sel = map2_chr(lets_list, na_if(posn, 0), ~ .x[.y]))#># 小费:3 x 3#>posnlets_listlets_sel#><int><列表><chr>#>1 2 <chr [5]>乙#>2 1 <chr [5]>一种#>3 0 <chr [5]><不适用>
由 reprex 包 (v0.2.0.9000) 于 2018 年 8 月 7 日创建.>
I suspect that this is a bug in the purrr package, but would like to check my logic in StackOverflow first, please.
It seems to me that the possibly
function is not working inside the map2_chr
function. I'm using purrr version 0.2.5
Consider this example:
library(dplyr)
library(purrr)
lets <- tibble(posn = 2:0,
lets_list = list(letters[1:5], letters[1:5], letters[1:5])) %>%
glimpse()
returns
Observations: 3
Variables: 2
$ posn <int> 2, 1, 0
$ lets_list <list> [<"a", "b", "c", "d", "e">, <"a", "b", "c", "d", "e">, <"a", "b", "c", "d", "e">]
In this example, I want to create another column using mutate to return the element in the list "lets_list" based on the value in "posn".
lets %>%
mutate(lets_sel = map2_chr(lets_list, posn, ~.x[.y]))
fails with this error message as the third row have posn = 0.
> lets %>%
+ mutate(lets_sel = map2_chr(lets_list, posn, ~.x[.y]))
# Error in mutate_impl(.data, dots) :
# Evaluation error: Result 3 is not a length 1 atomic vector.
Using the possibly
function with map2_chr
returns an error too.
lets %>%
mutate(lets_sel = map2_chr(lets_list, posn, possibly(~.x[.y], NA_character_)))
# Error in mutate_impl(.data, dots) :
# Evaluation error: Result 3 is not a length 1 atomic vector.
However, the map2
function works fine:
> lets %>%
+ mutate(lets_sel = map2(lets_list, posn, possibly(~.x[.y], NA_character_)))
# A tibble: 3 x 3
posn lets_list lets_sel
<int> <list> <list>
1 2 <chr [5]> <chr [1]>
2 1 <chr [5]> <chr [1]>
3 0 <chr [5]> <chr [0]>
A workaround solution is to use map2 and then map_chr, but I suspect that this is a bug.
> lets %>%
+ mutate(lets_sel = map2(lets_list, posn, ~.x[.y]),
+ lets_sel = map_chr(lets_sel, possibly(~.x[1], NA_character_)))
# A tibble: 3 x 3
posn lets_list lets_sel
<int> <list> <chr>
1 2 <chr [5]> b
2 1 <chr [5]> a
3 0 <chr [5]> NA
Am I missing something here? Thanks.
possibly()
doesn't work because indexing with 0
doesn't throw an error;
it just returns a length 0 vector:
nth_letter <- function(n) letters[n]
possibly(nth_letter, "not returned")(0)
#> character(0)
nth_letter(0)
#> character(0)
In this case it would probably be easier to replace invalid indices with NA
(using e.g. dplyr::na_if()
, or plain old ifelse
if the real problem is more complex) to get what you are after:
lets %>%
mutate(lets_sel = map2_chr(lets_list, na_if(posn, 0), ~ .x[.y]))
#> # A tibble: 3 x 3
#> posn lets_list lets_sel
#> <int> <list> <chr>
#> 1 2 <chr [5]> b
#> 2 1 <chr [5]> a
#> 3 0 <chr [5]> <NA>
Created on 2018-08-07 by the reprex package (v0.2.0.9000).
这篇关于purrr::possively 函数可能不适用于 map2_chr 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!