这个问题可能与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"))
在第一种情况下,df
是的预期第一个参数filter
。df
以后可以使用访问.
。
在第二种情况下,df
不是的预期第一个参数pmap_lgl
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句