purrr:将 %in% 与列表列一起使用

Gre*_*reg 1 r dplyr purrr

我有一列问题responses 和一列可能correct_answers。我想创建第三个(逻辑)列 ( correct) 来显示响应是否与可能的正确答案之一匹配。

我想我可能需要使用 purrr 函数,但我不确定如何将其中一个map函数与一起使用%in%

library(tibble)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(purrr)

data <- tibble(
  response = c('a', 'b', 'c'),
  correct_answers = rep(list(c('a', 'b')), 3)
)

# works but correct answers specified manually
data %>%
  mutate(correct = response %in% c('a', 'b'))
#> # A tibble: 3 x 3
#>   response correct_answers correct
#>   <chr>    <list>          <lgl>  
#> 1 a        <chr [2]>       TRUE   
#> 2 b        <chr [2]>       TRUE   
#> 3 c        <chr [2]>       FALSE

# doesn't work
data %>%
  mutate(correct = response %in% correct_answers)
#> # A tibble: 3 x 3
#>   response correct_answers correct
#>   <chr>    <list>          <lgl>  
#> 1 a        <chr [2]>       FALSE  
#> 2 b        <chr [2]>       FALSE  
#> 3 c        <chr [2]>       FALSE
Run Code Online (Sandbox Code Playgroud)

reprex 包(v0.2.1)于 2018 年 11 月 5 日创建

Psi*_*dom 5

%in%不检查列表内的嵌套元素,使用mapply(baseR) 或map2(purrr) 遍历列并检查:

data %>% mutate(correct = mapply(function (res, ans) res %in% ans, response, correct_answers))
# A tibble: 3 x 3
#  response correct_answers correct
#  <chr>    <list>          <lgl>  
#1 a        <chr [2]>       TRUE   
#2 b        <chr [2]>       TRUE   
#3 c        <chr [2]>       FALSE  
Run Code Online (Sandbox Code Playgroud)

使用map2_lgl

library(purrr)
data %>% mutate(correct = map2_lgl(response, correct_answers, ~ .x %in% .y))
# A tibble: 3 x 3
#  response correct_answers correct
#  <chr>    <list>          <lgl>  
#1 a        <chr [2]>       TRUE   
#2 b        <chr [2]>       TRUE   
#3 c        <chr [2]>       FALSE 
Run Code Online (Sandbox Code Playgroud)

或者正如@thelatemail 所评论的,两者都可以简化:

data %>% mutate(correct = mapply(`%in%`, response, correct_answers)) 
data %>% mutate(correct = map2_lgl(response, correct_answers, `%in%`))
Run Code Online (Sandbox Code Playgroud)