Home > Back-end >  Using pmax/pmin with vector of variable string names in R
Using pmax/pmin with vector of variable string names in R

Time:11-16

Is there a way to use pmax and pmin function in R with a vector of string variable names using the tidyverse (dplyr) format?

For instance, I want to run the following:

data(mtcars)
mtcars %>% mutate(maxval = pmax(drat, wt, na.rm = T)

This properly gets me the following:

                     mpg cyl  disp  hp drat    wt  qsec vs am gear carb maxval
Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4  3.900
Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4  3.900
Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1  3.850
Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1  3.215
Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2  3.440
Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1  3.460
Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4  3.570
Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2  3.690
Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2  3.920
Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4  3.920
Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4  3.920
Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3  4.070
Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3  3.730
Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3  3.780
Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4  5.250
Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4  5.424
Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4  5.345
Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1  4.080

But, say that I'm working on a very large data with quite a lot of variables, and I want to just use a vector of strings like x1 = sprintf("xval_%1.0f", 1:25) where x1 will be the list of variable columns I want to run pmax and pmin with. But when I do this, then I'm always given an error message that confuses the number of vectors with the number of observations. For instance, say I run the following:

values = c("drat", "wt")
mtcars %>% mutate(maxval = pmax(all_of(values), na.rm = T))

Then I get the following error:

Error: Problem with `mutate()` column `maxval`.
i `maxval = pmax(values, na.rm = T)`.
i `maxval` must be size 32 or 1, not 2.

Which seems to be getting at the number of observations (32).

CodePudding user response:

We may use invoke (similar to do.call in base R) with across

library(purrr)
library(dplyr)
out <- mtcars %>% 
        mutate(maxval = invoke(pmax, c(across(all_of(values)), na.rm = TRUE)))
        # or use do.call
        #  mutate(maxval = do.call(pmax, c(across(all_of(values)), na.rm = TRUE)))

-output

> head(out)
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb maxval
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4  3.900
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4  3.900
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1  3.850
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1  3.215
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2  3.440
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1  3.460

Or may use exec as well

out2 <- mtcars %>%
      mutate(maxval = exec(pmax, !!! rlang::syms(values), na.rm = TRUE))

-output

> head(out2)
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb maxval
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4  3.900
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4  3.900
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1  3.850
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1  3.215
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2  3.440
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1  3.460
  • Related