operate_cols: Flexibly operate on sets of variables within a dataframe

Description Usage Arguments Examples

Description

This simple function allows for simple operations to be performed on sets of variables within a dataframe. It is useful mainly for maintaining code consistency and saving subsetting code. It can add, divide, multiply and subtract by a constant, and will perform the same operation on all variables passed in.

Usage

1
operate_cols(data, variables, operation = "add", number)

Arguments

data

The input dataset

variables

A character vector of variables to be operated on

operation

one of 'add', 'subtract', 'multiply', 'divide'

number

A constant. This should usually be positive (subtract will automatically flip it), and will make sense to say out loud "subtract 2" rather than "subtract -2" is usually what is intended. Of course, you can multiply by -4, if for some reason you so wish...

Examples

1
2
# Subtract one from the carb column of mtcars
operate_cols(mtcars, 'carb', 'subtract', 1)

seanchrismurphy/emacalc documentation built on May 12, 2019, 2:03 p.m.