Home > Software engineering >  Purrr map a function to all columns except for one
Purrr map a function to all columns except for one

Time:06-29

I would like to apply Blom transformation to all columns but 'ID'. Since they are all numeric, map_if and is.numeric do not work here.

library(rcompanion)
data("mtcars")

# Get a list of the columns that I would like to apply the Blom transformation
col_names = colnames(mtcars)

# Create the column ID so each row has a unique identifier
mtcars = mtcars %>% mutate(id = row_number())

What I came up with is:

mtcars = map_if(mtcars, names(.) %in% col_names ~ rcompanion::blom(.x, method = "blom")

But I received an error: ! Can't convert `.f`, a two-sided formula, to a function.

CodePudding user response:

This could be done within dplyr itself if we use across

library(dplyr)
mtcars <-  mtcars %>% 
    mutate(across(all_of(col_names), rcompanion::blom, method = "blom"))

-output

> head(mtcars)
                         mpg        cyl        disp         hp       drat          wt       qsec         vs         am       gear
Mazda RX4          0.2353289 -0.1168527 -0.23532887 -0.2754823  0.3989603 -0.62056827 -0.9368471 -0.5741128  0.8223940  0.3989603
Mazda RX4 Wag      0.2353289 -0.1168527 -0.23532887 -0.2754823  0.3989603 -0.35721583 -0.4846749 -0.5741128  0.8223940  0.3989603
Datsun 710         0.6684036 -0.9368471 -0.93684711 -0.8223940  0.2754823 -0.82239396  0.5288648  0.7690553  0.8223940  0.3989603
Hornet 4 Drive     0.3989603 -0.1168527  0.11685275 -0.2754823 -0.6684036 -0.03887224  0.8223940  0.7690553 -0.5288648 -0.7178201
Hornet Sportabout -0.1168527  0.7690553  0.99892823  0.3572158 -0.4846749  0.19555148 -0.4846749 -0.5741128 -0.5288648 -0.7178201
Valiant           -0.1955515 -0.1168527  0.03887224 -0.5288648 -1.8134176  0.35721583  1.6411071  0.7690553 -0.5288648 -0.7178201
                        carb id
Mazda RX4          0.7690553  1
Mazda RX4 Wag      0.7690553  2
Datsun 710        -1.2138469  3
Hornet 4 Drive    -1.2138469  4
Hornet Sportabout -0.3160850  5
Valiant           -1.2138469  6

CodePudding user response:

Using purrr::map_at you could do:

library(rcompanion)
library(purrr)
library(dplyr)

mtcars[] <- map_at(mtcars, col_names, ~ rcompanion::blom(.x, method = "blom"))

head(mtcars)
#>                          mpg        cyl        disp         hp       drat
#> Mazda RX4          0.2353289 -0.1168527 -0.23532887 -0.2754823  0.3989603
#> Mazda RX4 Wag      0.2353289 -0.1168527 -0.23532887 -0.2754823  0.3989603
#> Datsun 710         0.6684036 -0.9368471 -0.93684711 -0.8223940  0.2754823
#> Hornet 4 Drive     0.3989603 -0.1168527  0.11685275 -0.2754823 -0.6684036
#> Hornet Sportabout -0.1168527  0.7690553  0.99892823  0.3572158 -0.4846749
#> Valiant           -0.1955515 -0.1168527  0.03887224 -0.5288648 -1.8134176
#>                            wt       qsec         vs         am       gear
#> Mazda RX4         -0.62056827 -0.9368471 -0.5741128  0.8223940  0.3989603
#> Mazda RX4 Wag     -0.35721583 -0.4846749 -0.5741128  0.8223940  0.3989603
#> Datsun 710        -0.82239396  0.5288648  0.7690553  0.8223940  0.3989603
#> Hornet 4 Drive    -0.03887224  0.8223940  0.7690553 -0.5288648 -0.7178201
#> Hornet Sportabout  0.19555148 -0.4846749 -0.5741128 -0.5288648 -0.7178201
#> Valiant            0.35721583  1.6411071  0.7690553 -0.5288648 -0.7178201
#>                         carb id
#> Mazda RX4          0.7690553  1
#> Mazda RX4 Wag      0.7690553  2
#> Datsun 710        -1.2138469  3
#> Hornet 4 Drive    -1.2138469  4
#> Hornet Sportabout -0.3160850  5
#> Valiant           -1.2138469  6
  • Related