Mutate multiple columns

mutateAll(object, fun, ...)

mutateAll(object, fun, ...)

mutateIf(object, predicate, fun, ...)

transmuteAt(object, vars, fun, ...)

transmuteIf(object, predicate, fun, ...)

# S4 method for DataFrame,`function`
mutateAll(object, fun, ...)

# S4 method for DataFrame,character,`function`
mutateAt(object, vars, fun, ...)

# S4 method for DataFrame,`function`,`function`
mutateIf(object, predicate, fun, ...)

# S4 method for DataFrame,character,`function`
transmuteAt(object, vars, fun, ...)

# S4 method for DataFrame,`function`,`function`
transmuteIf(object, predicate, fun, ...)

Arguments

object

Object.

fun

function. Mutation function.

...

Passthrough arguments to function declared in fun argument.

vars

character. Column names.

predicate

function. A predicate function to be applied to the columns. For example, is.atomic().

Value

Modified object.

Note

Updated 2019-09-02.

See also

These functions are inspired by dplyr. However, they are designed to only work on Bioconductor S4 class objects, and use base R code internally.

Examples

data(mtcars, package = "datasets") ## DataFrame ==== x <- as(mtcars, "DataFrame") mutateAll(x, fun = log, base = 2L)
#> DataFrame with 32 rows and 11 columns #> mpg cyl disp #> <numeric> <numeric> <numeric> #> Mazda RX4 4.39231742277876 2.58496250072116 7.32192809488736 #> Mazda RX4 Wag 4.39231742277876 2.58496250072116 7.32192809488736 #> Datsun 710 4.51096191927738 2 6.75488750216347 #> Hornet 4 Drive 4.41953889151378 2.58496250072116 8.01122725542325 #> Hornet Sportabout 4.22496636500027 3 8.49185309632967 #> ... ... ... ... #> Lotus Europa 4.92599941855622 2 6.5713734359732 #> Ford Pantera L 3.98185265328974 3 8.45532722030456 #> Ferrari Dino 4.30012372456901 2.58496250072116 7.17990909001493 #> Maserati Bora 3.90689059560852 3 8.2336196767597 #> Volvo 142E 4.41953889151378 2 6.91886323727459 #> hp drat wt #> <numeric> <numeric> <numeric> #> Mazda RX4 6.78135971352466 1.96347412397489 1.38956681176273 #> Mazda RX4 Wag 6.78135971352466 1.96347412397489 1.52356195605701 #> Datsun 710 6.53915881110803 1.94485844580754 1.21412480535285 #> Hornet 4 Drive 6.78135971352466 1.62293035092018 1.68481873755322 #> Hornet Sportabout 7.45121111183233 1.65535182861255 1.78240856492737 #> ... ... ... ... #> Lotus Europa 6.82017896241519 1.91456452349394 0.59741198755465 #> Ford Pantera L 8.04439411935845 2.07724299893246 1.66448284036468 #> Ferrari Dino 7.45121111183233 1.85598969730848 1.46988597627446 #> Maserati Bora 8.38801728534514 1.82374936030827 1.83592407425437 #> Volvo 142E 6.76818432477693 2.03913839390696 1.47508488294878 #> qsec vs am gear #> <numeric> <numeric> <numeric> <numeric> #> Mazda RX4 4.0408924306469 -Inf 0 2 #> Mazda RX4 Wag 4.08915913191124 -Inf 0 2 #> Datsun 710 4.21800615028443 0 0 2 #> Hornet 4 Drive 4.28095631383106 0 -Inf 1.58496250072116 #> Hornet Sportabout 4.08915913191124 -Inf -Inf 1.58496250072116 #> ... ... ... ... ... #> Lotus Europa 4.07895134139482 0 0 2.32192809488736 #> Ford Pantera L 3.85798099512757 -Inf 0 2.32192809488736 #> Ferrari Dino 3.95419631038688 -Inf 0 2.32192809488736 #> Maserati Bora 3.86789646399265 -Inf 0 2.32192809488736 #> Volvo 142E 4.21723071622067 0 0 2 #> carb #> <numeric> #> Mazda RX4 2 #> Mazda RX4 Wag 2 #> Datsun 710 0 #> Hornet 4 Drive 0 #> Hornet Sportabout 1 #> ... ... #> Lotus Europa 1 #> Ford Pantera L 2 #> Ferrari Dino 2.58496250072116 #> Maserati Bora 3 #> Volvo 142E 1
mutateAt(x, vars = c("mpg", "cyl"), fun = log, base = 2L)
#> DataFrame with 32 rows and 11 columns #> mpg cyl disp hp #> <numeric> <numeric> <numeric> <numeric> #> Mazda RX4 4.39231742277876 2.58496250072116 160 110 #> Mazda RX4 Wag 4.39231742277876 2.58496250072116 160 110 #> Datsun 710 4.51096191927738 2 108 93 #> Hornet 4 Drive 4.41953889151378 2.58496250072116 258 110 #> Hornet Sportabout 4.22496636500027 3 360 175 #> ... ... ... ... ... #> Lotus Europa 4.92599941855622 2 95.1 113 #> Ford Pantera L 3.98185265328974 3 351 264 #> Ferrari Dino 4.30012372456901 2.58496250072116 145 175 #> Maserati Bora 3.90689059560852 3 301 335 #> Volvo 142E 4.41953889151378 2 121 109 #> drat wt qsec vs am gear #> <numeric> <numeric> <numeric> <numeric> <numeric> <numeric> #> Mazda RX4 3.9 2.62 16.46 0 1 4 #> Mazda RX4 Wag 3.9 2.875 17.02 0 1 4 #> Datsun 710 3.85 2.32 18.61 1 1 4 #> Hornet 4 Drive 3.08 3.215 19.44 1 0 3 #> Hornet Sportabout 3.15 3.44 17.02 0 0 3 #> ... ... ... ... ... ... ... #> Lotus Europa 3.77 1.513 16.9 1 1 5 #> Ford Pantera L 4.22 3.17 14.5 0 1 5 #> Ferrari Dino 3.62 2.77 15.5 0 1 5 #> Maserati Bora 3.54 3.57 14.6 0 1 5 #> Volvo 142E 4.11 2.78 18.6 1 1 4 #> carb #> <numeric> #> Mazda RX4 4 #> Mazda RX4 Wag 4 #> Datsun 710 1 #> Hornet 4 Drive 1 #> Hornet Sportabout 2 #> ... ... #> Lotus Europa 2 #> Ford Pantera L 4 #> Ferrari Dino 6 #> Maserati Bora 8 #> Volvo 142E 2
mutateIf(x, predicate = is.double, fun = as.integer)
#> DataFrame with 32 rows and 11 columns #> mpg cyl disp hp drat wt #> <integer> <integer> <integer> <integer> <integer> <integer> #> Mazda RX4 21 6 160 110 3 2 #> Mazda RX4 Wag 21 6 160 110 3 2 #> Datsun 710 22 4 108 93 3 2 #> Hornet 4 Drive 21 6 258 110 3 3 #> Hornet Sportabout 18 8 360 175 3 3 #> ... ... ... ... ... ... ... #> Lotus Europa 30 4 95 113 3 1 #> Ford Pantera L 15 8 351 264 4 3 #> Ferrari Dino 19 6 145 175 3 2 #> Maserati Bora 15 8 301 335 3 3 #> Volvo 142E 21 4 121 109 4 2 #> qsec vs am gear carb #> <integer> <integer> <integer> <integer> <integer> #> Mazda RX4 16 0 1 4 4 #> Mazda RX4 Wag 17 0 1 4 4 #> Datsun 710 18 1 1 4 1 #> Hornet 4 Drive 19 1 0 3 1 #> Hornet Sportabout 17 0 0 3 2 #> ... ... ... ... ... ... #> Lotus Europa 16 1 1 5 2 #> Ford Pantera L 14 0 1 5 4 #> Ferrari Dino 15 0 1 5 6 #> Maserati Bora 14 0 1 5 8 #> Volvo 142E 18 1 1 4 2
transmuteAt(x, vars = c("mpg", "cyl"), fun = log, base = 2L)
#> DataFrame with 32 rows and 2 columns #> mpg cyl #> <numeric> <numeric> #> Mazda RX4 4.39231742277876 2.58496250072116 #> Mazda RX4 Wag 4.39231742277876 2.58496250072116 #> Datsun 710 4.51096191927738 2 #> Hornet 4 Drive 4.41953889151378 2.58496250072116 #> Hornet Sportabout 4.22496636500027 3 #> ... ... ... #> Lotus Europa 4.92599941855622 2 #> Ford Pantera L 3.98185265328974 3 #> Ferrari Dino 4.30012372456901 2.58496250072116 #> Maserati Bora 3.90689059560852 3 #> Volvo 142E 4.41953889151378 2
transmuteIf(x, predicate = is.double, fun = as.integer)
#> DataFrame with 32 rows and 11 columns #> mpg cyl disp hp drat wt #> <integer> <integer> <integer> <integer> <integer> <integer> #> Mazda RX4 21 6 160 110 3 2 #> Mazda RX4 Wag 21 6 160 110 3 2 #> Datsun 710 22 4 108 93 3 2 #> Hornet 4 Drive 21 6 258 110 3 3 #> Hornet Sportabout 18 8 360 175 3 3 #> ... ... ... ... ... ... ... #> Lotus Europa 30 4 95 113 3 1 #> Ford Pantera L 15 8 351 264 4 3 #> Ferrari Dino 19 6 145 175 3 2 #> Maserati Bora 15 8 301 335 3 3 #> Volvo 142E 21 4 121 109 4 2 #> qsec vs am gear carb #> <integer> <integer> <integer> <integer> <integer> #> Mazda RX4 16 0 1 4 4 #> Mazda RX4 Wag 17 0 1 4 4 #> Datsun 710 18 1 1 4 1 #> Hornet 4 Drive 19 1 0 3 1 #> Hornet Sportabout 17 0 0 3 2 #> ... ... ... ... ... ... #> Lotus Europa 16 1 1 5 2 #> Ford Pantera L 14 0 1 5 4 #> Ferrari Dino 15 0 1 5 6 #> Maserati Bora 14 0 1 5 8 #> Volvo 142E 18 1 1 4 2