This question is probably about magrittr but I am not sure. If I pipe data (%>%) into the next function it goes to the first argument as far as I understand. I have this example dataset (df) and would like to extract all rows that contain the values 101 to 104 in columns col1 to col3.
# 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
I can do this
library(tidyverse)
df %>% filter(pmap_lgl(select(., starts_with("col")), ~any(c(...) %in% c(101:104))))
However, when I want to get only the boolean vector I get a warning
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
I figured out that I get it to work using
df %>% {pmap_lgl(select(., starts_with("col")), ~any(c(...) %in% c(101:104)))}
As far as I understand df would usually be passed to the first argument of pmap but like this it goes where the dot is. However, why is this not necessary in the first case.
The data:
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"))