Unexpected behavior -- pmap and select

I'm running into a strange issue when using pmap and select. Here's a stripped down version of what works and what doesn't.

For a two argument function, .x and .y work to select variables from a dataset:

pmap(
  list("mpg", "wt"), 
  ~select(mtcars, cyl, disp, .x, .y))

For a three argument function, ..1, ..2, and ..3 don't work to select variables from a dataset:

pmap(
  list("mpg", "wt", "carb"), 
  ~select(mtcars, cyl, disp, ..1, ..2, ..3))

In the second example, it seems you can select if you use !!(..1) etc -- not sure why or if that's intended. Any thoughts?

I don't think I'm answering your question in full, but I think this will make it easier to discuss.

Here's a reprex of three scenarios. In the last, I'm using the big bang, !!!, to get the names of the columns being referenced by ..1, ..2, etc.

I believe thast's what's happening here, select() is looking for the names of the columns (hence the error saying that there isn't a variable named ..1. Note also that with the dev version, you'll get a warning about using an external vector (though it works), and a recommendation to see the select helpers:

library(tidyverse)

pmap(
  list("mpg", "wt"),
  ~ select(mtcars, cyl, disp, .x, .y)
)
#> Note: Using an external vector in selections is ambiguous.
#> ℹ Use `all_of(.x)` instead of `.x` to silence this message.
#> ℹ See <https://tidyselect.r-lib.org/reference/faq-external-vector.html>.
#> This message is displayed once per session.
#> Note: Using an external vector in selections is ambiguous.
#> ℹ Use `all_of(.y)` instead of `.y` to silence this message.
#> ℹ See <https://tidyselect.r-lib.org/reference/faq-external-vector.html>.
#> This message is displayed once per session.
#> [[1]]
#>                     cyl  disp  mpg    wt
#> Mazda RX4             6 160.0 21.0 2.620
#> Mazda RX4 Wag         6 160.0 21.0 2.875
#> Datsun 710            4 108.0 22.8 2.320
#> Hornet 4 Drive        6 258.0 21.4 3.215
#> Hornet Sportabout     8 360.0 18.7 3.440
#> Valiant               6 225.0 18.1 3.460
#> Duster 360            8 360.0 14.3 3.570
#> Merc 240D             4 146.7 24.4 3.190
#> Merc 230              4 140.8 22.8 3.150
#> Merc 280              6 167.6 19.2 3.440
#> Merc 280C             6 167.6 17.8 3.440
#> Merc 450SE            8 275.8 16.4 4.070
#> Merc 450SL            8 275.8 17.3 3.730
#> Merc 450SLC           8 275.8 15.2 3.780
#> Cadillac Fleetwood    8 472.0 10.4 5.250
#> Lincoln Continental   8 460.0 10.4 5.424
#> Chrysler Imperial     8 440.0 14.7 5.345
#> Fiat 128              4  78.7 32.4 2.200
#> Honda Civic           4  75.7 30.4 1.615
#> Toyota Corolla        4  71.1 33.9 1.835
#> Toyota Corona         4 120.1 21.5 2.465
#> Dodge Challenger      8 318.0 15.5 3.520
#> AMC Javelin           8 304.0 15.2 3.435
#> Camaro Z28            8 350.0 13.3 3.840
#> Pontiac Firebird      8 400.0 19.2 3.845
#> Fiat X1-9             4  79.0 27.3 1.935
#> Porsche 914-2         4 120.3 26.0 2.140
#> Lotus Europa          4  95.1 30.4 1.513
#> Ford Pantera L        8 351.0 15.8 3.170
#> Ferrari Dino          6 145.0 19.7 2.770
#> Maserati Bora         8 301.0 15.0 3.570
#> Volvo 142E            4 121.0 21.4 2.780

pmap(
  list("mpg", "wt", "carb"),
  ~ select(mtcars, cyl, disp, ..1, ..2, ..3)
)
#> Error: Can't subset columns that don't exist.
#> x Column `..1` doesn't exist.

pmap(
  list("mpg", "wt", "carb"),
  ~ select(mtcars, cyl, disp, !!!(list(..1, ..2, ..3)))
)
#> [[1]]
#>                     cyl  disp  mpg    wt carb
#> Mazda RX4             6 160.0 21.0 2.620    4
#> Mazda RX4 Wag         6 160.0 21.0 2.875    4
#> Datsun 710            4 108.0 22.8 2.320    1
#> Hornet 4 Drive        6 258.0 21.4 3.215    1
#> Hornet Sportabout     8 360.0 18.7 3.440    2
#> Valiant               6 225.0 18.1 3.460    1
#> Duster 360            8 360.0 14.3 3.570    4
#> Merc 240D             4 146.7 24.4 3.190    2
#> Merc 230              4 140.8 22.8 3.150    2
#> Merc 280              6 167.6 19.2 3.440    4
#> Merc 280C             6 167.6 17.8 3.440    4
#> Merc 450SE            8 275.8 16.4 4.070    3
#> Merc 450SL            8 275.8 17.3 3.730    3
#> Merc 450SLC           8 275.8 15.2 3.780    3
#> Cadillac Fleetwood    8 472.0 10.4 5.250    4
#> Lincoln Continental   8 460.0 10.4 5.424    4
#> Chrysler Imperial     8 440.0 14.7 5.345    4
#> Fiat 128              4  78.7 32.4 2.200    1
#> Honda Civic           4  75.7 30.4 1.615    2
#> Toyota Corolla        4  71.1 33.9 1.835    1
#> Toyota Corona         4 120.1 21.5 2.465    1
#> Dodge Challenger      8 318.0 15.5 3.520    2
#> AMC Javelin           8 304.0 15.2 3.435    2
#> Camaro Z28            8 350.0 13.3 3.840    4
#> Pontiac Firebird      8 400.0 19.2 3.845    2
#> Fiat X1-9             4  79.0 27.3 1.935    1
#> Porsche 914-2         4 120.3 26.0 2.140    2
#> Lotus Europa          4  95.1 30.4 1.513    2
#> Ford Pantera L        8 351.0 15.8 3.170    4
#> Ferrari Dino          6 145.0 19.7 2.770    6
#> Maserati Bora         8 301.0 15.0 3.570    8
#> Volvo 142E            4 121.0 21.4 2.780    2

Created on 2020-04-16 by the reprex package (v0.3.0.9001)

Thanks -- that helps clarify a bit, and the note / warning directing to the FAQ about external vectors is also useful.

The reprex below is maybe a bit more consistent with the suggestion in the FAQ, and I'm including it mostly for completeness.

library(tidyverse)

pmap(
  list("mpg", "wt", "carb"),
  ~ select(mtcars, cyl, disp, all_of(c(..1, ..2, ..3)))
)
#> [[1]]
#>                     cyl  disp  mpg    wt carb
#> Mazda RX4             6 160.0 21.0 2.620    4
#> Mazda RX4 Wag         6 160.0 21.0 2.875    4
#> Datsun 710            4 108.0 22.8 2.320    1
#> Hornet 4 Drive        6 258.0 21.4 3.215    1
#> Hornet Sportabout     8 360.0 18.7 3.440    2
#> Valiant               6 225.0 18.1 3.460    1
#> Duster 360            8 360.0 14.3 3.570    4
#> Merc 240D             4 146.7 24.4 3.190    2
#> Merc 230              4 140.8 22.8 3.150    2
#> Merc 280              6 167.6 19.2 3.440    4
#> Merc 280C             6 167.6 17.8 3.440    4
#> Merc 450SE            8 275.8 16.4 4.070    3
#> Merc 450SL            8 275.8 17.3 3.730    3
#> Merc 450SLC           8 275.8 15.2 3.780    3
#> Cadillac Fleetwood    8 472.0 10.4 5.250    4
#> Lincoln Continental   8 460.0 10.4 5.424    4
#> Chrysler Imperial     8 440.0 14.7 5.345    4
#> Fiat 128              4  78.7 32.4 2.200    1
#> Honda Civic           4  75.7 30.4 1.615    2
#> Toyota Corolla        4  71.1 33.9 1.835    1
#> Toyota Corona         4 120.1 21.5 2.465    1
#> Dodge Challenger      8 318.0 15.5 3.520    2
#> AMC Javelin           8 304.0 15.2 3.435    2
#> Camaro Z28            8 350.0 13.3 3.840    4
#> Pontiac Firebird      8 400.0 19.2 3.845    2
#> Fiat X1-9             4  79.0 27.3 1.935    1
#> Porsche 914-2         4 120.3 26.0 2.140    2
#> Lotus Europa          4  95.1 30.4 1.513    2
#> Ford Pantera L        8 351.0 15.8 3.170    4
#> Ferrari Dino          6 145.0 19.7 2.770    6
#> Maserati Bora         8 301.0 15.0 3.570    8
#> Volvo 142E            4 121.0 21.4 2.780    2

I'm still not clear why the shift from 2 arguments to 3 is the breakpoint, but I suppose sometimes things just are that way :man_shrugging:

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.