使用 dplyr 在除某些列之外的所有列中进行变异

Mutate across all but some columns using dplyr

我发现 across() 对于在多个列上重复操作非常有用。 但是,我还没有完全理解如何select特定列进行操作。

假设我想将函数应用于 mtcars 除了 gearcarb.[=17 中的所有列=]

我试过

# Function to use over columns
demean <- function(x) {
  x - mean(x, na.rm = TRUE)
}

# Use function on all but columns gear and carb
mtcars %>% mutate(across(.cols = select(.,-gear,-carb), demean))

但是,这会引发错误

Error: Problem with `mutate()` input `..1`.
x Must subset columns with a valid subscript vector.
x Subscript has the wrong type `data.frame<

取消select across 中某些列的正确方法是什么?

这比你想象的要容易:

mtcars %>% mutate(across(-c(gear, carb), demean))
                        mpg     cyl        disp       hp       drat       wt
Mazda RX4          0.909375 -0.1875  -70.721875 -36.6875  0.3034375 -0.59725
Mazda RX4 Wag      0.909375 -0.1875  -70.721875 -36.6875  0.3034375 -0.34225
Datsun 710         2.709375 -2.1875 -122.721875 -53.6875  0.2534375 -0.89725
Hornet 4 Drive     1.309375 -0.1875   27.278125 -36.6875 -0.5165625 -0.00225
Hornet Sportabout -1.390625  1.8125  129.278125  28.3125 -0.4465625  0.22275
Valiant           -1.990625 -0.1875   -5.721875 -41.6875 -0.8365625  0.24275
Duster 360        -5.790625  1.8125  129.278125  98.3125 -0.3865625  0.35275
Merc 240D          4.309375 -2.1875  -84.021875 -84.6875  0.0934375 -0.02725
Merc 230           2.709375 -2.1875  -89.921875 -51.6875  0.3234375 -0.06725
                      qsec      vs       am gear carb
Mazda RX4         -1.38875 -0.4375  0.59375    4    4
Mazda RX4 Wag     -0.82875 -0.4375  0.59375    4    4
Datsun 710         0.76125  0.5625  0.59375    4    1
Hornet 4 Drive     1.59125  0.5625 -0.40625    3    1
Hornet Sportabout -0.82875 -0.4375 -0.40625    3    2
Valiant            2.37125  0.5625 -0.40625    3    1
Duster 360        -2.00875 -0.4375 -0.40625    3    4
Merc 240D          2.15125  0.5625 -0.40625    4    2
Merc 230           5.05125  0.5625 -0.40625    4    2
 [ reached 'max' / getOption("max.print") -- omitted 23 rows ]

或者:

mtcars %>% mutate(across(-gear & -carb, demean))
#>                           mpg     cyl        disp       hp       drat       wt
#> Mazda RX4            0.909375 -0.1875  -70.721875 -36.6875  0.3034375 -0.59725
#> Mazda RX4 Wag        0.909375 -0.1875  -70.721875 -36.6875  0.3034375 -0.34225
#> Datsun 710           2.709375 -2.1875 -122.721875 -53.6875  0.2534375 -0.89725
#> Hornet 4 Drive       1.309375 -0.1875   27.278125 -36.6875 -0.5165625 -0.00225
#> Hornet Sportabout   -1.390625  1.8125  129.278125  28.3125 -0.4465625  0.22275
#> Valiant             -1.990625 -0.1875   -5.721875 -41.6875 -0.8365625  0.24275
#> Duster 360          -5.790625  1.8125  129.278125  98.3125 -0.3865625  0.35275
#> Merc 240D            4.309375 -2.1875  -84.021875 -84.6875  0.0934375 -0.02725
#> Merc 230             2.709375 -2.1875  -89.921875 -51.6875  0.3234375 -0.06725
#> Merc 280            -0.890625 -0.1875  -63.121875 -23.6875  0.3234375  0.22275
#> Merc 280C           -2.290625 -0.1875  -63.121875 -23.6875  0.3234375  0.22275
#> Merc 450SE          -3.690625  1.8125   45.078125  33.3125 -0.5265625  0.85275
#> Merc 450SL          -2.790625  1.8125   45.078125  33.3125 -0.5265625  0.51275
#> Merc 450SLC         -4.890625  1.8125   45.078125  33.3125 -0.5265625  0.56275
#> Cadillac Fleetwood  -9.690625  1.8125  241.278125  58.3125 -0.6665625  2.03275
#> Lincoln Continental -9.690625  1.8125  229.278125  68.3125 -0.5965625  2.20675
#> Chrysler Imperial   -5.390625  1.8125  209.278125  83.3125 -0.3665625  2.12775
#> Fiat 128            12.309375 -2.1875 -152.021875 -80.6875  0.4834375 -1.01725
#> Honda Civic         10.309375 -2.1875 -155.021875 -94.6875  1.3334375 -1.60225
#> Toyota Corolla      13.809375 -2.1875 -159.621875 -81.6875  0.6234375 -1.38225
#> Toyota Corona        1.409375 -2.1875 -110.621875 -49.6875  0.1034375 -0.75225
#> Dodge Challenger    -4.590625  1.8125   87.278125   3.3125 -0.8365625  0.30275
#> AMC Javelin         -4.890625  1.8125   73.278125   3.3125 -0.4465625  0.21775
#> Camaro Z28          -6.790625  1.8125  119.278125  98.3125  0.1334375  0.62275
#> Pontiac Firebird    -0.890625  1.8125  169.278125  28.3125 -0.5165625  0.62775
#> Fiat X1-9            7.209375 -2.1875 -151.721875 -80.6875  0.4834375 -1.28225
#> Porsche 914-2        5.909375 -2.1875 -110.421875 -55.6875  0.8334375 -1.07725
#> Lotus Europa        10.309375 -2.1875 -135.621875 -33.6875  0.1734375 -1.70425
#> Ford Pantera L      -4.290625  1.8125  120.278125 117.3125  0.6234375 -0.04725
#> Ferrari Dino        -0.390625 -0.1875  -85.721875  28.3125  0.0234375 -0.44725
#> Maserati Bora       -5.090625  1.8125   70.278125 188.3125 -0.0565625  0.35275
#> Volvo 142E           1.309375 -2.1875 -109.721875 -37.6875  0.5134375 -0.43725
#>                         qsec      vs       am gear carb
#> Mazda RX4           -1.38875 -0.4375  0.59375    4    4
#> Mazda RX4 Wag       -0.82875 -0.4375  0.59375    4    4
#> Datsun 710           0.76125  0.5625  0.59375    4    1
#> Hornet 4 Drive       1.59125  0.5625 -0.40625    3    1
#> Hornet Sportabout   -0.82875 -0.4375 -0.40625    3    2
#> Valiant              2.37125  0.5625 -0.40625    3    1
#> Duster 360          -2.00875 -0.4375 -0.40625    3    4
#> Merc 240D            2.15125  0.5625 -0.40625    4    2
#> Merc 230             5.05125  0.5625 -0.40625    4    2
#> Merc 280             0.45125  0.5625 -0.40625    4    4
#> Merc 280C            1.05125  0.5625 -0.40625    4    4
#> Merc 450SE          -0.44875 -0.4375 -0.40625    3    3
#> Merc 450SL          -0.24875 -0.4375 -0.40625    3    3
#> Merc 450SLC          0.15125 -0.4375 -0.40625    3    3
#> Cadillac Fleetwood   0.13125 -0.4375 -0.40625    3    4
#> Lincoln Continental -0.02875 -0.4375 -0.40625    3    4
#> Chrysler Imperial   -0.42875 -0.4375 -0.40625    3    4
#> Fiat 128             1.62125  0.5625  0.59375    4    1
#> Honda Civic          0.67125  0.5625  0.59375    4    2
#> Toyota Corolla       2.05125  0.5625  0.59375    4    1
#> Toyota Corona        2.16125  0.5625 -0.40625    3    1
#> Dodge Challenger    -0.97875 -0.4375 -0.40625    3    2
#> AMC Javelin         -0.54875 -0.4375 -0.40625    3    2
#> Camaro Z28          -2.43875 -0.4375 -0.40625    3    4
#> Pontiac Firebird    -0.79875 -0.4375 -0.40625    3    2
#> Fiat X1-9            1.05125  0.5625  0.59375    4    1
#> Porsche 914-2       -1.14875 -0.4375  0.59375    5    2
#> Lotus Europa        -0.94875  0.5625  0.59375    5    2
#> Ford Pantera L      -3.34875 -0.4375  0.59375    5    4
#> Ferrari Dino        -2.34875 -0.4375  0.59375    5    6
#> Maserati Bora       -3.24875 -0.4375  0.59375    5    8
#> Volvo 142E           0.75125  0.5625  0.59375    4    2