purrr:在列表列中使用%in%

时间:2018-11-05 21:44:01

标签: r dplyr purrr

我有一列问题response和一列可能的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

reprex package(v0.2.1)于2018-11-05创建

1 个答案:

答案 0 :(得分:4)

%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  

使用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 

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

data %>% mutate(correct = mapply(`%in%`, response, correct_answers)) 
data %>% mutate(correct = map2_lgl(response, correct_answers, `%in%`))