scale_at: Rescale columns of tabular data

scale_atR Documentation

Rescale columns of tabular data

Description

multiply_at() and divide_at() avoid one of the pitfalls of mutate_at(), which is that mutate_at() applies transformations sequentially, rather than in parallel. See Details, below.

Usage

multiply_at(...)

divide_at(...)

Arguments

.tbl

tabular data

.vars

A list of columns generated by vars(), a character vector of column names, a numeric vector of column positions, or NULL.

by

a value or expression, to be evaluated within .tbl

Details

Suppose we want to "normalize" all columns by the values in one column. The following code doesn't yield that result:

mtcars %>% mutate_at(vars(everything()), ~ 1 / wt)

All columns after wt are left "untouched". Why?

What's happening is that the columns are being transformed sequentially, rather than in parallel. First mpg is divided by wt. Then, cyl is divided by wt, followed by disp, hp, and drat.

Next, wt is divided by wt. From this point on, wt is equal to 1.

When the next column, qsec, is divided by wt, it's just being divided by 1. And so on for vs, am, gear, and carb. The values in those columns remain the same.

Functions

  • multiply_at: multiplication

  • divide_at: division


BAAQMD/tbltools documentation built on Feb. 29, 2024, 5:45 a.m.