conv_: Apply a given function to specified columns in a data table

View source: R/utils-conv.R

conv_R Documentation

Apply a given function to specified columns in a data table

Description

Apply a given function to specified columns in a data table

Usage

conv_(df, cols, f, ..., newcol)

Arguments

df

a data table

cols

a character vector of column names to which the function f is applied

f

a function that applies to each column.

...

arguments to pass to function f

newcol

a character vector of new column names to which the output of f will be applied.

Details

This function modifies by reference. If passed a column name that does not exist in the provided data table, this will simply return the original data.table unmodified without error.

Examples

x <- data.table::data.table(x = rep("APPLE", 10), y = rep("BANANA", 10), z = rep("CHERRY", 10))
nsqipr:::conv_(x, c("x","y"), tolower)
x

x <- data.table::data.table(x = rep("APPLE", 10), y = rep("BANANA", 10), z = rep("CHERRY", 10))
nsqipr:::conv_(x, c("x","y"), tolower, newcol =  c("X","Y"))
x

x <- data.table::data.table(x = rep("APPLE", 10), y = rep("BANANA", 10), z = rep("CHERRY", 10))
nsqipr:::conv_(x, "x", paste, "JUICE", sep = " ", newcol = "drink")


dylanrussellmd/nsqipr documentation built on Oct. 13, 2023, 11:01 a.m.