这个问题可能是关于magrittr的,但我不确定。如果我通过管道将数据(%>%)传递给下一个函数,据我所知,它将转到第一个参数。我有这个示例数据集(df),并希望提取在列col1到col3中包含值101到104的所有行。
# A tibble: 5 x 4
ID col1 col2 col3
<dbl> <dbl> <dbl> <dbl>
1 101 102 201
2 201 202 203
3 104 NA 301
4 101 NA NA
5 201 301 302我能做这些
library(tidyverse)
df %>% filter(pmap_lgl(select(., starts_with("col")), ~any(c(...) %in% c(101:104))))但是,当我只想获取布尔向量时,我会得到一个警告
df %>% pmap_lgl(select(., starts_with("col")), ~any(c(...) %in% c(101:104)))
Error: Can't convert a `spec_tbl_df/tbl_df/tbl/data.frame` object to function我想我可以通过以下方式让它工作
df %>% {pmap_lgl(select(., starts_with("col")), ~any(c(...) %in% c(101:104)))}据我所知,df通常会传递给pmap的第一个参数,但就像这样,它会去点所在的地方。然而,为什么在第一种情况下这是不必要的。
数据:
df <- structure(list(ID = c(1, 2, 3, 4, 5), col1 = c(101, 201, 104,
101, 201), col2 = c(102, 202, NA, NA, 301), col3 = c(201, 203,
301, NA, 302)), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"
), row.names = c(NA, -5L), spec = structure(list(cols = list(
ID = structure(list(), class = c("collector_double", "collector"
)), col1 = structure(list(), class = c("collector_double",
"collector")), col2 = structure(list(), class = c("collector_double",
"collector")), col3 = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1), class = "col_spec"))发布于 2019-11-27 00:47:46
在第一种情况下,df是filter的第一个参数。然后可以使用.访问df。
在第二种情况下,df不是pmap_lgl的预期第一个参数。
https://stackoverflow.com/questions/59055579
复制相似问题