You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

受Jennifer Bryan演讲启发转用tidyverse,首次使用pmap遇执行失败求助

Why Your pmap Code Isn't Working (and Fixes!)

Hey there! Let's walk through what's going wrong with your tidyverse code and get it working right.

First, the Root Causes

Your code hits two main issues:

  1. Misusing pmap: pmap is designed to work with lists (or data frames) where each element of the list corresponds to a separate argument for your function. You passed sample1 (a 5-element character vector) directly, so pmap tries to pass 5 separate arguments to your single-parameter function—this is a mismatch that throws an error.
  2. Comparing the whole tibble instead of a column: prob == x checks every value in the entire prob tibble against x, but you only care about matching against the names column. That's not what you want, and it creates messy output even if it ran.

Fixes That Work

Since your goal is to check each value in sample1 against the names column in prob, here are two clean tidyverse approaches:

1. Use purrr::map (the right tool for single-vector iteration)

map is made exactly for iterating over a single vector and applying a function to each element. This is simpler and more appropriate than pmap here:

# Load packages
library(tidyverse)

# Create your data
prob = tibble(names = letters[1:17])
sample1 = sample(letters, 5)

# Working map approach
map(sample1, ~ prob$names == .x)

The ~ creates a formula-style shortcut for your function, and .x refers to each element in sample1.

2. Wrap into a tibble for cleaner, tabular output

If you want to keep your sample values paired with their match results, use mutate with map:

tibble(sample_value = sample1) %>%
  mutate(match_results = map(sample_value, ~ prob$names == .x))

This gives you a tidy tibble where each row has a sample value and its corresponding logical vector of matches.

3. If you really want to use pmap (though map is better here)

If you're set on pmap, you need to wrap sample1 into a single-element list (so pmap treats each element of sample1 as the first (and only) argument to your function):

pmap(list(sample1), function(x) prob$names == x)

Again, this is overkill for your use case—map is the more intuitive choice here.

内容的提问来源于stack exchange,提问作者Karl Cottenie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:10:15